Java and mode Learning Series-Visitor Mode

Source: Internet
Author: User

 

I, Structure of visitor (visitor) Mode

The visitor mode aims to encapsulate operations that are applied to certain data structure elements. The visitor mode is suitable for systems with relatively undetermined data structures. It frees the coupling between the data structure and the operations acting on the structure, allowing the operation set to evolve relatively freely.

The method selected based on the object type is called dispatch ). The object to which a method belongs is called the method receiver. the receiver of the method and the parameters of the method are collectively referred to as the parcel of the method. Based on how many types of assignment can be used, object-oriented languages can be divided into single-assignment languages and multi-assignment languages. A single dispatch language selects a method based on the type of a single parcel. Multiple dispatch languages select a method based on the type of more than one parcel. C ++ and Java are both single-assignment languages, and C ++ and Java are dynamic single-assignment languages, because the Dynamic assignment of these two languages only takes into account the type of method receiver, meanwhile, it is also a static multi-distribution language, because the distribution of these two languages for overloaded methods takes into account the receiver type of the method and the type of all the parameters of the method.

 

The structure is as follows:

The Code is as follows:

// Visitor Interface

Public interface
Visitor {

Void visit (nodea node );

Void visit (NodeB node );

}

// Visitora class

Public class
Visitora implements visitor {

Public void visit (nodea ){

System. Out. println (nodea. opertiona );

}

Public void visit (NodeB ){

System. Out. println (NodeB. operationb ());

}

}

// Node abstract class

Abstract Public
Class node {

Public abstract void accept (visitor
Visitor );

}

// Nodea class

Public class
Nodea extends node {

Public void accept (visitor ){

Visitor. Visit (this );

}

Public String operationa (){

Return "nodea is
Visited ";

}

}

// NodeB class

Public class
NodeB extends node {

Public void accept (visitor ){

Visitor. Visit (this );

}

Public String operationb (){

Return "NodeB is
Visited ";

}

}

// Objectstructure class

Public class
Objectstructure {

Private vector nodes;

Private node;

Public objectstructure (){

Nodes = new vector ();

}

Public void
Action (visitor ){

For (enumeration E =
Nodes. Elements (); E. hasmoreelements ();){

Node =
(Node) E. nextelement ();

Node. Accept (visitor );

}

}

Public void add (node ){

Nodes. addelement (node );

}

}

II, Advantages and disadvantages of visitor Mode

Advantages

(1) The visitor mode makes it easy to add new operations. If some operations depend on a complex structure object, it is generally complicated to add new operations. In the visitor mode, adding new operations means adding a new visitor class. Therefore, it becomes easy.

(2) The visitor mode aggregates relevant behaviors into a visitor object, rather than being dispersed into node classes.

(3) The visitor mode can access member classes of different hierarchies over the hierarchical structure of several classes. An iterator can only access Member objects of the same type of hierarchy, but cannot access objects of different levels.

(4) accumulation status. Each individual visitor object has a concentrated set of related behaviors, so that the status of the executed operations can be accumulated within itself during the access process, rather than distributed to many node objects.

Disadvantages

(1) It is very difficult to add new node classes. Adding a new node means adding a new abstract operation to the abstract visitor role and adding corresponding operations to each specific visitor class.

(2) Damage encapsulation. The visitor mode requires the visitor object to access and call operations on each node object, which implies a requirement on all node objects: they must expose some of their own operations and internal states. Otherwise, the visitor's access becomes meaningless. Because the visitor object accumulates the States required for access operations, the States are no longer stored in the Node object, which also destroys encapsulation.

Applicability

1. The visitor mode should only be used when the accessed class structure is very stable. In other words, the system rarely needs to add new nodes. If a new node needs to be added, an access operation corresponding to the new node must be added to each access object, which is a large-scale modification to a system, this violates the "open-close" principle.

III, Relationship between Visitor mode and other modes

Iteration Mode

An iterator can only access Member objects of the same type of hierarchy, but cannot access objects of different levels. The visitor mode can be

Merging Mode

The visitor mode often browses some structured objects that conform to the merging mode.

 

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.