VISITOR DESIGN PATTERN
MHD SALEM KABBANI
PROBLEM
• When we have similar classes and we want to add new
behavior to them.
• Each one of those classes should performs the new behavior in
different way.
• This operation forces us to modify all those class.
VISITOR PATTERN
• Visitor pattern is one of the behavioral design patterns.
• It allows you to add new behavior to an existed class without
modifying it.
• New behavior (algorithm) is written in the visitor class which
allows to separate the algorithm from object structure.
SOLUTION
• We create visitor interface which has visit method for each type
of our concrete elements.
• Concrete visitor implements visitor and its visit methods.
• Each concrete element implements element interface which
contains accept method.
• Accept method takes visitor interface as a parameter and
returns visitor’s visit with current object.
UML
IMPLEMENTATION
IMPLEMENTATION
IMPLEMENTATION
Book: Software Engineering
cost = 19
Banana: cost = 20
Book: Java Tutorial cost =
95
Apple: cost = 25
Total Cost = 159
PROS. VS CONS.
• Needs to update all visitors
each time a class gets added
to or removed from the
element hierarchy.
• Visitors might lack the
necessary access to the
private fields and methods of
the elements that they’re
supposed to work with.
• Open/Closed Principle: add a
new behavior that works with
objects of different classes
without changing these
classes.
• Single Responsibility
Principle: move multiple
versions of the same behavior
into the same class.

Visitor design pattern

  • 1.
  • 2.
    PROBLEM • When wehave similar classes and we want to add new behavior to them. • Each one of those classes should performs the new behavior in different way. • This operation forces us to modify all those class.
  • 3.
    VISITOR PATTERN • Visitorpattern is one of the behavioral design patterns. • It allows you to add new behavior to an existed class without modifying it. • New behavior (algorithm) is written in the visitor class which allows to separate the algorithm from object structure.
  • 4.
    SOLUTION • We createvisitor interface which has visit method for each type of our concrete elements. • Concrete visitor implements visitor and its visit methods. • Each concrete element implements element interface which contains accept method. • Accept method takes visitor interface as a parameter and returns visitor’s visit with current object.
  • 5.
  • 6.
  • 7.
  • 8.
    IMPLEMENTATION Book: Software Engineering cost= 19 Banana: cost = 20 Book: Java Tutorial cost = 95 Apple: cost = 25 Total Cost = 159
  • 9.
    PROS. VS CONS. •Needs to update all visitors each time a class gets added to or removed from the element hierarchy. • Visitors might lack the necessary access to the private fields and methods of the elements that they’re supposed to work with. • Open/Closed Principle: add a new behavior that works with objects of different classes without changing these classes. • Single Responsibility Principle: move multiple versions of the same behavior into the same class.

Editor's Notes

  • #3 الطريقة التقليدية لحل هالمشكلة عن طريق اضافة انترفيس بتحوي العملية اللي بدي ضيفا واعمللها implement بكل الكلاسات
  • #4 بساهم ال visitor في تخفيف تعقيد كود الاوبجيكت بسبب نقل الخوارزميات الى مكان منفصل
  • #10 The Single Responsibility Principle — Classes should have a single responsibility and thus only a single reason to change. ------- each one of versions in visitor Open/Closed Principle  — Classes and other entities should be open for extension but closed for modification. ------------