2. Overview Of Pattern:-
● Problem
● Intent
● Motivation
● Applicability
● Structure
● Participants
● Collaboration
● Consequences
● Related Pattern
3. Real Life Problem
Problem
● A Person Want to go at Office From Home
Constraint
● Office Is So Far From His Home.
● And He Don't Have Any Personal Vehicle.
5. Solution
● The Visitor pattern represents an operation to be performed on the
elements of an object structure without changing the classes on which
it operates. This pattern can be observed in the operation of a taxi
company. When a person calls a taxi company (accepting a visitor), the
company dispatches a cab to the customer. Upon entering the taxi the
customer, or Visitor, is no longer in control of his or her own
transportation.
8. Intent:-
● Represent 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 element on which it operates.
9. Motivation:-
● Problems with this approach:
➢ Adding new operations requires changes to all of
the node classes
➢ It can be confusing to have such a diverse set of
operations in each node class. For example,
mixing type-checking code with pretty-printing
code can be hard to understand and maintain.
● Another solution is to encapsulate a desired operation in a
separate object, called a visitor. The visitor object then
traverses the elements of the tree. When an tree node
"accepts" the visitor, it invokes a method on the visitor that
includes the node type as an argument. The visitor will
then execute the operation for that node - the operation
that used to be in the node class.
10. Applicability:-
Use the Visitor Pattern When
➢ There are many distinct and unrelated operations needed to
be performed. Visitor pattern allows us to create a separate
visitor concrete class for each type of operation and to
separate this operation implementation from the objects
structure.
➢ The object structure is not likely to be changed but is very
probable to have new operations which have to be added.
Since the pattern separates the visitor (representing
operations, algorithms, behaviors) from the object structure
it's very easy to add new visitors as long as the structure
remains unchanged.
➢ Similar operations have to be performed on objects of
different types grouped in a structure (a collection or a more
complex structure)
11. Participants:-
● Visitor
➢ Declares a Visit operation for each class of ConcreteElement
in the object structure.The operation's name and signature
identifies the class that sends the Visit request to the
visitor.That lets the visitor determine the concrete class of
the element being visited.Then the visitor can access the
element directly through its particular interface.
● ConcreteVisitor
➢ Implements each operation declared by Visitor.Each
operation implements a fragment of the algorithm defined
for the corresponding class of object in the
structure.ConcretVisitor provides the context for the
algorithm and stores its local state.This state often
accumulates results during the traversal of the structure.
➢ Element
➢ Defines an Accept operation that takes a visitor as an
argument.
12. ● ConcreteElement
➢ Implements an Accept operation that takes a visitor as
an argument
● ObjectStructure
➢ Can enumerate its elements.
➢ May provide a high-level interface to allow the visitor to
visit its elements.
Continue...
13. Collaboration:-
➢ A client that uses the Visitor pattern must create a
ConcreteVisitor object and then 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 itselef as
an argument to this operation to let the visitor access its
state, if necessary.
14. Consequences:-
● Benefits
➢ Adding new operations is easy.
➢ Related behavior isn't spread over the
classes defining the object structure; it's
localized in a visitor. Unrelated sets of
behavior are partitioned in their own
visitor subclasses.
● Liabilities
● Adding new ConcreteElement classes is
hard. Each new ConcreteElement gives
rise to a new abstract operation on Visitor
and a corresponding implementation in
every ConcreteVisitor class.
15. ● Single-Dispatch
➢ The actual method invoked depends on the
name of the request (method signature) and the
type of the receiver object.
● Double-Dispatch
➢ The actual method invoked depends on the
name of the request and the types of two
receivers.
Continue...
16. Related Pattern:-
● Composite
➢ Visitors can be used to apply an operation over an
object structure defined by the Composite pattern.
● Interpreter
➢ Visitor may be applied to do the interpretation.