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.
Consider a compiler that represents programs as abstract syntax trees. It will need to perform operations on abstract syntax trees for "static semantic" analyses like checking that all variables are defined. It will also need to generate code. So it might define operations for type-checking, code optimization, flow analysis, checking for variables being assigned values before they're used, and so on. Moreover, we could use the abstract syntax trees for pretty-printing, program restructuring, code instrumentation, and computing various metrics of a program.
Most of these operations will need to treat nodes that represent assignment statements differently from nodes that represent variables or arithmetic expressions. Hence there will be one class for assignment statements, another for variable accesses, another for arithmetic expressions, and so on. The set of node classes depends on the language being compiled, of course, but it doesn't change much for a given language.
This diagram shows part of the Node class hierarchy. The problem here is that distributing all these operations across the various node classes leads to a system that's hard to understand, maintain, and change. It will be confusing to have type-checking code mixed with pretty-printing code or flow analysis code. Moreover, adding a new operation usually requires recompiling all of these classes. It would be better if each new operation could be added separately, and the node classes were independent of the operations that apply to them.
We can have both by packaging related operations from each class in a separate object, called a visitor, and passing it to elements of the abstract syntax tree as it's traversed. When an element "accepts" the visitor, it sends a request to the visitor that encodes the element's class. It also includes the element as an argument. The visitor will then execute the operation for that element-the operation that used to be in the class of the element.
For example, a compiler that didn't use visitors might type-check a procedure by calling the TypeCheck operation on its abstract syntax tree. Each of the nodes would implement TypeCheck by calling TypeCheck on its components (see the preceding class diagram). If the compiler type-checked a procedure using visitors, then it would create a TypeCheckingVisitor object and call the Accept operation on the abstract syntax tree with that object as an argument. Each of the nodes would implement Accept by calling back on the visitor: an assignment node calls VisitAssignment operation on the visitor, while a variable reference calls VisitVariableReference. What used to be the TypeCheck operation in class AssignmentNode is now the VisitAssignment operation on TypeCheckingVisitor.
To make visitors work for more than just type-checking, we need an abstract parent class NodeVisitor for all visitors of an abstract syntax tree. NodeVisitor must declare an operation for each node class. An application that needs to compute program metrics will define new subclasses of NodeVisitor and will no longer need to add application-specific code to the node classes. The Visitor pattern encapsulates the operations for each compilation phase in a Visitor associated with that phase.
With the Visitor pattern, you define two class hierarchies: one for the elements being operated on (the Node hierarchy) and one for the visitors that define operations on the elements (the NodeVisitor hierarchy). You create a new operation by adding a new subclass to the visitor class hierarchy. As long as the grammar that the compiler accepts doesn't change (that is, we don't have to add new Node subclasses), we can add new functionality simply by defining new NodeVisitor subclasses.
Use the Visitor pattern when:
A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor.
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.
The following interaction diagram illustrates the collaborations between an object structure, a visitor, and two elements:
So the key consideration in applying the Visitor pattern is whether you are mostly likely to change the algorithm applied over an object structure or the classes of objects that make up the structure. The Visitor class hierarchy can be difficult to maintain when new ConcreteElement classes are added frequently. In such cases, it's probably easier just to define operations on the classes that make up the structure. If the Element class hierarchy is stable, but you are continually adding operations or changing algorithms, then the Visitor pattern will help you manage the changes.
template <class Item> class Iterator { // ... Item CurrentItem() const; };This implies that all elements the iterator can visit have a common parent class Item. Visitor does not have this restriction. It can visit objects that don't have a common parent class. You can add any type of object to a Visitor interface. For example, in
class Visitor { public: // ... void VisitMyType(MyType*); void VisitYourType(YourType*); };MyType and YourType do not have to be related through inheritance at all.
Each object structure will have an associated Visitor class. This abstract visitor class declares a VisitConcreteElement operation for each class of ConcreteElement defining the object structure. Each Visit operation on the Visitor declares its argument to be a particular ConcreteElement, allowing the Visitor to access the interface of the ConcreteElement directly. ConcreteVisitor classes override each Visit operation to implement visitor-specific behavior for the corresponding ConcreteElement class.
class Visitor { public: virtual void VisitElementA(ElementA*); virtual void VisitElementB(ElementB*); // and so on for other concrete elements protected: Visitor(); };
Each class of ConcreteElement implements an Accept operation that calls the matching Visit... operation on the visitor for that ConcreteElement. Thus the operation that ends up getting called depends on both the class of the element and the class of the visitor
The concrete elements are declared as:
class Element { public: virtual ~Element(); virtual void Accept(Visitor&) = 0; protected: Element(); }; class ElementA : public Element { public: ElementA(); virtual void Accept(Visitor& v) { v.VisitElementA(this); } }; class ElementB : public Element { public: ElementB(); virtual void Accept(Visitor& v) { v.VisitElementB(this); } };
A class CompositeElement can implement the following accept method:
class CompositeElement : public Element { public: virtual void Accept(Visitor&); private: List* _children; }; void CompositeElement::Accept (Visitor& v) { ListIteratori(_children); for(i.First(); !i.IsDone(); i.Next()) { i.CurrentItem()->Accept(v); } v.VisitCompositeElement(this); }
1. For the following simple rules is required:
Expression:: = Expression '+' Term | Term Term:: = Term '*' Factor | Factor Factor:: = '(' Expression ')' | IntegerConstant
- to shape each terminal and nonterminal in a Java class;
- to implement methods to accept these classes node;
- to write a class that displays the AST visitor's Polish shaped infix;
- to write a class that displays the AST visitor's Polish prefixed shape;
- to write a class that evaluates the AST visitor's;
- to build an expression tree example on which to execute the three visitors;
Assignments time: 1 week.
https://www.digitalocean.com/community/tutorials/visitor-design-pattern-java https://www.cs.unc.edu/~stotts/GOF/hires/pat5kfso.htm