Presentation is loading. Please wait.

Presentation is loading. Please wait.

עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns

Similar presentations


Presentation on theme: "עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns"— Presentation transcript:

1 עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns

2 Outline Design Patterns Singleton pattern Visitor pattern

3 Obejct-Oriented Software Design
Designing object-oriented software is hard – designing reusable object oriented software if even harder. Your design should be specific to the problem at hand, but also general enough to address future problems and requirements. Don’t solve every problem from first principles - Reuse solutions that have worked in the past.

4 Design patterns A general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design. It is a description or template for how to solve a problem. Patterns are formalized best practices that the programmer can use to solve common problems when designing.

5 Example 1 - problem Design problem:
We want to ensure a class only has one instance, and provide a global point of access to it. Examples: Log file Printer spooler File system access

6 Example 1- Solution Singleton Pattern

7 Singleton pattern The instance is held privately in the class.
A getter is responsible of returning the instance. The constructor is private.

8 Singleton pattern Eager initialization
public class Singleton { private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; }

9 Singleton pattern Lazy initialization
public class SingletonDemo { private static SingletonDemo instance = null; private SingletonDemo() { } public static SingletonDemo getInstance(){ if (instance == null) { instance = new SingletonDemo(); } return instance;

10 Example 2 Visitor pattern

11 Reminder: Polymorphic Variables

12 Reminder: Polymorphic Variables
Animal animal; animal = new Dog(); animal.say(); \\ “Woof” animal = new Cat(); animal.say(); \\ “Miau” Animal * + say() * Cat + say() Dog + say() System.out.println(“Woof”) System.out.println(“Miau”)

13 What happens if a method accepts an argument?
Animal * + say() * + eats(mouse: Mouse) : bool * + eats(cat: Cat): bool * boolean b; Animal cat = new Cat(); Animal mouse = new Mouse(); b = cat.eats(mouse); //compilation error Cat + say() + eats(mouse : Mouse) : bool + eats(cat : Cat) : bool Mouse + say() + eats(mouse : Mouse) : bool + eats(cat : Cat) : bool

14 No support for polymorphism of arguments
Java (and many other languages) supports only polymorphism of the receiver (the variable on which the method is invoked). Polymorphism of the receiver is performed using single dispatch. Polymorphism of arguments is performed using multiple-level dispatch, which is resource consuming. Therefore, it is not supported. Nevertheless, we can simulate it using the Visitor Pattern.

15 The visitor pattern (for methods with one argument)
Define two interfaces: Visitor and Visited. The Visitor interface contains a method for each possible argument type. The Visited interface contains an accpet() method. Visitor +visit (cat : Cat) +visit (mouse : Mouse) Visited +accept(v : Visitor)

16 The visitor pattern (for methods with one argument)
The receiver's base class implements the visitor interface The argument’s base class implements the visited interface Visitor +visit (cat : Cat) +visit (mouse : Mouse) Visited +accept (v : Visitor) Animal * +eats(animal: Animal)* +visit (cat : Cat)* +visit (mouse : Mouse)*

17 Implementation of Visitor subclasses
public class Cat extends Animal{ public void eats(Animal animal){ animal.accept(this); } public void visit(Cat cat){ s.o.p(“No”); } public void visit(Mouse mouse){ s.o.p(“Yes”); } Implementation of Visited subclasses public void accept(Animal animal){ animal.visit(this); }

18 Implementation of Visitor subclasses
public class Mouse extends Animal{ public void eats(Animal animal){ animal.accept(this); } public void visit(Cat cat){ s.o.p(“No”); } public void visit(Mouse mouse){ s.o.p(“No”); } Implementation of Visited subclasses public void accept(Animal animal){ animal.visit(this); }

19 Putting it all together
Visitor +visit (cat : Cat) +visit (mouse : Mouse) Visited +accept (v : Visitor) Animal * +eats (animal: Animal) animal.accept(this) Cat +accept(animal: Animal) +visit (cat : Cat) +visit (mouse : Mouse) Mouse +accept(animal: Animal) +visit (cat : Cat) +visit (mouse : Mouse) animal.visit(this) animal.visit(this)

20 Different types of Visitor and Visited
Employees and Reports example

21 print(s: Specialist)+ Employee
Format f = new CSV(); Employee e = new Manager(); f.print(e); Format print(r: Regular)+ print(m: Manager)+ print(s: Specialist)+ Employee Regular Manager Block print(r: Regular)+ print(m: Manager)+ print(s: Specialist)+ CSV print(r: Regular)+ print(m: Manager)+ print(s: Specialist)+ Specialist

22 visit(s: Specialist)+ Csv visit(r : Regular)+ visit(m :Manager)+
Visitor visit(r : Regular) visit(m :Manager) visit(s :Specialist) Visited accept(v: Visitor) Employee accept(v: Visitor) Format print(e :Employee)+ e.accept(this) Regular accept(v: Visitor) + Manager accept(v: Visitor)+ Block visit(r: Regular)+ visit(m: Manager)+ visit(s: Specialist)+ Csv visit(r : Regular)+ visit(m :Manager)+ visit(s :Specialist)+ Specialist accept(v: Visitor)+ v.visit(this)

23 The Visitor Pattern Advantages
Simulating multiple-level dispatch. Type-based treatment without instanceof. Extending the functionality of a class hierarchy by an external class.

24 The Visitor Pattern Shortcomings
Tedious to code. Adding a Visited class requires modifying existing Visitor classes. Dispatching multiple arguments becomes inconvenient.


Download ppt "עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns"

Similar presentations


Ads by Google