Visitor Pattern

             Yateen L. Nikharge
         Aravindh Manickavasagam
          Ider Zheng



                            S
The VISITOR Family


The Visitor family allows new methods to be added
  to existing hierarchies without modifying the
  hierarchies.
The Patterns in this family are:
S VISITOR

S ACYCLIC VISITOR

S DECORATOR

S EXTENSION OBJECT
Intent


S Represent an operation to be performed on the
  elements of an object structure.
S Visitor lets you define a new operation without
  changing the classes of the elements on which it
  operates.
Motivation


S Decouple the data structure and algorithms.

S Allow addition of new algorithms without changing the data
   structure.

S Example: Modem Configuration
  S To configure a modem for Unix, create an instance of the visitor
     and pass it to accept.
   S The appropriate derivative call visit(this)
   S New OS configuration can be added by adding a new
     derivative of visitor.
Motivation (contd…)
Motivation(contd…)
Applicability

Use the pattern when-
S An object structure contains many classes of
  objects with differing interfaces…
S Many distinct and unrelated operations need to
  be performed on objects in an object structure…
S The classes defining the object structure rarely
  change, but you often want to define new
  operations over the structure…
Structure
Participants

S Visitor (ModemVisitor):
       Declares a Visit operation or each class…

S ConcreteVisitor (ConfigureUnixVisitor):
       Implements each Visit…

S Element (Modem) :
       Defines an Accept operation…

S ConcreteElement(ZoomModem):
       Implements Accept …
Participants(Contd…)


S ObjectStructure (Program):
     May provide a high-level interface to allow the visitor to visit its
     elements .
Collaborations

S A client using the Visitor pattern must create a ConcreteVisitor
  object and traverse the object structure, visiting each element
  with the visitor.



S 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.
Collaborations cont.
Consequences-Benefits


S Adding new operations

S Gathers related operations and separates unrelated ones

S Visitors can visit objects that don’t have a common parent
  class

S Visitors can accumulate state as they visit each element
Consequences - Liabilities


S Adding new ConcreteElement classes is hard



S Allowing a Visitor to access the internal state of a
  Concrete Element breaks encapsulation
Implementation - Issues


S Double Dispatch

S Who is Responsible for Traversing the Object Structure?
  S Object Structure


  S   Visitor – whenever operations depend on other operations
      on the object structure

  S   Iterator Object
Sample Codes
Implementation



Function Pointer
Implementation



Global Function
Implementation



Static Methods
   Functor
Implementation


        Graphic Project
From CSE 687 Objected Oriented Design
Graphic

         “v1”           “e5”         “v3”
  “e1”
                “e2”
                                 “e9”
“v4”                   “v7”
                                 “e7”
           “e4”
                                        “v5”
         “v2”            “e3”

                 “e8”                “e6”
                              “v6”
Known Uses


S Smalltalk-80 Compiler

S IRIS Inventor Toolkit

S X Consortium’s Fresco Application Toolkit

S Bistro Programming Language Compiler
Related Patterns


S Composite

S Interpreter
References

S Design Patterns: Elements of Reusable Object-Oriented
  Software

  By Erich Gamma, Richard Helm, Ralph
  Johnson, and John Vlissides

Shttp://www.codeproject.com/KB/aspnet/SoftArch3.aspx

Visitor Pattern

  • 1.
    Visitor Pattern Yateen L. Nikharge Aravindh Manickavasagam Ider Zheng S
  • 2.
    The VISITOR Family TheVisitor family allows new methods to be added to existing hierarchies without modifying the hierarchies. The Patterns in this family are: S VISITOR S ACYCLIC VISITOR S DECORATOR S EXTENSION OBJECT
  • 3.
    Intent S Represent anoperation to be performed on the elements of an object structure. S Visitor lets you define a new operation without changing the classes of the elements on which it operates.
  • 4.
    Motivation S Decouple thedata structure and algorithms. S Allow addition of new algorithms without changing the data structure. S Example: Modem Configuration S To configure a modem for Unix, create an instance of the visitor and pass it to accept. S The appropriate derivative call visit(this) S New OS configuration can be added by adding a new derivative of visitor.
  • 5.
  • 6.
  • 7.
    Applicability Use the patternwhen- S An object structure contains many classes of objects with differing interfaces… S Many distinct and unrelated operations need to be performed on objects in an object structure… S The classes defining the object structure rarely change, but you often want to define new operations over the structure…
  • 8.
  • 9.
    Participants S Visitor (ModemVisitor): Declares a Visit operation or each class… S ConcreteVisitor (ConfigureUnixVisitor): Implements each Visit… S Element (Modem) : Defines an Accept operation… S ConcreteElement(ZoomModem): Implements Accept …
  • 10.
    Participants(Contd…) S ObjectStructure (Program): May provide a high-level interface to allow the visitor to visit its elements .
  • 11.
    Collaborations S A clientusing the Visitor pattern must create a ConcreteVisitor object and traverse the object structure, visiting each element with the visitor. S 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.
  • 12.
  • 13.
    Consequences-Benefits S Adding newoperations S Gathers related operations and separates unrelated ones S Visitors can visit objects that don’t have a common parent class S Visitors can accumulate state as they visit each element
  • 14.
    Consequences - Liabilities SAdding new ConcreteElement classes is hard S Allowing a Visitor to access the internal state of a Concrete Element breaks encapsulation
  • 15.
    Implementation - Issues SDouble Dispatch S Who is Responsible for Traversing the Object Structure? S Object Structure S Visitor – whenever operations depend on other operations on the object structure S Iterator Object
  • 16.
  • 17.
  • 19.
  • 20.
  • 21.
    Implementation Graphic Project From CSE 687 Objected Oriented Design
  • 22.
    Graphic “v1” “e5” “v3” “e1” “e2” “e9” “v4” “v7” “e7” “e4” “v5” “v2” “e3” “e8” “e6” “v6”
  • 23.
    Known Uses S Smalltalk-80Compiler S IRIS Inventor Toolkit S X Consortium’s Fresco Application Toolkit S Bistro Programming Language Compiler
  • 24.
  • 25.
    References S Design Patterns:Elements of Reusable Object-Oriented Software By Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Shttp://www.codeproject.com/KB/aspnet/SoftArch3.aspx