Visitor mode in Design Mode

Source: Internet
Author: User

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 );
}


}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.