Visitor
Definition
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
UML class diagram
Participants
- Visitor
- declares a Visit operation for each class of ConcreteElement in the object structure. The operation's name and signature identifies the class that sends the Visit request to the visitor. That lets the visitor determine the concrete class of the element being visited. Then the visitor can access the elements directly through its particular interface
- ConcreteVisitor
- implements each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class or object in the structure. ConcreteVisitor provides the context for the algorithm and stores its local state. This state often accumulates results during the traversal of the structure.
- Element
- defines an Accept operation that takes a visitor as an argument.
- ConcreteElement
- implements an Accept operation that takes a visitor as an argument
- ObjectStructure
- can enumerate its elements
- may provide a high-level interface to allow the visitor to visit its elements
- may either be a composite pattern or a collection such as a list or a set
Sample code in Java
package com.hong.visitor; import java.util.ArrayList; abstract class Element { private String name; public Element(String name) { this.name = name; } public String getName() { return this.name; } public abstract void setVisitor(Visitor visitor); } class ConcreteElementA extends Element{ public ConcreteElementA(String name){ super(name); } @Override public void setVisitor(Visitor visitor) { visitor.visitElement(this); } } class ConcreteElementB extends Element{ public ConcreteElementB(String name){ super(name); } @Override public void setVisitor(Visitor visitor) { visitor.visitElement(this); } } interface Visitor { public void visitElement(Element element); } class ConcreteVisitorA implements Visitor{ @Override public void visitElement(Element element) { extraAbility(element); System.out.println("The element has been visited by VisitorA"); } public void extraAbility(Element element) { System.out.println("Adds additional ability to element " + element.getName()); } } class ConcreteVisitorB implements Visitor { @Override public void visitElement(Element element) { extraAbility(element); System.out.println("The element has been visited by VisitorB"); } public void extraAbility(Element element) { System.out.println("Adds additional ability to element " + element.getName()); } } class ObjectStructure { private ArrayList<Element> elements = new ArrayList<Element>(); public void addElement(Element element) { elements.add(element); } public void removeElement(Element element) { elements.remove(element); } public void accept(Visitor visitor){ for(Element element : elements) { element.setVisitor(visitor); } } } public class Visitor_structure { public static void main(String[] args) { Element element1 = new ConcreteElementA("ElementA1"); Element element2 = new ConcreteElementB("ElementB1"); Element element3 = new ConcreteElementA("ElementA2"); Element element4 = new ConcreteElementB("ElementB2"); Visitor visitorA = new ConcreteVisitorA(); Visitor visitorB = new ConcreteVisitorB(); ObjectStructure structure = new ObjectStructure(); structure.addElement(element1); structure.addElement(element2); structure.addElement(element3); structure.addElement(element4); structure.accept(visitorA); structure.accept(visitorB); } }