Visitor of behavior mode

Source: Internet
Author: User

Visitor Definition



Operations on objects in an object group.
It allows you to define new operations on these objects without changing the objects themselves.

In Java, the visitor mode is actually
Is to separate the elements in the collection structure and operate on these elements.

Why use visitor?




Java Collection (including vector and
Hashtable) is the most frequently used technology, but collection seems to be a black big dyeing cylinder. Once an object with various distinctive types of features is put into it and then retrieved, these classes
The Type disappears, so we must use the if statement to determine the value, 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 used
Instanceof to determine the O type.

Obviously, the disadvantage code for doing so is if else
If it is cumbersome, we can use the visitor mode to solve it.

How to Use visitor?




For the above example, we designed an interface for visitor Visitor:

Public
Interface visitor
{


  
Public void visitcollection (Collection
Collection );


  
Public void visitstring (string );


  
Public void
Visitfloat (float );
}

In this interface, we think
The possible types of classes in collection are put in.

With the visitor, we need to be visited.
Each element of the collection is an interface that can be accessed.
If the visitor says they are not welcome, the visitor will not be able to access ),

We define this interface as visitable,
Defines an accept operation, that is, to make each element of the collection accessible.

Public
Interface visitable
{


  
Public void accept (visitor
Visitor );
}

Well, with two interfaces, we need to define their
Specific implementation (concrete class ):

Public
Class concreteelement implements visitable
{


  

Private string value;


  
Public concreteelement (string ){


  


  

Value = string;


  
}


  
//

Definition
Accept

Specific content


Here is a simple call


  
Public void accept (visitor
Visitor ){


  


  

Visitor. visitstring (this );


  
}
}

Let's look at the visitor's concrete 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 (string ){
     
System. Out. println ("'" + String + "'");
}

  
Public void visitfloat (float ){
     
System. Out. println (float. tostring () + "F ");
}
}

 

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

So far, we have completed the basic visitor Mode
Architecture.

Use the visitor Mode
Prerequisites




Collection in the object group structure)
The object type in is rarely changed, that is, the visitor's identity type is rarely changed. For example, the type in the visitor in the above is rarely changed. If you need to add new operations, for example, in the previous example, we
In addition to the specific implementation of concreteelement, a new concreteelement2 concreteelement3.

It can be seen that the visitor mode is a prerequisite,
In the visitor and visitable interfaces, make sure that the visitor rarely changes and the change is visitable, which is the most convenient way to use the visitor.

If the visitor changes frequently,
That is to say, the object type in the object group often changes. It is recommended that you define operations one by one in these object classes, but the reflect Technology of Java solves this problem.

Reflect technology dynamically acquires object types and
For more information, see javaworld's
Original ENGLISH

.

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.