In the visitor pattern, we use a visitor class, which changes the execution algorithm of the element class. In this way, the execution algorithm of the element can change as the visitor changes. This type of design pattern belongs to behavioral pattern. According to the schema, the element object has accepted the visitor object so that the visitor object can handle operations on the element object.
The visitor pattern contains the following main roles.
- Abstract visitor role: define an interface for accessing concrete elements. Each concrete element class corresponds to an access operation visit(). The parameter type in the operation identifies the accessed concrete elements.
- Specific visitor role: implement each access operation declared in the abstract visitor role and determine what visitors should do when accessing an element.
- Abstract element role: declare an interface containing the accept operation accept(), and the accepted visitor object is used as the parameter of the accept() method.
- Concrete element role: implement the accept() operation provided by the abstract element role, and its method body is usually visitor Visit (this). In addition, the specific elements may also contain relevant operations of their own business logic.
- Object structure role: it is a container containing element roles and provides methods for visitor objects to traverse all elements in the container. It is usually implemented by aggregate classes such as List, Set and Map.
Implementation of visitor mode:
1. Create visitor interface
public interface Visitor { void visit(Element element); }
2. Create specific visitors
public class AVisitor implements Visitor { @Override public void visit(Element element) { System.out.println(this.getClass().getName() + "Visited" + element.getClass().getName()); } }
public class BVisitor implements Visitor { @Override public void visit(Element element) { System.out.println(this.getClass().getName() + "Visited" + element.getClass().getName()); } }
3. Create element interface
public interface Element { void accept(Visitor visitor); }
4. Create specific elements
public class AElement implements Element { @Override public void accept(Visitor visitor) { visitor.visit(this); } }
public class BElement implements Element { @Override public void accept(Visitor visitor) { visitor.visit(this); } }
5. Create object structure class
public class Structure { private List<Element> elements = new ArrayList<>(); public void add(Element element){ elements.add(element); } public void remove(Element element){ elements.remove(element); } public void accept(Visitor visitor){ for(Element element:elements){ visitor.visit(element); } } }
6. Visitor mode test
public class VisitorMain { public static void main(String[] args) { Structure structure = new Structure(); structure.add(new AElement()); structure.add(new BElement()); Visitor visitor_A = new AVisitor(); structure.accept(visitor_A); Visitor visitor_B = new BVisitor(); structure.accept(visitor_B); } }
- What is a design pattern: [one java design pattern per day (zero)] - basic concept of design pattern and seven design principles
Create pattern
- Factory mode: [one java design pattern per day (1)] - Factory Pattern
- Abstract factory pattern: [one java design pattern per day (2)] - abstract factory pattern
- Singleton mode: [one java design pattern per day (3)] - singleton pattern
- Prototype mode: [one java design pattern per day (4)] - prototype pattern
- Builder mode: [one java design pattern per day (5)] - builder pattern
Structural model
- Adapter mode: [one java design pattern per day (6)] - adapter pattern
- Proxy mode: [one java design pattern per day (7)] - proxy pattern
- Bridging mode: [one java design pattern per day (8)] - bridging mode
- Decorator mode: [one java design pattern per day (9)] - Decorator Pattern
- Appearance mode: [one java design pattern per day (10)] - appearance pattern
- Meta mode: [one java design pattern per day (11)] - enjoy meta pattern
- Combination mode: [one java design pattern per day (12)] - composite pattern
Behavioral model
- Template mode: [one java design pattern per day (13)] - template pattern
- Interpreter mode: [one java design pattern per day (14)] - interpreter mode
- Command mode: [one java design pattern per day (15)] - command mode
- Policy mode: [one java design pattern per day (16)] - policy pattern
- Status mode: [one java design pattern per day (17)] - status mode
- Observer mode: [one java design pattern per day (18)] - observer pattern
- Responsibility chain mode: [one java design pattern per day (19)] - responsibility chain pattern
- Memo mode: [one java design pattern per day (20)] - memo pattern
- Mediator mode: [one java design pattern per day (21)] - mediator pattern
- Iterator mode: [one java design pattern per day (22)] - iterator pattern