Download presentation
Presentation is loading. Please wait.
1
Marcelo Santos 1 Design Patterns Object Oriented Programming Advanced Course 2007
2
Marcelo Santos 2 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!
3
Marcelo Santos 3 How to use design patterns?
4
Marcelo Santos 4 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
5
Marcelo Santos 5 An easy way Hack a similar code example from internet
6
Marcelo Santos 6 Java example Run modified hacked Java example in NetBins
7
Marcelo Santos 7 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
8
Marcelo Santos 8
9
9 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?
10
Marcelo Santos 10 The decorator design pattern Also called wrapper Use it to add (or decorate with) more functionalities to an object/class
11
Marcelo Santos 11 The decorator design pattern Class for the types of coffee Class for the extras General class that adds something to the coffee
12
Marcelo Santos 12 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.}
13
Marcelo Santos 13 Decorator abstract class 1.public abstract class CondimentDecorator extends Beverage { 2.public abstract String getDescription(); 3.}
14
Marcelo Santos 14 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.}
15
Marcelo Santos 15 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.}
16
Marcelo Santos 16 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.…
17
Marcelo Santos 17 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
18
Marcelo Santos 18 Java example Run decorator Java example in NetBins
19
Marcelo Santos 19 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?
20
Marcelo Santos 20 Exercise: make the program more general ”decorate” an empty cup with the options made by the user: coffee, milk, sugar, tea, etc.
21
Marcelo Santos 21 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
22
Marcelo Santos 22 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.}
23
Marcelo Santos 23 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!!! Can you think of a design pattern for the GUI?
24
Marcelo Santos 24 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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.