This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

In the Visitor Pattern, a Visitor class is used that changes the execution algorithm of the element class. In this way, the element’s execution algorithm can change as visitors change. This allows the visitor object to handle operations on the element object.

This type of design pattern is behavioral.

introduce

describe parsing
intentions The main separation of data structures from data operations.
Mainly to solve Stable data structures and volatile operational coupling problems.
When to use You need to do many different, unrelated operations on objects in an object structure, and you need to avoid having those operations “contaminate” the classes of those objects by encapsulating them in the visitor pattern.
How to solve Add an interface for receiving visitors to the class being visited.
The key code In the data base class there is a method that accepts the visitor, passing a reference to itself to the visitor.
Examples of application You go to a friend’s house, you are a visitor, your friend receives your interview, you make a judgment about the friend’s description, this is the visitor model.
advantages 1. Conform to the principle of single responsibility. 2. Excellent scalability. 3. Flexibility.
disadvantages 1. Specific elements disclose details to visitors, violating the Demeter principle. 2. It is difficult to change specific elements. 3. It violates the principle of dependency inversion by relying on concrete classes instead of abstraction.
Usage scenarios 1. The corresponding class of an object in an object structure rarely changes, but it is often necessary to define new operations on this object structure. 2. You need to do many different and unrelated operations on objects in an object structure, and you want to avoid “contaminating” the classes of those objects with these operations, and you don’t want to modify those classes when you add new operations.
Matters needing attention Visitors can unify features such as reports, UI, interceptors, and filters.

implementation

The example implements the scenario of accessing various modules of the computer. The following classes are needed:

1. ComputerPart interface;

2, the interface realizes Keyboard, Mouse, Monitor and Computer;

ComputerPartVisitor interface class, which defines the operations of the visitor class.

4, ComputerPartDisplayVisitor visitors to implement interface classes;

VisitorPatternDemo Scenario class.

Step 1- Define the element interface class

Define an interface that represents an element.

// ComputerPart.java
public interface ComputerPart {
   public void accept(ComputerPartVisitor computerPartVisitor);
}
Copy the code

Step 2- Implement the element interface class

Create an entity class that extends the above classes.

// Keyboard.java public class Keyboard implements ComputerPart { @Override public void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); }}Copy the code
// Monitor.java public class Monitor implements ComputerPart { @Override public void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); }}Copy the code
// Mouse.java public class Mouse implements ComputerPart { @Override public void accept(ComputerPartVisitor computerPartVisitor) { computerPartVisitor.visit(this); }}Copy the code
// 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); }}Copy the code

Step 3- Define the visitor interface class

Define an interface that represents a 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);
}
Copy the code

Step 4- Implement the visitor interface class

Create an entity visitor that implements the above classes.

// 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."); }}Copy the code

Step 5- Create the scene class

Use ComputerPartDisplayVisitor to show part of the Computer.

// VisitorPatternDemo.java public class VisitorPatternDemo { public static void main(String[] args) { ComputerPart computer = new Computer(); computer.accept(new ComputerPartDisplayVisitor()); }}Copy the code

Step 6- Output print

Execute the program and output the result:

Displaying Mouse.
Displaying Keyboard.
Displaying Monitor.
Displaying Computer.
Copy the code