The Visitor visitor mode defines the operation of each object in a target group. It allows you to define new operations that act on these objects without changing these object itself.
In Java, Visitor mode is actually an element that separates the Collection structure and operates these elements.
Why use Visitor? Java's Collection (including Vector and Hashtable) is our most frequently used technology, but the Collection seems to be a black large dyeing cylinder. Objects have been made in a variety of clear types of features, which is removed, these types It will disappear. Then we must 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"); else system.out.println (O . Tostring ());} In the above example, we used instanceof to determine the type of O.
Obviously, the shortcomings of this shortcomings if else IF is very cumbersome. We can use the Visitor mode to resolve it.
How to use Visitor? For the above example, define the interface called Visitable to define an accept operation, that is, let Collection each element has accessibility.
The visitor is the element Element of our Collection, we have to define an interface that can be accessed (accessing and accessing is interactive), only visitors, if you are not welcome, visitors can not access ), Named Visitable, can also be named ELEMENT.
Public interface visitable {public void accept (visitor visitor);}
The specific elements visited inherit this new interface Visitable:
public class StringElement implements Visitable {private String value; public StringElement (String string) {value = string;} public String getValue () {return value;} // accept specific content defined herein is a simple call to public void accept ( Visitor Visitor) {visitor.visitstring (this);}}
The above is the visitor is a string type, and then build a float type:
public class FloatElement implements Visitable {private Float value; public FloatElement (Float value) {this.value = value;} public Float getValue () {return value;} // accept specific content defined herein is a simple call public void Accept (Visitor Visitor) {visitor.visitfloat (this);}}
We design an interface Visitor visitors. In this interface, there are some access operations, which are possible to access object collection Collection, which is now assumed to have three behaviors: Access the string type in the object collection; Access the Float type in the object collection; access the object collection type in the object collection. Note that the last type is a collection nested, which can be seen from this nested implementation. Interface Visitor visitors are as follows:
Public interface visitor {public void visitor; public void visitfloat; public void visitcollection;}
Visitors' implementation:
public class ConcreteVisitor implements Visitor {// In this method, we have achieved success in the Collection of the elements of access public void visitCollection (Collection collection) {Iterator iterator = collection.iterator () while (iterator.hasNext ()) {Object o = iTerator.next (); if (visitable) ((visitable) o) .accept (this);}} public void visitstring (StringeElement stringe) {system.out.println ("'" stringe.getValue () "'");} Public void visitfloat (floateLEMENT FLOATE) {system.out.println (FLOATE.GETVALUE (). Tostring () "f");}}
On the top of VisitCollection we implemented access to the Collection, only one judgment statement is used, as long as it can be accessed.
StringeElement is just an implementation, which can expand to more implementations, the entire core mystery in the Accept method, when traversing the Collection, call the specific type of visited by the corresponding Accept method. This step determines the type of visitor,
If StringeElement, StringeElement calls the visitor's VisiteString method, which implements a behavioral 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 ("i am a string2"); list .add (new floatlement); list .add (New StringeElement); Visitor.visitCollection (List); A variety of data types in the List object collection in the client code are placed, and the access in the object collection does not have to be judged one by one, but use instance of one by one, but through the visitor mode cleverly implementation. .
At this point, we have completed the basic structure of the Visitor mode.
Using the Visitor mode Using the Accessor Mode is a small change in the object type in the object group structure.
In two interface Visitor and Visitable, make sure that Visitable rarely changes, that is, make sure that the old Element element type is added, can vary, visitors or operation, that is, Visitor's different subclasses can have Many, so that the visitor mode is most convenient.
If the object collections in the object collection are often changed, but not only the Visitor is changed, and Visistable also adds the corresponding behavior. The GOF suggestion is not as directed in these object classes, and the accesser design mode is not required.
However, in Java, Java's REFLECT technology solves this problem, thus combining the reflective mechanism, allowing the visitor mode to apply a wider range.
Reflect Technology is a technique for dynamically acquiring object types and methods during operation, and implements reference JavaWorld's original text.