Marcelo Santos 1 Design Patterns Object Oriented Programming Advanced Course 2007.

Slides:



Advertisements
Similar presentations
Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
Advertisements

Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
Chapter 3: The Decorator Pattern
Unit 7.6 Lesson 2 Goals Identify and use flowchart symbols. Plan a sequence of events and incorporate them into a flowchart. Create a simple flowchart.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Open-closed principle.
Aims and Objectives Aim describe skills, knowledge required to prepare and serve a range of teas, coffees and other non-alcoholic beverages.
SE-2811 Dr. Mark L. Hornick 1. The Decorator Pattern SE-2811 Dr. Mark L. Hornick 2.
CS 210 Introduction to Design Patterns September 12 th, 2006.
Do you know what’s in your cup??. Espresso is a strong black coffee made by forcing steam through dark-roast aromatic coffee beans at high pressure in.
Object Oriented Programming Lecture 7: Algorithm animation using strategy and factory patterns, The Adapter design pattern
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Name of.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Farewells You are only as good as you last deed!.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Introduction.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Command Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
Winter 2011ACS-3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
More OOP Design Patterns
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
Struts 2.0 an Overview ( )
Problem Solving for Programming Session 4 Calculating and Keeping Track of Values.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
Computer Science II 810:062 Section 01 Session 3 - Objects and Responsibilities.
9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns.
Design Patterns: Structural Design Patterns
Lecture 16 Composition vs Inheritance: The Final Chapter.
MTO Ordering System HENRY, HAYDEN, AND KELLIE. Purpose  Our project modeled an ordering system similar to Sheetz  It allowed a user to order a variety.
Abstract Factory Pattern. What Is an Abstract Factory Pattern?  The Abstract Factory pattern is a creative way to expand on the basic Factory pattern.
Jan Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
Decorator Pattern So many options!. Starbuzz Coffee  Want to offer a variety of combinations of coffee and condiments  Cost of a cup depends on the.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
CSC 313 – Advanced Programming Topics. Open-Closed Principle Classes should be open for extension, but closed to modification  So, what does this mean?
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Design Patterns. OO-Concepts Don’t rewrite code Encapsulation Inheritance Write flexible code.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Using Software Design Patterns Bill Anderson. About me Fox developer since 1987 Fox developer since 1987 Program Director, Los Angeles Visual Foxpro Developers.
How to Make an Iced Caramel Macchiato Espresso Beverage Justin Bass December 08, 2009 ENGL 393 Section 401.
CSC 480 Software Engineering Lab 5 – Abstract Factory Pattern Oct 30, 2002.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Decorator Design Pattern Phillip Shin. Overview Problem Solution Example Key points.
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Week 5, Day 2: Decorator Decorators Muddiest Point Tomorrow: Quiz on lab reading: web.msoe.edu/hasker/se2811/labs/5/ SE-2811 Slide design:
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
Builder Introduction. Intent Separate the construction of a complex object from its representation so that the same construction process can create different.
CS 210 Introduction to Design Patterns September 14 th, 2006.
Other names for coffee Java Cup of joe Battery acid Tar Mud Jitter juice Brew.
Learning to Design Programs
Object-Orientated Analysis, Design and Programming
Design Patterns Lecture part 2.
Interfaces.
7. Decorator, Façade Patterns
Decorator Pattern.
OO Design Patterns - Decorator
Week 7, Class 1: The Command Pattern (cont.)
Advanced Java Programming
7. Decorator SE2811 Software Component Design
Implementing Black Scholes in Providing Easily-Accessed Objective Stock Predictions Nihaar Sinha.
Donovan S. Marketing Essentials Period 7
16. Visitors SE2811 Software Component Design
Presentation transcript:

Marcelo Santos 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

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.} 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 Dark Roast Coffee, Chocolate, Chocolate, Whip SEK 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 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 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?

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?