Visitor mode
In the Visitor Pattern, we use a visitor class, which changes the execution algorithm of the element class. In this way, the element's execution algorithm can change as the visitor changes. This type of design pattern is a behavioral pattern. According to the schema, the element object has accepted a visitor object so that the visitor object can handle operations on the element object.
Introduction
Intent: Mainly separate the data structure and data operations.
Mainly solves: Stable data structure and volatile operation coupling issues.
When to use: You need to perform many different and unrelated operations on the objects in an object structure, and you need to avoid letting these operations "pollute" the classes of these objects. Use access The Orator pattern encapsulates these into classes.
How to solve: Add an interface in the visited class that provides an external interface for receiving visitors.
Key code: There is a method in the data base class to accept visitors and pass its own reference into the visitor.
Application example: You are a guest at a friend's house, you are a visitor, the friend accepts your visit, you pass the friend's description, and then make a judgment on the friend's description, this is Visitor mode.
Advantages: 1. Comply with the single responsibility principle. 2. Excellent scalability. 3. Flexibility.
Disadvantages: 1. Specific elements disclose details to visitors, which violates the Dimit Principle. 2. It is difficult to change specific elements. 3. It violates the principle of dependency inversion and relies on concrete classes instead of abstractions.
Usage scenarios: 1. The class corresponding to the object in the object structure rarely changes, but it is often necessary to define new operations on this object structure. 2. Many different and unrelated operations need to be performed on the objects in an object structure, and you need to avoid letting these operations "pollute" the classes of these objects, and you do not want to modify these classes when adding new operations.
Notes: Visitors can unify functions and make reports, UI, interceptors and filters.
Implementation
We will create a ComputerPart interface that defines the accepted operations. Keyboard, Mouse, Monitor and Computer are entity classes that implement the ComputerPart interface. We will define another interface ComputerPartVisitor, which defines the operations of the visitor class. Computer Use entity visitors to perform corresponding actions.
VisitorPatternDemo, our demo class uses the Computer, ComputerPartVisitor classes to demonstrate the usage of the visitor pattern.
Step 1
Define an interface that represents the element.
ComputerPart.java
public interface class ComputerPart { public void accept(ComputerPartVisitor computerPartVisitor); }
Step 2
Create an entity class that extends the above class.
Keyboard.java
public class Keyboard implements ComputerPart { @Override public void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); } }
Monitor.java
public class Monitor implements ComputerPart { @Override public void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); } }
Mouse.java
public class Mouse implements ComputerPart { @Override public void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); } }
Computer.java
public class Computer implements ComputerPart { ComputerPart[] parts; public Computer(){ parts = new ComputerPart[] {new Mouse(), new Keyboard(), new Monitor()}; } @Override public void accept(ComputerPartVisitor computerPartVisitor) { for (int i = 0; i < parts.length; i++) { parts[i].accept(computerPartVisitor); } computerPartVisitor.visit(this); } }
Step 3
Define an interface that represents the visitor.
ComputerPartVisitor.java
public interface ComputerPartVisitor { public void visit(Computer computer); public void visit(Mouse mouse); public void visit(Keyboard keyboard); public void visit(Monitor monitor); }
Step 4
Create an entity visitor that implements the above class.
ComputerPartDisplayVisitor.java
public class ComputerPartDisplayVisitor implements ComputerPartVisitor { @Override public void visit(Computer computer) { System.out.println("Displaying Computer."); } @Override public void visit(Mouse mouse) { System.out.println("Displaying Mouse."); } @Override public void visit(Keyboard keyboard) { System.out.println("Displaying Keyboard."); } @Override public void visit(Monitor monitor) { System.out.println("Displaying Monitor."); } }
Step 5
Use ComputerPartDisplayVisitor to display the components of Computer .
VisitorPatternDemo.java
public class VisitorPatternDemo { public static void main(String[] args) { ComputerPart computer = new Computer(); computer.accept(new ComputerPartDisplayVisitor()); } }
Step 6
Verify the output.
Displaying Mouse. Displaying Keyboard. Displaying Monitor. Displaying Computer.