Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visitor Pattern Explanation

So I've read up all the documentation about the Visitor pattern, and I'm still mightily confused. I've taken this example from another SO question, could someone help me understand? For instance when do we use a visitor design pattern? I think I may have understood some of it, but I'm just not able to see the bigger picture. How do I know when I can use it?

class equipmentVisited {   virtual void accept(equipmentVisitor* visitor) = 0; }  class floppyDisk : public equipmentVisited {   virtual void accept(equipmentVisitor* visitor); }  class processor : public equipmentVisited {   virtual void accept(equipmentVisitor* visitor); }  class computer : public equipmentVisited {   virtual void accept(equipmentVisitor* visitor); }  class equipmentVisitor {   virtual void visitFloppyDisk(floppyDisk* );   virtual void visitProcessor(processor* );   virtual void visitComputer(computer* ); }  // Some additional classes inheriting from equipmentVisitor would be here  equipmentVisited* visited; equipmentVisitor* visitor;  // Here you initialise visited and visitor in any convenient way  visited->accept(visitor); 
like image 204
Trista N Avatar asked Apr 12 '12 00:04

Trista N


People also ask

How does the visitor pattern work?

The Visitor pattern represents an operation to be performed on the elements of an object structure without changing the classes on which it operates. This pattern can be observed in the operation of a taxi company. When a person calls a taxi company (accepting a visitor), the company dispatches a cab to the customer.

Where is visitor pattern used?

The visitor pattern is used to specify behaviour for different types which is however cohesive. And when you need to add a feature (via Open-Closed principle), you just may add a new Visitor class.

Why is visitor design pattern used?

Visitor pattern is used when we have to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects to another class. For example, think of a Shopping cart where we can add different type of items (Elements).

What is visitor pattern in C++?

Visitor in C++ Visitor is a behavioral design pattern that allows adding new behaviors to existing class hierarchy without altering any existing code.


1 Answers

Visitor pattern is used to implement double dispatch. In plain words it means that the code that gets executed depends on runtime types of two objects.

When you call a regular virtual function, it is a single dispatch: the piece of code that gets executed depends on the runtime type of a single object, namely, the one the virtual method of which you are calling.

With the visitor pattern, the method that is being called ultimately depends on the type of two objects - the type of the object implementing the equipmentVisitor, and the type of the object on which you call accept (i.e. the equipmentVisited subclass).

There are other ways to implement double dispatch in C++. Item 31 of Scott Meyer's "More Effective C++" treats this subject in depth.

like image 110
Sergey Kalinichenko Avatar answered Oct 05 '22 18:10

Sergey Kalinichenko