marcelo santos 1 design patterns object oriented programming advanced course 2007
Post on 19-Dec-2015
216 views
TRANSCRIPT
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!
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
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
Marcelo Santos 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?
Marcelo Santos 10
The decorator design pattern
• Also called wrapper
• Use it to add (or decorate with) more functionalities to an object/class
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
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. }
Marcelo Santos 13
Decorator abstract class
1. public abstract class CondimentDecorator extends Beverage {
2. public abstract String getDescription();
3. }
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.}
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.}
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.…
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
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?
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.
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
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.}
Marcelo Santos 23
The GUI1. 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?