Visitor mode Overview
Indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the classes of each element.
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. Visitor allows you to define related operations in a class. When this object structure is shared by many applications, the visitor mode is used to allow each application to only include the required operations. 3. Classes defining the object structure are rarely changed, but new operations are often needed in this structure. Changing the object structure class requires redefining the interfaces for all visitors, which may be costly. If the object structure class changes frequently, it may be better to define these operations in these classes.
Participants
1. Visitor declares a visit operation for each class of concreteelement in the object structure. The operation name and features identify the class that sends a visit request to the visitor. This allows the visitor to determine the specific class of the element being accessed. In this way, visitors can directly access the element through a specific interface of the element. 2. concretevisitor implements each operation declared by the visitor. Each operation implements a part of the algorithm, and the algorithm segment corresponds to the class of the object in the structure. Concretevisitor provides context for the algorithm and stores its local state. This state often accumulates results while traversing the structure. 3. element defines an accept operation, which takes a visitor as the parameter. 4. concreteelement implements the accept operation, which takes a visitor as the parameter. 5. objectstructure can enumerate its elements. A high-level interface is provided to allow the visitor to access its elements. It can be a combination or a set, such as a list or an unordered set.
Example
Visitor public interface Visitor { public void visitString(StringElement stringE); public void visitFloat(FloatElement floatE); public void visitCollection(Collection collection); }ConcreteVisitor public class ConcreteVisitor implements Visitor { public void visitCollection(Collection collection) { // TODO Auto-generated method stub Iterator iterator = collection.iterator(); while (iterator.hasNext()) { Object o = iterator.next(); if (o instanceof Visitable) { ((Visitable)o).accept(this); } } } public void visitFloat(FloatElement floatE) { System.out.println(floatE.getFe()); } public void visitString(StringElement stringE) { System.out.println(stringE.getSe()); }}Element public interface Visitable { public void accept(Visitor visitor);}ConcreteElement public class FloatElement implements Visitable { private Float fe; public FloatElement(Float fe) { this.fe = fe; } public Float getFe() { return this.fe; } public void accept(Visitor visitor) { visitor.visitFloat(this); }}public class StringElement implements Visitable { private String se; public StringElement(String se) { this.se = se; } public String getSe() { return this.se; } public void accept(Visitor visitor) { visitor.visitString(this); }}Test public class Test { public static void main(String[] args) { Visitor visitor = new ConcreteVisitor(); StringElement se = new StringElement("abc"); se.accept(visitor); FloatElement fe = new FloatElement(new Float(1.5)); fe.accept(visitor); System.out.println("==========="); List result = new ArrayList(); result.add(new StringElement("abc")); result.add(new StringElement("abc")); result.add(new StringElement("abc")); result.add(new FloatElement(new Float(1.5))); result.add(new FloatElement(new Float(1.5))); result.add(new FloatElement(new Float(1.5))); visitor.visitCollection(result); }}result abc1.5===========abcabcabc1.51.51.5