Intent
Define a new operation to deal with the classes of the elements without changing their structures.
Where to use & benefits
- Add operations on a bunch of classes which have different interfaces.
- Traverse the object structure to gather related operations
- Easy to add new operations.
- Crossing class hierarchies may break encapsulation.
Structure

Participants
Visitor (IShapeVisitor)
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 (ShapeVisitor)
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 (IVisitable)
defines an Accept operation that takes a visitor as an argument.
ConcreteElement (RectangleShape, CircleShape, TriangleShape)
implements an Accept operation that takes a visitor as an argument.
ObjectStructure (ColorDecorator)
may provide a high-level interface to allow the visitor to visit its elements
Example
In the demonstration application, the shape (Rectangle, Trinagle, Circle) objects implements the IVisitable interface to allows the visitor to visit the object and execute the visitor method. The ColorDecorator uses the ShapeVisitor class to perform the color decoration on the shape object by visiting ShapeVisitor object .
Class Diagram
