Presentation is loading. Please wait.

Presentation is loading. Please wait.

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns.

Similar presentations


Presentation on theme: "Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns."— Presentation transcript:

1 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns

2 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 2 Software Frameworks They are code libraries designed to facilitate software development. Examples: –Ruby on Rails: for creating web applications –Unidraw: for building domain-specific graphical editors

3 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 3 Patterns and frameworks Both enable design elements to be recorded and reused Despite the similarities, they are quite different! So what’s the difference?

4 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 4 Patterns and frameworks Different size: A framework defines an architecture for a complete application A pattern describe a solution to a single design problem, and can be used in many applications or frameworks

5 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 5 Patterns and frameworks Different content: A framework is a mixture of design and code, and can be extended (or customized) by programmers A pattern is a design idea, and can be adapted and implemented in various ways in different languages

6 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 6 Patterns and frameworks Difference in portability: Patterns are language independent and can be applied several situations Frameworks already have code and are usually restricted to a particular environment. Patterns are more portable than frameworks

7 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 7 Pattern documentation or pattern templates Name Problem: situation in which the pattern is applicable Solution: describes the design elements that address the problem Consequences: results and tradeoffs of applying the pattern Other aspects: aliases, related patterns, example use, sample program code, context, etc.

8 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 8 The strategy pattern Name: strategy Problem: alternative implementations of an algorithm to be provided in such a way that different instances of a class can support different implementations of the same operation, or even change implementation at run-time. Solution: see class diagram Consequences: extra level of indirection, need polymorphism

9 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 9 The observer pattern

10 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 10 The observer pattern Problem: you want to take some action when some event happens. How can you do this?

11 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 11 Possible solution Keep on observing and checking. Is this a good solution? observer

12 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 12 A better solution Somehow, the observer register that it wants to be notified of the event. Now the observer can go to sleep or do something else, until…

13 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 13 A better solution Until the event finally happens! And the observer is notified

14 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 14 A better solution Now that the observer has been notified that the event has happened, it can proceed to its processing of the event

15 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 15 The observer pattern You can have several objects waiting for several events. What’s the solution? What’s the class diagram?

16 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 16 The decorator pattern

17 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 17 An example: a coffee machine You are requested to do an implementation for the GUI menu for a coffee machine Return value: the option chosen by the user

18 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 18 An easy way Hack a similar code example from internet

19 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 19 Adding more options There are 4 basic types of coffee: normal coffee, strong coffee, coffee without caffeine, white coffee The user can add some flavouring: soy milk, sugar, whipped milk, cinnamon, strawberry cream. Each set of options have different prices Easy solution: one class for each combination of options

20 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 20

21 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 21 Problems In the design, you should predict the future: what if the client wants to add more options or change the price? Maintenance: how easy will it be to do a change or find and fix a bug? Can you give a better solution?

22 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 22 The decorator design pattern Also called wrapper Use it to add (or decorate with) more functionalities to an object/class

23 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 23 The decorator design pattern Class for the types of coffee Class for the extras General class that adds something to the coffee

24 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 24 Component abstract class 1.public abstract class Beverage { 2.String description = "Unknown Beverage"; 3. 4.public String getDescription() { 5.return description; 6.} 7. 8.public abstract double cost(); 9.}

25 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 25 Decorator abstract class 1.public abstract class CondimentDecorator extends Beverage { 2.public abstract String getDescription(); 3.}

26 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 26 Class for coffee 1.public class Espresso extends Beverage { 2. 3.public Espresso() { 4.description = "Espresso"; 5.} 6. 7.public double cost() { 8.return 10; 9.} 10.}

27 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 27 Class for the concrete decorators 1.public class Chocolate extends CondimentDecorator { 2.Beverage beverage; 3. 4.public Chocolate(Beverage beverage) { 5.this.beverage = beverage; 6.} 7. 8.public String getDescription() { 9.return beverage.getDescription() + ", Chocolate"; 10.} 11. 12.public double cost() { 13.return 2 + beverage.cost(); 14.} 15.} Each chocolate topping has a fixed cost

28 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 28 Main program 1.…. 2.Beverage beverage = new Espresso(); 3.System.out.println(beverage.getDescription() 4.+ " SEK " + beverage.cost()); 5. 6.Beverage beverage2 = new DarkRoast(); 7.beverage2 = new Chocolate(beverage2); 8.beverage2 = new Chocolate(beverage2); 9.beverage2 = new Whip(beverage2); 10.System.out.println(beverage2.getDescription() 11.+ " SEK " + beverage2.cost()); 12.…

29 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 29 Sample output 1.Espresso SEK 14.0 2.Dark Roast Coffee, Chocolate, Chocolate, Whip SEK 17.0 3.House Blend Coffee, Soy, Chocolate, Whip SEK 13.0

30 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 30 Is this a good solution? The objects are loosely coupled: if you add tea to the options, you don’t need to change the code for the coffee object. The only constant thing in software is that it is always changing. Is this code easy to adapt to a new reality? How can you make the design more general?

31 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 31 Making the design more general ”decorate” an empty cup with the options made by the user: coffee, milk, sugar, tea, etc.

32 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 32 The decorator design pattern Class for the empty cup Class for the extras General class that adds something to the cup The coffe types are now just decorators

33 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 33 Class for the empty cup 1.public class EmptyCup extends Beverage { 2. 3.public EmptyCup () { 4.description = ""; 5.} 6. 7.public double cost() { 8.return 0; 9.} 10.}

34 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 34 The GUI 1.super(new BorderLayout()); 2. //Create the check boxes. 3. coffeeButton = new JCheckBox("Coffee"); 4. coffeeButton.setMnemonic(KeyEvent.VK_C); 5. coffeeButton.setSelected(false); 6. milkButton = new JCheckBox("Milk"); 7. milkButton.setMnemonic(KeyEvent.VK_M); 8. milkButton.setSelected(false); 9. chocolateButton = new JCheckBox("Chocolate"); 10. chocolateButton.setMnemonic(KeyEvent.VK_H); 11. chocolateButton.setSelected(false); 12. cappuccinoButton = new JCheckBox("Cappuccino"); 13. cappuccinoButton.setMnemonic(KeyEvent.VK_P); 14. cappuccinoButton.setSelected(false); 15.….. Code with strong coupling!!!

35 Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 35 Exercises 1.How can you build the GUI (showing also the prices) using the observer design pattern? 2.Study the factory design pattern. Use it to ”build” the drinks.


Download ppt "Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns."

Similar presentations


Ads by Google