Visitor Pattern

Download Visitor Pattern

Post on 10-Jul-2015




3 download

Embed Size (px)


<p>Visitor Pattern</p> <p>Visitor Pattern Yateen L. NikhargeAravindh Manickavasagam Ider ZhengSThe VISITOR FamilyThe Visitor family allows new methods to be added to existing hierarchies without modifying the hierarchies.The Patterns in this family are:VISITORACYCLIC VISITORDECORATOREXTENSION OBJECT</p> <p>IntentRepresent 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.MotivationDecouple the data structure and algorithms.Allow addition of new algorithms without changing the data structure.Example: Modem ConfigurationTo configure a modem for Unix, create an instance of the visitor and pass it to accept.The appropriate derivative call visit(this)New OS configuration can be added by adding a new derivative of visitor.</p> <p>Motivation (contd)Motivation(contd)</p> <p>ApplicabilityUse the pattern when-An object structure contains many classes of objects with differing interfacesMany distinct and unrelated operations need to be performed on objects in an object structureThe classes defining the object structure rarely change, but you often want to define new operations over the structureStructure</p> <p>ParticipantsVisitor (ModemVisitor): Declares a Visit operation or each class</p> <p>ConcreteVisitor (ConfigureUnixVisitor): Implements each VisitElement (Modem) :Defines an Accept operationConcreteElement(ZoomModem): Implements Accept </p> <p>Participants(Contd)ObjectStructure (Program):May provide a high-level interface to allow the visitor to visit its elements .CollaborationsA client using the Visitor pattern must create a ConcreteVisitor object and traverse the object structure, visiting each element with the visitor.</p> <p>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.</p> <p>12Consequences-BenefitsAdding new operationsGathers related operations and separates unrelated onesVisitors can visit objects that dont have a common parent classVisitors can accumulate state as they visit each element</p> <p>Consequences - LiabilitiesAdding new ConcreteElement classes is hard</p> <p>Allowing a Visitor to access the internal state of a Concrete Element breaks encapsulation</p> <p>Implementation - IssuesDouble DispatchWho is Responsible for Traversing the Object Structure?Object Structure </p> <p> Visitor whenever operations depend on other operations on the object structure</p> <p> Iterator Object</p> <p>Sample Codes</p> <p>ImplementationFunction Pointer</p> <p>18ImplementationGlobal FunctionImplementationStatic MethodsFunctorImplementationGraphic ProjectFrom CSE 687 Objected Oriented DesignGraphic</p> <p>Known UsesSmalltalk-80 CompilerIRIS Inventor ToolkitX Consortiums Fresco Application ToolkitBistro Programming Language Compiler</p> <p>Related PatternsCompositeInterpreterReferencesDesign Patterns: Elements of Reusable Object-Oriented Software By Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides</p> <p>v3</p> <p>v4</p> <p>v7</p> <p>v2</p> <p>v5</p> <p>v6</p> <p>e3</p> <p>e6</p> <p>e8</p> <p>e7</p> <p>e4</p> <p>e1</p> <p>e5</p> <p>e9</p> <p>e2</p>