Design Pattern visitor

Source: Internet
Author: User
Design Pattern visitor

Banqiao Renren http://www.jdon.com

Click here to attend monthly design model lectures

Visitor mode definition
Operations on objects in an object group. This allows you to define new operations on these objects without changing the objects themselves.

In Java, the visitor mode separates elements in the collection structure from the operations on these elements.

Why use visitor?
Java collections (including vector and hashtable) are the most frequently used technologies. However, collection seems to be a large black dye cylinder. Once an object with various distinctive features is put in, then, these types disappear. therefore, we must use if to judge, for example:

Iterator = collection. iterator ()
While (iterator. hasnext ()){
Object o = iterator. Next ();
If (O instanceof collection)
Messyprintcollection (Collection) O );
Else if (O instanceof string)
System. Out. println ("'" + O. tostring () + "'");
Else if (O instanceof float)
System. Out. println (O. tostring () + "F ");
Else
System. Out. println (O. tostring ());
}
In the above example, we use instanceof to determine the O type.

Obviously, the disadvantage of doing soCodeIf else if is cumbersome, we can use the visitor mode to solve it.

How to Use visitor?
For the above example, the defined interface is called visitable, which is used to define an accept operation, that is, to make each element of the collection accessible.

Visitors are each element of our collection. We need to define an interface for these elements that can be accessed (access and access are interactive, only visitors, if the visitor says it is not welcome, the visitor will not be able to access it). The name can also be named visitable or element.

Public interface visitable
{
Public void accept (visitor );
}

The accessed element inherits the new interface visitable:

Public class stringelement implements visitable
{
Private string value;
Public stringelement (string ){
Value = string;
}

Public String getvalue (){
Return value;
}

// Define the accept content. Here is a simple call.
Public void accept (visitor ){
Visitor. visitstring (this );
}
}

The above is the string type of the visitor, and a float type is created below:

Public class floatelement implements visitable
{
Private float value;
Public floatelement (float value ){
This. value = value;
}

Public float getvalue (){
Return value;
}

// Define the accept content. Here is a simple call.
Public void accept (visitor ){
Visitor. visitfloat (this );
}
}

We designed an interface for visitor. There are some access operations in this interface. These access operations are all possible classes in the special access object collection. Currently, we assume there are three actions: string type in the access object set; float type in the access object set; object set type in the access object set. Note that the last type is set nesting. This nested implementation shows An advantage of using the access mode.

The visitor to the interface visitor is as follows:

Public interface visitor
{

Public void visitstring (stringelement stringe );
Public void visitfloat (floatelement floate );
Public void visitcollection (Collection collection );

}

Visitor implementation:

Public class concretevisitor implements visitor
{
// In this method, we have successfully accessed the elements of the collection.
Public void visitcollection (Collection collection ){
Iterator = collection. iterator ()
While (iterator. hasnext ()){
Object o = iterator. Next ();
If (O instanceof visitable)
(Visitable) O). Accept (this );
}
}

Public void visitstring (stringelement stringe ){
System. Out. println ("'" + stringe. getvalue () + "'");
}
Public void visitfloat (floatelement floate ){
System. Out. println (floate. getvalue (). tostring () + "F ");
}

}

In the preceding visitcollection, we implemented access to each element of the collection. We only used one judgment statement to determine whether it can be accessed.

Stringelement is only an implementation that can be extended to more implementations. The entire core mystery is that in the accept method, when traversing the collection, the corresponding accept method is used to call specific types of visitors. In this step, the types of visitors are determined,

If it is stringelement, and stringelement calls back the visitor's visitestring method, this step implements the behavior operation method.

Client code:

Visitor visitor = new concretevisitor ();

Stringelement stringe = new stringelement ("I Am a string ");
Visitor. visitstring (stringe );

Collection list = new arraylist ();
List. Add (New stringelement ("I Am a string1 "));
List. Add (New stringelement ("I Am a string2 "));
List. Add (New floatelement (new float (12 )));
List. Add (New stringelement ("I Am a string3 "));
Visitor. visitcollection (list );

There are multiple data types in the list object set in client code. The access to the object set does not have to be determined one by one using the instance of as in the beginning, but is cleverly implemented through the visitor mode.

So far, we have completed the basic structure of the visitor mode.

Prerequisites for using the visitor Mode
The visitor mode is that the object type in the object group structure (Collection) rarely changes.

In the visitor and visitable interfaces, make sure that the visitable changes little. That is to say, make sure that the new element type cannot be added, and that the visitor behavior or operation can be changed, that is, different sub-classes of visitor can have multiple types, which makes it easier to use the visitor mode.

If the object set in the object set changes frequently, not only does the visitor implementation need to change, but the visistable also needs to increase the corresponding behavior. gof recommends that you define operations one by one in these object classes, the visitor design mode is not required.

However, in Java, the reflect Technology of Java solves this problem. Therefore, combined with the reflect reflection mechanism, the visitor mode can be applied to a wider range.

Reflect is a technique used to dynamically obtain object types and methods during running. For more information, see the original javaworld document.

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.