Visitor definition: An operation that acts on individual objects in an object group. It allows you to define new actions that act on these objects without changing the objects themselves.
In Java, the visitor pattern actually separates the elements in the collection structure from the behavior of manipulating those elements.
Why Use visitor mode
Java's collection (including vectors and Hashtable) are the techniques we use most often, but collection seems to be a black big dyeing tank, and when objects of various distinct types have been put in, they disappear when they are removed. Then we are bound to use if to judge, such as:
Iterator 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"); ElseSystem. out. println (O.tostring ());}
In the example above, we used the instanceof to determine the type of O.
Obviously, the disadvantage of this code if else if is cumbersome, we can use the visitor mode to solve it.
How to use visitor mode
For the above example, we designed an interface visitor visitor:
Public Interface public void publicvoidpublic void float);}
In this interface, we put the types of classes that we think collection possible.
With the visitor, we need to be the visitor, the visitor is each element of our collection elements, we want to define an acceptable interface for these element (access and access is interactive, only the visitor, if the visitor is not welcome, visitors can not access )。
We define this interface called Visitable, which is used to define an accept operation, which means that each element of collection is accessible.
Public Interface publicvoid Accept (Visitor Visitor);}
Well, with two interfaces, we're going to define their specific implementations (concrete Class):
Public class Implements privatepublic=// define the specific contents of the accept here is a very simple sentence called publicvoid Accept (Visitor Visitor) {visitor.visitstring ( This ); }}
Then look at the concrete implementation of the visitor:
Public classConcretevisitorImplementsvisitor{//In this method, we implement a successful access to the elements of the collection Public voidvisitcollection (Collection Collection) {Iterator Iterator=Collection.iterator () while(Iterator.hasnext ()) {Object o=Iterator.next (); if(OinstanceofVisitable) ((Visitable) O). Accept ( This); } Public voidvisitstring (String string) {System.out.println ("'" +string+ "'"); } Public voidVisitfloat (Floatfloat) {System.out.println (float. toString () + "F"); }}
In the above visitcollection we implement access to each element of the collection, using only a single judgment statement, as long as it is possible to determine whether it can be accessed.
At this point, we have completed the basic architecture of visitor mode.
Prerequisites for using the visitor model
The object type in the object group structure (Collection) rarely changes, that is, the visitor's identity type is rarely changed, such as the type of visitor in the above changes rarely, if you need to add new operations, such as in the above example we in the concreteelement concrete implementation, A new ConcreteElement2 ConcreteElement3 is also needed.
Visible use of the visitor mode is a prerequisite, in two interfaces visitor and visitable, to ensure that visitor very little change, the change is visitable, so the use of visitor most convenient.
If the visitor also changes frequently, that is, the object types in the object group often change, the general recommendation is to define the actions individually in these object classes, but Java's reflect technology solves this problem.
Java access mode (visitor mode)