Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Visitor Pattern
1. Visitor Pattern
Yateen L. Nikharge
Aravindh Manickavasagam
Ider Zheng
S
2. 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
3. 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.
4. 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.
7. 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…
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 …
11. 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.
13. 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
14. Consequences - Liabilities
S Adding new ConcreteElement classes is hard
S Allowing a Visitor to access the internal state of a
Concrete Element breaks encapsulation
15. 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
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