Visitor Pattern (Behavioral)

Intent

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.

Structure

Abstract Class: Visitor

class Visitor
{
        public:
                virtual void VisitElementA(ElementA*);
                virtual void VisitElementB(ElementB*);

        protected:
                Visitor();
};

Abstract Class: Element

class Element
{
        public:
                virtual ~Element();
                virtual void Accept(Visitor&) = 0;

        protected:
                Element();
};

Concrete Class: ElementA

class ElementA : public Element
{
        public:
                ElementA();
                virtual void Accept(Visitor& v) {v.VisitElementA(this);}
};

Concrete Class: ElementB

class ElementB : public Element
{
        public:
                ElementB();
                virtual void Accept(Visitor& v) {v.VisitElementB(this);}
};

Concrete Class: CompositeElement
class CompositeElement : public Element
{
        public:
                virtual void Accept(Visitor&);

        private:
                List<Element*>* _children;
};

void CompositeElement :: Accept(Visitor& v)
{
        ListIterator<Element*> i(_children);

        for(i.First(); !i.IsDone(); i.Next())
        {
                i.CurrentItem()->Accept(v);
        }

      v.VisitCompositeElement(this);
}

Participants

o     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 element directly through its particular interface.
o    ConcreteVisitor
- implements each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class of 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 sturcture.
o     Element
-  defines an Accept operation that takes a visitor as an argument.
o     ConcreteElement
- implements an Accept operation that takes a visitor as an argument.
o      ObjectStructure
- can enumerate its elements.
- may provide a high-level interface to allow the visitor to visit its elements.
- may either be a composite (Composite Pattern) or a collection such as a list or a set.

Collaborations

o     A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the  visitor.

o     When an element is visited, it calls the Visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state, if necessary.

Example

vs.

Applicability

Use Visitor pattern when
 

o   an object structure contains many classes of objects with differeing interfaces, and you want to perform operations on these objects that depend on their concrete class.
o   many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid "polluting"their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them.
o   the classes defining the object structure rarely change, but you often want to define new opertions over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it's probably better to define the operations in those classes.
 

Consequences

o     Visitor makes adding new operations easy.
o     A Visitor gathers related operations and separates unrelated ones.

o     Adding new ConcreteElement classes is hard.

o     Visiting across class hierarchies. (Iterator vs. Visitor)
o     Accumulating state.

o     Breaking encapsulation.