SlideServe
Menu
Préférences
© SlideServe · v2026

Advanced Design Patterns in Object-Oriented Programming: Coffee Machine Example

vaughan avatar
24 diapositives · 406 Views · Aug 02, 2014

This advanced course by Marcelo Santos delves into design patterns, highlighting their utility in solving persistent design problems. It emphasizes recognizing opportunities to apply these patterns in existing applications. Using a coffee machine GUI as a practical example, the course demonstrates the Decorator pattern, showcasing how it allows the addition of features like coffee options and customizations without altering the underlying code structure. Participants will learn methods of maintaining and extending the system with ease, preparing them for future enhancements.

Advanced Design Patterns in Object-Oriented Programming: Coffee Machine Example - slide 1
01 / 24Advanced Design Patterns in Object-Oriented Programming: Coffee Machine Example
1 / 17

Télécharger la présentation

Advanced Design Patterns in Object-Oriented Programming: Coffee Machine Example
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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