
fadca107414609734f44e14a3ace345c.ppt
- Количество слайдов: 24
Design Patterns Object Oriented Programming Advanced Course 2007 Marcelo Santos 1
Design patterns • Patterns don't give code, but general solutions to design problems • We need to recognize places in our designs and existing applications where we can apply them • In the end, we need the code for the compiler! Marcelo Santos 2
How to use design patterns? Marcelo Santos 3
An example: a coffee machine • You are requested to do an implementation for the GUI menu for the coffee machine • Return value: the option chosen by the user Marcelo Santos 4
An easy way Hack a similar code example from internet Marcelo Santos 5
Java example • Run modified hacked Java example in Net. Bins Marcelo Santos 6
Adding more options • There are 4 types of coffee where the user can add something more • Soy, sugar, whipped milk, cinnamon, coffee without caffeine, strong coffee, etc. . • Each set of options have different prices • Easy solution: one class for each combination of options Marcelo Santos 7
Marcelo Santos 8
Maintenance • In the design, you should predict the future: what if the client want to add more options or change the price? • Maintenance: how easy will it be to do a change? Marcelo Santos 9
The decorator design pattern • Also called wrapper • Use it to add (or decorate with) more functionalities to an object/class Marcelo Santos 10
The decorator design pattern Class for the types of coffee General class that adds something to the coffee Class for the extras Marcelo Santos 11
Component abstract class 1. public abstract class Beverage { 2. String description = "Unknown Beverage"; 3. 4. public String get. Description() { 5. return description; 6. } 7. 8. public abstract double cost(); 9. } Marcelo Santos 12
Decorator abstract class 1. public abstract class Condiment. Decorator extends Beverage { 2. public abstract String get. Description(); 3. } Marcelo Santos 13
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. } Marcelo Santos 14
Class for the concrete decorators 1. public class Chocolate extends Condiment. Decorator { 2. Beverage beverage; 3. 4. public Chocolate(Beverage beverage) { 5. this. beverage = beverage; 6. } 7. 8. public String get. Description() { 9. return beverage. get. Description() + ", Chocolate"; 10. } 11. 12. public double cost() { 13. return 2 + beverage. cost(); 14. } 15. } Marcelo Santos 15
Main program 1. …. 2. Beverage beverage = new Espresso(); 3. System. out. println(beverage. get. Description() 4. + " SEK " + beverage. cost()); 5. 6. Beverage beverage 2 = new Dark. Roast(); 7. beverage 2 = new Chocolate(beverage 2); 8. beverage 2 = new Chocolate(beverage 2); 9. beverage 2 = new Whip(beverage 2); 10. System. out. println(beverage 2. get. Description() 11. + " SEK " + beverage 2. cost()); 12. … Marcelo Santos 16
Sample output 1. Espresso SEK 14. 0 2. Dark Roast Coffee, Chocolate, Whip SEK 17. 0 3. House Blend Coffee, Soy, Chocolate, Whip SEK 13. 0 Marcelo Santos 17
Java example • Run decorator Java example in Net. Bins Marcelo Santos 18
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? Marcelo Santos 19
Exercise: make the program more general • ”decorate” an empty cup with the options made by the user: coffee, milk, sugar, tea, etc. Marcelo Santos 20
The decorator design pattern Class for the empty cup General class that adds something to the cup Class for the extras The coffe types are now just decorators Marcelo Santos 21
Class for the empty cup 1. public class Empty. Cup extends Beverage { 2. 3. public Empty. Cup () { 4. description = ""; 5. } 6. 7. public double cost() { 8. return 0; 9. } 10. } Marcelo Santos 22
The GUI 1. super(new Border. Layout()); Code with strong coupling!!! 2. 3. 4. 5. //Create the check boxes. coffee. Button = new JCheck. Box("Coffee"); coffee. Button. set. Mnemonic(Key. Event. VK_C); coffee. Button. set. Selected(false); 6. 7. 8. milk. Button = new JCheck. Box("Milk"); milk. Button. set. Mnemonic(Key. Event. VK_M); milk. Button. set. Selected(false); 9. 10. 11. chocolate. Button = new JCheck. Box("Chocolate"); chocolate. Button. set. Mnemonic(Key. Event. VK_H); chocolate. Button. set. Selected(false); 12. cappuccino. Button = new JCheck. Box("Cappuccino"); 13. cappuccino. Button. set. Mnemonic(Key. Event. VK_P); 14. cappuccino. Button. set. Selected(false); 15. …. . Can you think of a design pattern for the GUI? Marcelo Santos 23
Exercise 1. Build the GUI (show also the prices) using a design pattern 2. Can you also use the factory design pattern to ”build” the drinks? Marcelo Santos 24
fadca107414609734f44e14a3ace345c.ppt