In the design pattern, there is a pattern that provides a visitor class that can change the execution algorithm of our target object class, which decouples the data structure from the data operation.
Introduction to visitor mode
- Visitor patterns are behavioral patterns
- Visitor pattern provides a visitor class that can change the execution algorithm of the target at will
Advantages and disadvantages of visitor mode
Advantages of visitor mode
- The visitor pattern conforms to the single responsibility principle of one of the design pattern principles
- Visitor mode has high scalability and high flexibility
Disadvantages of visitor mode
- Because the specific goal of the visit provides specific details for the visitor class, which violates the Dimitri principle and the dependency inversion principle. For novices, Xiaobai wants to learn java promotion, Java architecture, web development, big data, data analysis, artificial intelligence and other technologies more easily. Here we share the system teaching resources and expand my Wei (Tongying): CGMX9880 [tutorial / tools / methods / troubleshooting]
- It is not easy to modify the specific objectives
Structure of visitor pattern
Visitor patterns are mainly divided into five roles: Abstract visitor, concrete visitor, abstract target, concrete target and object structure. In terms of structure, visitor patterns are relatively complex compared with other design patterns.
Abstract Visitor: defines a concrete interface for accessing elements.
Concrete visitor: all concrete visitors must implement Abstract visitors and access operations.
Abstract target: declares an interface to receive operations.
Concrete target: it realizes the abstract goal and some business-related logic.
Object structure: a container containing specific targets.
Java implementation visitor pattern
Define target object
- Target
public interface Hello { void say(Visitor visitor);}
- ConcreteTarget...
public class ChineseHello implements Hello { @Override public void say(Visitor visitor) { visitor.visit(this); } } public class EnglishHello implements Hello { @Override public void say(Visitor visitor) { visitor.visit(this); } }
- ObjectStructure
public class StructureHello implements Hello { Hello[] hello; { hello = new Hello[]{new EnglishHello(), new ChineseHello()}; } @Override public void say(Visitor visitor) { Arrays.stream(hello).forEach(h -> h.say(visitor)); visitor.visit(this); } }
Define visitor object
- Abstract visitor
public interface Visitor { void visit(EnglishHello hello); void visit(ChineseHello hello); void visit(StructureHello hello); }
- Concrete visitor
public class ConcreteVisitor implements Visitor { @Override public void visit(EnglishHello hello) { System.err.println("Hello world!"); } @Override public void visit(ChineseHello hello) { System.err.println("Hello world!"); } @Override public void visit(StructureHello hello) { System.err.println("I am an object container hello"); } }
Define test class run results
- Test class
public class Test { public static void main(String[] args) { Hello hello = new StructureHello(); hello.say(new ConcreteVisitor()); } }
- Operation results
In the above example, our target object accepts a parameter as the visitor, and the visitor accepts a parameter as the target. There is no other business written in our target, but simply hand over the logic to the visitor for processing. Among them, the StructureHello class object is the role of object structure container. Thus, we have implemented a simple visitor pattern.
Visitor mode is applicable to many different operations in the object, but we don't want all these operations to be written together, and we don't want to modify the original target object when adding or modifying.