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 pf 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 Visitor pattern Example 2

11 Reminder: Polymorphic Variables

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

13 What happens if a method accepts an argument? Animal * + say() * + eats(mouse: Mouse) : bool * + eats(cat: Cat): bool * Cat + say() + eats(mouse : Mouse) : bool + eats(cat : Cat) : bool Mouse + 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

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 Animal * +eats(animal: Animal)* +visit (cat : Cat)* +visit (mouse : Mouse)* Visitor +visit (cat : Cat) +visit (mouse : Mouse) Visited +accept (v : Visitor)

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 Animal * +eats (animal: Animal) Visited +accept (v : Visitor) Visitor +visit (cat : Cat) +visit (mouse : Mouse) 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)

20 Different types of Visitor and Visited Employees and Reports example

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

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

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