1. Visitor mode definition:
Indicates an operation that acts on each element in an object structure. It allows you to define new operations acting on these elements without modifying the element classes, that is, to dynamically add new methods.
Ii. Structure of the Visitor mode:
Iii. Main roles in Visitor mode:
1) Visitor role (Visitor): provides an access operation interface for specific elements in the object structure (ObjectStructure. The operation interface name and parameters identify the specific element role to be accessed. In this way, the visitor can directly access the element through a specific interface of the element role.
2) specific visitor role (ConcreteVisitor): Implements operations on the Vistor interface.
3) Element: this interface defines an accept operation to accept specific visitors.
4) A specific Element role (ConcreteElement): implements the accept operation on the Element.
5) object structure Role (ObjectStructure): A required role in the visitor mode. It must have the following features: it can enumerate its elements; it can provide a high-level interface to allow the visitor to access its elements; it can be a combination (combination mode) or a set, such as a list or an unordered set.
Iv. Advantages and disadvantages of the Visitor mode:
1) Advantage: You can add new operations without modifying specific element classes. The accept method of the element class is used to accept a visitor object.
2) Disadvantages: it is not easy to add element classes frequently. If no element class is added, you must write a method for this element in the Visitor interface and modify the subclass of Visitor.