Visitor
Represent an operation to be performed on the elements of an object structure. Visit or lets you define a new operation without changing the classes of the elements on which it operates.
Last updated
Represent an operation to be performed on the elements of an object structure. Visit or lets you define a new operation without changing the classes of the elements on which it operates.
Last updated
Visitor is a design pattern belonging to the Behavior Pattern group
Visitor is also known as Double dispatch
Allows defining operations on a set of objects of heterogeneous type without changing the class definition of those objects.
Allows algorithms to be separated from the objects they operate on.
Helps restore lost data types (instead of using instanceof).
Imagine that your team develops an app which works with geographic information structured as one colossal graph. Each node of the graph may represent a complex entity such as a city, but also more granular things like industries, sightseeing areas, etc. The nodes are connected with others if there’s a road between the real objects that they represent. Under the hood, each node type is represented by its own class, while each specific node is an object.
At some point, you got a task to implement exporting the graph into XML format. At first, the job seemed pretty straightforward. You planned to add an export method to each node class and then leverage recursion to go over each node of the graph, executing the export method. The solution was simple and elegant: thanks to polymorphism, you weren’t coupling the code which called the export method to concrete classes of nodes.
Unfortunately, the system architect refused to allow you to alter existing node classes. He said that the code was already in production and he didn’t want to risk breaking it because of a potential bug in your changes.
Besides, he questioned whether it makes sense to have the XML export code within the node classes. The primary job of these classes was to work with geodata. The XML export behavior would look alien there.
There was another reason for the refusal. It was highly likely that after this feature was implemented, someone from the marketing department would ask you to provide the ability to export into a different format, or request some other weird stuff. This would force you to change those precious and fragile classes again.
The Visitor pattern suggests that you place the new behavior into a separate class called visitor, instead of trying to integrate it into existing classes. The original object that had to perform the behavior is now passed to one of the visitor’s methods as an argument, providing the method access to all necessary data contained within the object.
Now, what if that behavior can be executed over objects of different classes? For example, in our case with XML export, the actual implementation will probably be a little bit different across various node classes. Thus, the visitor class may define not one, but a set of methods, each of which could take arguments of different types, like this:
But how exactly would we call these methods, especially when dealing with the whole graph? These methods have different signatures, so we can’t use polymorphism. To pick a proper visitor method that’s able to process a given object, we’d need to check its class. Doesn’t this sound like a nightmare?
You might ask, why don’t we use method overloading? That’s when you give all methods the same name, even if they support different sets of parameters. Unfortunately, even assuming that our programming language supports it at all (as Java and C# do), it won’t help us. Since the exact class of a node object is unknown in advance, the overloading mechanism won’t be able to determine the correct method to execute. It’ll default to the method that takes an object of the base Node
class.
However, the Visitor pattern addresses this problem. It uses a technique called Double Dispatch, which helps to execute the proper method on an object without cumbersome conditionals. Instead of letting the client select a proper version of the method to call, how about we delegate this choice to objects we’re passing to the visitor as an argument? Since the objects know their own classes, they’ll be able to pick a proper method on the visitor less awkwardly. They “accept” a visitor and tell it what visiting method should be executed.
I confess. We had to change the node classes after all. But at least the change is trivial and it lets us add further behaviors without altering the code once again.
Now, if we extract a common interface for all visitors, all existing nodes can work with any visitor you introduce into the app. If you find yourself introducing a new behavior related to nodes, all you have to do is implement a new visitor class.
Imagine a seasoned insurance agent who’s eager to get new customers. He can visit every building in a neighborhood, trying to sell insurance to everyone he meets. Depending on the type of organization that occupies the building, he can offer specialized insurance policies:
If it’s a residential building, he sells medical insurance.
If it’s a bank, he sells theft insurance.
If it’s a coffee shop, he sells fire and flood insurance.
The Visitor interface declares a set of visiter methods that can take specific elements of the object structure as arguments. These methods can have the same name if the program is written in a language that supports overloading, but their parameter types must be different.
Each Concrete Visitor implements several versions of the same behaviors, tailored to different specific element classes.
The Element interface declares a method to "accept" visitors. This method must have one parameter declared with type visitor interface.
Each Concrete Element must implement an accept method. The purpose of this method is to redirect the call to the appropriate visitor method corresponding to the current element class. Be aware that even if a base element class implements this method, all subclasses must still override this method in their own classes and call the appropriate method on the visitor object.
The client usually represents a collection or some other complex object (for example, a Composite tree). Typically, clients do not know all the concrete element classes because they work with objects from that collection through some abstract interface.
Advantage
Open/Closed Principle: can introduce a new behavior that can work with objects of different classes without changing these classes.
Single Responsibility Principle: can pass multiple instances of the same behavior into the same class.
A visitor object can accumulate some useful information when working with many different audiences. This can be helpful when we want to traverse some complex object structure, such as an object tree, and apply visitors to each object of this structure.
Disadvantage
It is necessary to update all visitors each time a class is added or removed from the element hierarchy.
Visitors may lack necessary access to the private fields and methods of the elements they must work with.
Visitor is used when:
Use when it is necessary to perform operations on all elements of a complex object structure.
Use to clean up the business logic of backend behaviors.
Use when a behavior only makes sense in some classes of the class hierarchy, but not in other classes.
Use the Visitor when you need to perform an operation on all elements of a complex object structure (for example, an object tree).
The Visitor pattern lets you execute an operation over a set of objects with different classes by having a visitor object implement several variants of the same operation, which correspond to all target classes.
Use the Visitor to clean up the business logic of auxiliary behaviors.
The pattern lets you make the primary classes of your app more focused on their main jobs by extracting all other behaviors into a set of visitor classes.
Use the pattern when a behavior makes sense only in some classes of a class hierarchy, but not in others.
You can extract this behavior into a separate visitor class and implement only those visiting methods that accept objects of relevant classes, leaving the rest empty.
Declare the visitor interface with a set of “visiting” methods, one per each concrete element class that exists in the program.
Declare the element interface. If you’re working with an existing element class hierarchy, add the abstract “acceptance” method to the base class of the hierarchy. This method should accept a visitor object as an argument.
Implement the acceptance methods in all concrete element classes. These methods must simply redirect the call to a visiting method on the incoming visitor object which matches the class of the current element.
The element classes should only work with visitors via the visitor interface. Visitors, however, must be aware of all concrete element classes, referenced as parameter types of the visiting methods.
For each behavior that can’t be implemented inside the element hierarchy, create a new concrete visitor class and implement all of the visiting methods.
You might encounter a situation where the visitor will need access to some private members of the element class. In this case, you can either make these fields or methods public, violating the element’s encapsulation or nest the visitor class in the element class. The latter is only possible if you’re lucky to work with a programming language that supports nested classes.
The client must create visitor objects and pass them into elements via “acceptance” methods.