Visitor mode in Design Mode
The visitor mode involves the following roles:
1. Abstract Visitor role: declares one or more method operations to form interfaces that must be implemented by all specific Visitor roles.
2. A specific visitor (ConcreteVisitor) Role: Implements abstract access on the declared interface, that is, each access operation declared by the abstract visitor.
3. Abstract Node role: declares an acceptance operation and accepts a parameter taken by a visitor object.
4. The ConcreteNode role allows the abstract node to accept the operation.
5. ObjectStructure: You have the following responsibilities to traverse all elements in the structure. If necessary,
A high-level interface is provided to allow visitors to access each element. It can be designed as a composite object or aggregation, List or set.
Visitor: Abstraction Layer
Public interface Visitor {
/**
* Access Operations corresponding to NodeA
*/
Public void visit (NodeA node );
/**
* Access Operations corresponding to NodeB
*/
Public void visit (NodeB node );
}
Visitors:
Public class VisitorA implements Visitor {
/**
* Access Operations corresponding to NodeA
*/
@ Override
Public void visit (NodeA node ){
System. out. println (node. operationA ());
}
/**
* Access Operations corresponding to NodeB
*/
@ Override
Public void visit (NodeB node ){
System. out. println (node. operationB ());
}
}
Public class VisitorB implements Visitor {
/**
* Access Operations corresponding to NodeA
*/
@ Override
Public void visit (NodeA node ){
System. out. println (node. operationA ());
}
/**
* Access Operations corresponding to NodeB
*/
@ Override
Public void visit (NodeB node ){
System. out. println (node. operationB ());
}
}
Abstract node class:
Public abstract class Node {
/**
* Accept the operation
*/
Public abstract void accept (Visitor visitor );
}
Specific nodes:
Public class NodeA extends Node {
/**
* Accept the operation
*/
@ Override
Public void accept (Visitor visitor ){
Visitor. visit (this );
}
/**
* NodeA-specific methods
*/
Public String operationA (){
Return "NodeA ";
}
}
Public class NodeB extends Node {
/**
* Acceptance Method
*/
@ Override
Public void accept (Visitor visitor ){
Visitor. visit (this );
}
/**
* NodeB-specific methods
*/
Public String operationB (){
Return "NodeB ";
}
}
Structure object role: This structure object role holds an aggregation and provides the add () method to the outside world for managing aggregation. By calling this method, you can dynamically Add a new node.
Public class ObjectStructure {
Private List Nodes = new ArrayList ();
/**
* Perform method operations
*/
Public void action (Visitor visitor ){
For (Node node: nodes)
{
Node. accept (visitor );
}
}
/**
* Add a new element.
*/
Public void add (Node node ){
Nodes. add (node );
}
}
Client type
Public class Client {
Public static void main (String [] args ){
// Create a structure object
ObjectStructure OS = new ObjectStructure ();
// Add a node to the structure
OS. add (new NodeA ());
// Add a node to the structure
OS. add (new NodeB ());
// Create a visitor
Visitor visitor = new VisitorA ();
OS. action (visitor );
}
}