JAVA design pattern 14-Visitor (Visitor pattern)

Source: Internet
Author: User

The Visitor Pattern is one of the 23 design patterns proposed by GoF and belongs to the behavior Pattern.
According to the big talk design model, it is the most complicated and hard to understand model.
Definition (derived from GoF Design Pattern): represents an operation that acts on each element in an object structure. It allows you
Define new operations for these elements without changing the element classes. It can be seen from the definition that the structure object is necessary to use the visitor Mode
Condition, and the structure object must have a method to traverse its own objects. This is similar to the collection concept in Java.
Involved roles:
1. IVisitor abstracts the visitor role and declares an access operation interface for the specific element role in the object structure. The name and
The parameter identifies an element role that sends an access request to a visitor, so that the visitor can directly access it through a specific interface of the element role.
2. ConcreteVisitor. The Visitor role implements the interface declared by the Visitor.
3. Element defines an accept () that uses a Visitor as a parameter.
4. The specific elements of ConcreteElement implement the acceptance operation interface defined by the abstract Element.
5. ObjectStructure object roles, which are essential for using the visitor mode. It has the following features:
It can enumerate its elements; it can provide a high-level interface to allow visitors to access its elements; if necessary, it can be designed as
A composite object or an aggregation (such as a list or unordered set ).
The visitor mode has the following features:
The visitor mode decouples the data structure and the operation acting on the structure, allowing the operation set to evolve relatively freely.
The visitor mode is suitable for systems with relatively stable data structures and easy-to-change algorithms. The visitor mode makes it easy to increase algorithm operations.
If the system data structure objects are easy to change and new data objects are often added, the visitor mode is not suitable.
The advantage of the visitor mode is that it is easy to add operations, because adding operations means adding new visitors. Visitor Mode
To a visitor object, its changes do not affect the system data structure. The disadvantage is that it is very difficult to add new data structures.
Applicability:
1) An object structure contains many class objects with different interfaces. You want to perform operations on these objects dependent on their specific classes.
2) You need to perform many different and unrelated operations on the objects in an object structure, and you want to avoid causing these operations to "pollute" the classes of these objects.
The Visitor mode allows you to set related operations in a class.
3) when the object structure is shared by many applications, use the Visitor mode to allow each application to only include the operations needed.
4) classes that define the object structure are rarely changed, but new operations are often needed in this structure. To change the object structure class, you need to redefine all accesses.
This may be costly. If the object structure class changes frequently, it may be better to define these operations in these classes.
Example:
[Java]
Package design. visitor;
 
/**
* File name: Body. java
* Created by: Fei Wong
* Created at: Jun 22,201 2
* Email: www.2cto.com
**/
 
Public class Body {
Public void accept (IVisitor visitor ){
Visitor. visit (this );
}
}
 
 
 
 
Package design. visitor;
 
/**
* File name: Engine. java
* Created by: Fei Wong
* Created at: Jun 22,201 2
* Email: www.2cto.com
**/
 
Public class Engine {
Public void accept (IVisitor visitor ){
Visitor. visit (this );
}
}
 
 
Package design. visitor;
 
/**
* File name: Wheel. java
* Created by: Fei Wong
* Created at: Jun 22,201 2
* Email: www.2cto.com
**/
 
Public class Wheel {
Private String name;
Public Wheel (String name ){
This. name = name;
}
String getName (){
Return this. name;
}
Public void accept (IVisitor visitor ){
Visitor. visit (this );
}
}
 
 
 
Package design. visitor;
 
/**
* File name: Car. java
* Created by: Fei Wong
* Created at: Jun 22,201 2
* Email: www.2cto.com
**/
 
Public class Car {
Private Engine engine = new Engine ();
Private Body body Body = new Body ();
Private Wheel [] wheels
= {New Wheel ("front left"), new Wheel ("front right "),
New Wheel ("back left"), new Wheel ("back right ")};
Public void accept (IVisitor visitor ){
Visitor. visit (this );
Engine. accept (visitor );
Body. accept (visitor );
For (int I = 0; I <wheels. length; ++ I)
Wheels [I]. accept (visitor );
}
}
 
 
 
 
Package design. visitor;
 
/**
* File name: Visitor. java
* Created by: Fei Wong
* Created at: Jun 22,201 2
* Email: www.2cto.com
**/
 
Public interface IVisitor {
Void visit (Wheel wheel );
Void visit (Engine engine );
Void visit (Body body );
Void visit (Car car );
}
 
 
 
 
Package design. visitor;
 
Public class PrintVisitor implements IVisitor {
 
@ Override
Public void visit (Wheel wheel ){
System. out. println ("Visiting" + wheel. getName () + "wheel ");
 
}
 
@ Override
Public void visit (Engine engine ){
System. out. println ("Visiting engine ");
}
 
@ Override
Public void visit (Body body ){
System. out. println ("Visiting body ");
}
 
@ Override
Public void visit (Car car ){
System. out. println ("Visiting car ");
}
 
}
Author: hfmbook

Related Article

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.