“This is the 20th day of my participation in the August Gwen Challenge.
Welcome friends to search “Andy Hui” on wechat to pay attention to a wave!
Write some thoughts of the programmer, hope it will help you.
Design Pattern visitor (QQ.com)
Introduction to the Visitor Pattern
The visitor pattern isolates the algorithm from the object it works on.
The visitor pattern suggests putting the new behavior into a separate class to be given to a visitor, rather than trying to integrate it into an existing class.
It allows you to add new behavior to an existing class hierarchy without modifying existing code.
The original object to perform the operation on is passed as a parameter to the method in the visitor, giving the method access to any necessary data the object contains.
To understand briefly what a visitor is, use the example of an insurance agent visiting a different level of customer. He would recommend different policies for different clients.
- Medical insurance is recommended for ordinary migrant workers.
- Ordinary workers, recommended medical insurance and property insurance.
- Entrepreneur, recommend financial insurance, annuity, etc.
Visitor pattern structure
- Visitor interface
Declares a series of visitor methods that take specific elements of the object structure as parameters.
- Specific visitor
Several versions of the same behavior are implemented for different concrete element classes.
- The element interface
Declare a method to receive visitors. The method must have one parameter declared as the visitor interface type.
- Specific elements
Implements an element interface method whose purpose is to redirect its call to the corresponding visitor’s method based on the current element class (note: even if the base class implements this method, all subclasses must override it and call the appropriate method in the visitor object).
- The client
As a representation of collections or other complex objects.
Suitable for application Scenarios
- When you need to perform some operations on all the elements of a complex object structure.
- When the visitor pattern is used to clean up the business logic for auxiliary behavior.
- When a behavior makes sense only at the class level, but not in other classes.
implementation
Declare a set of “access” methods in the visitor interface, one for each concrete element class in the program.
2. Declare the element interface and implement the receive method in the concrete primitive class.
Element classes can only interact with visitors through the visitor interface (visitors must know all the specific element classes).
Create a concrete visitor and implement all visitor methods for each behavior that cannot be implemented in the element hierarchy.
5. The client must create the visitor object and pass it to the element through the “receive” method.
advantages
1, meet the open and closed principle and single responsibility principle.
2. Visitor objects can collect useful information as they interact with various objects.
disadvantages
1. Update all visitors every time you add or remove a class from the element hierarchy.
When visitors interact with an element, they may not have the necessary permissions to access the element’s private member variables and methods.
Demo
Element interface and element interface implementation
Visitor interface and interface implementation
Client code with a Main() call
The visitor pattern is rarely used in regular coding life, and we just need to understand how it works. In the actual development process, if there are businesses that meet this pattern, they can be decomposed and constructed according to the structure of the visitor pattern.
Small remarks
Life is short, I don’t want to go after what I can’t see, I just want to catch what I can see.
Original is not easy, give a attention.
I am Hui, thank you for reading, if it is helpful to you, please like, forwarding thank you.