Visitor

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.

Motivation

Applicability

Appliquer le motif Visitor lorsque :

  • an object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes.
  • many distinct and unrelated operations need to be performed on objects in an objec 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.
  • The classes defining the object structure rearely change, but you often want to define new operations 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.

Participants

  • Visitor (NodeVisitor)
  • ConcreteVisitor (TypeCheckingVisitor)
  • Element (Node)
  • ConcreteElement (AssignmentNode, VariableRefNode)
    • implements an Accept operation that takes a visitor as an argument.
  • ObjectStructure (Program)
    • can enumerate its elements
    • may provide a high-level interface to allow the visitor to visit its elements
    • may either be a composite (see Composite) or a collection such as a list or set.

 
m2ilc/dp_visitor.txt · Dernière modification: 2010/10/08 14:59 par suitable