Learn about Visitor Pattern (Behavioral Pattern)

Tram Ho

Because I recently read a few articles about Visitor Pattern and realized that each person’s perspective on this pattern is different. Therefore, I have re-referenced from many pages and also specializing in the production of articles about Design Pattern which is Refactoring Guru , summarizing and rewriting in a concise way that is more accessible and closely related to the goal of Visitor Pattern. No more trouble in the matter 😂


Allow changing, extending operations for objects without changing the structure and content inside the object

In order to do this, the Elements must separate those operations into their own methods and define them on separate classes called Visitor classes. Thus, the operations are separated from the object structure, making the operation changes become flexible.

For each new operation for the created object, a corresponding visitor class is also created.

In addition, this is also a technique to help us recover the lost data type of the argument passed. Because it implements the corresponding method based on the data type of both the calling object and the input argument ( Double Dispatch ).

1. What is Double Dispatch and Single Dispatch?

  • Single Dispatch : The method name called depends only on the data type of the object that calls it

  • Double Dispatch : The method name is called based on the data type of the object calling it and the data type of the input object. This is the same technology that Visitor Pattern uses, so it is also called Double Dispatch

2. Advantages

  • Allow one or more behaviors to be applied to a set of objects at run-time, separating behaviors from object structures
  • Ensure Open / Close principle: The original object has not been changed, easily adding new behaviors to the object through the visitor

3. When to use Visitor Pattern?

  • When there is a complex object structure with multiple classes and interfaces. Users need to perform some specific behaviors of their own objects, depending on their concrete class
  • We want to move the behavioral logic from objects to another class for processing to reduce complexity
  • When the data structure of the object rarely changes but their behavior is changed frequently
  • When you want to avoid using instanceof operator

4. Structure

Participants in Visitor Pattern:

  • Element : Interface declares the skeleton for data processing objects. Especially must declare accept() to receive the input operations
  • ConcreteElement : Data processing object deployed from Element
  • Visitor : The interface declares the skeleton for visitors to support the definition and to include alternative operations in ConcreteElement
  • ConcreteVisitor : The support class invokes alternative operations on ConcreteElement implemented from the Visitor

5. For example

Suppose we have the following problem: You are a ladykiller, you want to confess to a girl but do not know what her nationality is, we simply cannot say “I love you” to A Japanese girl, because she won’t understand anything, so we’ll say “Aishite imasu” instead. Therefore, we will write a common function to say our love is saylove() and pass on the love according to the nationality of each woman.

The problem reappears when you want to change, for example, when we want to add another sayGoodBye() , we have to add the inferface Lady and implement it for all the implemented classes. also add risk. So now it’s time to use Visitor Partten.

  • First we revise the Lady interface and redeploy JapanLady and AmericaLady only with accept() to reduce processing complexity and bring that processing to ConcreteVisitor

  • Declaring the Visitor interface creates the skeleton and implements SayLoveVisitor to print out the message to the ladies (because Javascript does not support polymorphism but does not inherit, so it should temporarily use instanceof instead)

  • Test

  • For example, later we get bored and want to SayGoodBye lady to flirt with another lady. We will create another ConcreteVisitor for this function

  • Let’s try it

6. Conclusion

When we want to extend the operation of the object handling ConcreteElement, we only update on Visitor without modifying ConcreteElement. This satisfies the Open / Close rule code.

The biggest drawback of the Visitor Pattern is that it does not support Element expansion, because extending the Element will result in an update of the entire Visitor’s interface and class. But we can fix this with various tweaks to the Pattern plus a bit of dexterity in data structure editing and data processing.




Share the news now

Source : Viblo