240 likes | 370 Views
Design Patterns. Object Oriented Programming Advanced Course 2007. 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!.
E N D
Design Patterns Object Oriented Programming Advanced Course 2007 Marcelo Santos
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
How to use design patterns? Marcelo Santos
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
An easy way Hack a similar code example from internet Marcelo Santos
Java example • Run modified hacked Java example in NetBins Marcelo Santos
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
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
The decorator design pattern • Also called wrapper • Use it to add (or decorate with) more functionalities to an object/class Marcelo Santos
The decorator design pattern General class that adds something to the coffee Class for the types of coffee Class for the extras Marcelo Santos
Component abstract class • public abstract class Beverage { • String description = "Unknown Beverage"; • public String getDescription() { • return description; • } • public abstract double cost(); • } Marcelo Santos
Decorator abstract class • public abstract class CondimentDecorator extends Beverage { • public abstract String getDescription(); • } Marcelo Santos
Class for coffee • public class Espresso extends Beverage { • public Espresso() { • description = "Espresso"; • } • public double cost() { • return 10; • } • } Marcelo Santos
Class for the concrete decorators • public class Chocolate extends CondimentDecorator { • Beverage beverage; • public Chocolate(Beverage beverage) { • this.beverage = beverage; • } • public String getDescription() { • return beverage.getDescription() + ", Chocolate"; • } • public double cost() { • return 2 + beverage.cost(); • } • } Marcelo Santos
Main program • …. • Beverage beverage = new Espresso(); • System.out.println(beverage.getDescription() • + " SEK " + beverage.cost()); • Beverage beverage2 = new DarkRoast(); • beverage2 = new Chocolate(beverage2); • beverage2 = new Chocolate(beverage2); • beverage2 = new Whip(beverage2); • System.out.println(beverage2.getDescription() • + " SEK " + beverage2.cost()); • … Marcelo Santos
Sample output • Espresso SEK 14.0 • Dark Roast Coffee, Chocolate, Chocolate, Whip SEK 17.0 • House Blend Coffee, Soy, Chocolate, Whip SEK 13.0 Marcelo Santos
Java example • Run decorator Java example in NetBins Marcelo Santos
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
Exercise: make the program more general • ”decorate” an empty cup with the options made by the user: coffee, milk, sugar, tea, etc. Marcelo Santos
The decorator design pattern General class that adds something to the cup Class for the empty cup Class for the extras The coffe types are now just decorators Marcelo Santos
Class for the empty cup • public class EmptyCup extends Beverage { • public EmptyCup () { • description = ""; • } • public double cost() { • return 0; • } • } Marcelo Santos
The GUI Code with strong coupling!!! • super(new BorderLayout()); • //Create the check boxes. • coffeeButton = new JCheckBox("Coffee"); • coffeeButton.setMnemonic(KeyEvent.VK_C); • coffeeButton.setSelected(false); • milkButton = new JCheckBox("Milk"); • milkButton.setMnemonic(KeyEvent.VK_M); • milkButton.setSelected(false); • chocolateButton = new JCheckBox("Chocolate"); • chocolateButton.setMnemonic(KeyEvent.VK_H); • chocolateButton.setSelected(false); • cappuccinoButton = new JCheckBox("Cappuccino"); • cappuccinoButton.setMnemonic(KeyEvent.VK_P); • cappuccinoButton.setSelected(false); • ….. Can you think of a design pattern for the GUI? Marcelo Santos
Exercise • Build the GUI (show also the prices) using a design pattern • Can you also use the factory design pattern to ”build” the drinks? Marcelo Santos