1 / 24

Design Patterns

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!.

vaughan
Download Presentation

Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Patterns Object Oriented Programming Advanced Course 2007 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

  3. How to use design patterns? 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

  5. An easy way Hack a similar code example from internet Marcelo Santos

  6. Java example • Run modified hacked Java example in NetBins 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

  8. 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 General class that adds something to the coffee Class for the types of coffee Class for the extras Marcelo Santos

  12. Component abstract class • public abstract class Beverage { • String description = "Unknown Beverage"; • public String getDescription() { • return description; • } • public abstract double cost(); • } Marcelo Santos

  13. Decorator abstract class • public abstract class CondimentDecorator extends Beverage { • public abstract String getDescription(); • } Marcelo Santos

  14. Class for coffee • public class Espresso extends Beverage { • public Espresso() { • description = "Espresso"; • } • public double cost() { • return 10; • } • } Marcelo Santos

  15. 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

  16. 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

  17. 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

  18. Java example • Run decorator Java example in NetBins 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 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

  22. Class for the empty cup • public class EmptyCup extends Beverage { • public EmptyCup () { • description = ""; • } • public double cost() { • return 0; • } • } Marcelo Santos

  23. 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

  24. 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

More Related