May 27, 2021 Design mode
In Visitor Pattern, we use a visitor class that changes the execution algorithm of the element class. I n this way, the element's execution algorithm can change as the visitor changes. T his type of design pattern is behavioral. Depending on the pattern, the element object accepts the visitor object so that the visitor object can handle operations on the element object.
Intent: Primarily separates the data structure from the data operation.
Main solution: stable data structure and variable operation coupling problems.
When to use: There are many different and unrelated operations on objects in an object structure, and you need to avoid "polluting" the classes of those objects by encapsulating them in visitor mode.
How to solve: Add an interface to the accessed class that provides an external reception visitor.
Key code: Inside the data base class, there is a way to accept visitors and pass on their own references to visitors.
Examples of apps: You're a guest at a friend's house, you're a visitor, a friend accepts your visit, you make a judgment about a friend's description, and that's the visitor pattern.
Pros: 1, in line with the principle of single responsibility. 2 , excellent extensivity. 3 , flexibility.
Cons: 1, specific elements to the visitor to disclose details, in violation of the Dimit principle. 2 , the specific element change is more difficult. 3 , in violation of the principle of dependency inverted, relying on specific classes, not relying on abstraction.
Use scenes: 1, the classes corresponding to objects in an object structure rarely change, but it is often necessary to define new operations on this object structure. 2. There are many different and irrelevant operations that need to be performed on 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.
Note: Visitors can unify features, reports, UIs, interceptors, and filters.
We'll create a ComputerPart interface that defines the accepted operation. ard, Mouse, Monitor, and Computer are entity classes that implement the ComputerPart interface. W e'll define another interface, ComputerPartVisitor, that defines the actions of the visitor class. Computer uses entity visitors to perform the appropriate actions.
VisitorPatternDemo, our demo class uses the Computer, ComputerPartVisitor class to demonstrate the use of visitor patterns.
Define an interface that represents an element.
ComputerPart.java
public interface ComputerPart { public void accept(ComputerPartVisitor computerPartVisitor); }
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); } }
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); }
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."); } }
Use ComputerPartDisplayVisitor to display the components of the Computer.
VisitorPatternDemo.java
public class VisitorPatternDemo { public static void main(String[] args) { ComputerPart computer = new Computer(); computer.accept(new ComputerPartDisplayVisitor()); } }
Verify the output.
Displaying Mouse. Displaying Keyboard. Displaying Monitor. Displaying Computer.