Where Do Surrogates Fit into This Proxy Pattern Observer Pattern Visitor Pattern By Kurt Rehwinkel
Visitor Pattern
-
Upload
ider-zheng -
Category
Education
-
view
1.193 -
download
3
Transcript of Visitor Pattern
S
Visitor Pattern
Yateen L. Nikharge
Aravindh Manickavasagam
Ider Zheng
The VISITOR Family
The Visitor family allows new methods to be addedto existing hierarchies without modifying thehierarchies.
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
Sam
ple
Cod
es
Implementation
Function Pointer
Implementation
Global Function
Implementation
Static Methods
Functor
Implementation
Graphic Project
From CSE 687 Objected Oriented Design
Graphic
“v1” “v3”
“v4” “v7”
“v2”
“v5”
“v6”“e8”
“e3”
“e6”
“e7”“e4”
“e1”
“e5”
“e9”“e2”
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