visitor pattern

Download Visitor Pattern

Post on 10-Jul-2015

1.179 views

Category:

Education

3 download

Embed Size (px)

TRANSCRIPT

Visitor Pattern

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

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.

Motivation (contd)Motivation(contd)

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

ParticipantsVisitor (ModemVisitor): Declares a Visit operation or each class

ConcreteVisitor (ConfigureUnixVisitor): Implements each VisitElement (Modem) :Defines an Accept operationConcreteElement(ZoomModem): Implements Accept

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.

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.

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

Consequences - LiabilitiesAdding new ConcreteElement classes is hard

Allowing a Visitor to access the internal state of a Concrete Element breaks encapsulation

Implementation - IssuesDouble DispatchWho is Responsible for Traversing the Object Structure?Object Structure

Visitor whenever operations depend on other operations on the object structure

Iterator Object

Sample Codes

ImplementationFunction Pointer

18ImplementationGlobal FunctionImplementationStatic MethodsFunctorImplementationGraphic ProjectFrom CSE 687 Objected Oriented DesignGraphic

Known UsesSmalltalk-80 CompilerIRIS Inventor ToolkitX Consortiums Fresco Application ToolkitBistro Programming Language Compiler

Related PatternsCompositeInterpreterReferencesDesign Patterns: Elements of Reusable Object-Oriented Software By Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides http://www.codeproject.com/KB/aspnet/SoftArch3.aspxv1

v3

v4

v7

v2

v5

v6

e3

e6

e8

e7

e4

e1

e5

e9

e2