CS 210 Introduction to Design Patterns September 12 th, 2006.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
The Language of The Web. Web Server To make Web pages, you create files written in HTML and place them on a Web server The HTML in your web page tells.
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
SE-2811 Dr. Mark L. Hornick 1. The Decorator Pattern SE-2811 Dr. Mark L. Hornick 2.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Name of.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Marcelo Santos 1 Design Patterns Object Oriented Programming Advanced Course 2007.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns.
March 2004Object Oriented Design1 Object-Oriented Design.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
ADAPTER PATTERN Ali Zonoozi Design patterns course Advisor: Dr. Noorhoseini Winter 2010.
Class Hierarchy Discussion D. Constructor public class X { private int capacity; public X() { capacity = 16;} public X(int i) {capacity = i;} public int.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Winter 2011ACS-3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
The Language of The Web. Web Server To make Web pages, you create files written in HTML and place them on a Web server The HTML in your web page tells.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
CS 210 Introduction to Design Patterns September 28 th, 2006.
CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe()
9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns.
Lecture 16 Composition vs Inheritance: The Final Chapter.
CSC 204 Programming I Loop I The while statement.
Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.
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.
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.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Welcome to AP Computer Science JAVA Programming in.
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
CS 210 Review October 3, 2006.
What if the milk price goes up? What if a new topping is added? What design principles are violated?
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
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.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Classes - Intermediate
Week 5, Day 2: Decorator Decorators Muddiest Point Tomorrow: Quiz on lab reading: web.msoe.edu/hasker/se2811/labs/5/ SE-2811 Slide design:
Decorator Pattern ¢ Design: Which classes must be included?  Story As a user I want a weekend planner so that I can have more fun  Scenario Given.
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.
Computer Science I Lab 1 ISMAIL ABUMUHFOUZ | CS 180.
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
The Decorator Pattern Decorators in Java I/O classes SE-2811 Dr. Mark L. Hornick 1.
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.
Programming with Patterns Jeremy Cronan Alliance Safety Council
Appendix 1 - Packages Jim Fawcett copyright (c)
Object-Orientated Analysis, Design and Programming
Factory Method, Abstract Factory, and More
Tony Webb Coffee House.
7. Decorator, Façade Patterns
Decorator Pattern.
Object Oriented Design Patterns - Structural Patterns
OO Design Patterns - Decorator
Decorator Pattern Richard Gesick.
Code Animation Examples
class PrintOnetoTen { public static void main(String args[]) {
7. Decorator SE2811 Software Component Design
Scope of variables class scopeofvars {
7. Decorator, Façade Patterns
Software Design Lecture : 28.
Presentation transcript:

CS 210 Introduction to Design Patterns September 12 th, 2006

Head First Design Patterns Chapter 3 Decorator Pattern

A coffee shop example… Beverage description getDescription() Cost() DarkRoast cost() HouseBlend cost() Decaf cost() Espresso cost() What if you want to show the addition of condiments such as steamed milk, soy, mocha and whipped milk?

Page 81 Head First Design Patterns

Beverage class redone Page 83 Head First Design Patterns

Potential problems with the design so far? Solution is not easily extendable How to deal with new condiments Price changes New beverages that may have a different set of condiments – a smoothie? Double helpings of condiments

Design Principle The Open-Closed Principle Classes should be open for extension, but closed for modification.

The Decorator Pattern Take a coffee beverage object – say DarkRoast object Decorate it with Mocha Decorate it with Whip Call the cost method and rely on delegation to correctly compute the composite cost

Decorator Pattern approach Page 89 Head First Design Patterns

Computing Cost using the decorator pattern Page 90 Head First Design Patterns

Decorator Pattern The decorator pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub- classing for extending functionality.

Decorator Pattern Defined Page 91 Head First Design Patterns Decorator Pattern Defined

Decorator Pattern for Beverage Example Page 92 Head First Design Patterns

Starbuzz code public abstract class Beverage { String description = "Unknown Beverage"; public String getDescription() { return description; } public abstract double cost(); }

Decorator Class public abstract class CondimentDecorator extends Beverage { public abstract String getDescription(); }

Coding Bevarages public class Espresso extends Beverage { public Espresso() { description = "Espresso"; } public double cost() { return 1.99; }

Another beverage public class DarkRoast extends Beverage { public DarkRoast() { description = "Dark Roast Coffee"; } public double cost() { return.99; }

Coding Condiments public class Mocha extends CondimentDecorator { Beverage beverage; public Mocha(Beverage beverage) { this.beverage = beverage; } public String getDescription() { return beverage.getDescription() + ", Mocha"; } public double cost() { return.20 + beverage.cost(); }

Serving some coffee – Espresso please public class StarbuzzCoffee { public static void main(String args[]) { Beverage beverage = new Espresso(); System.out.println(beverage.getDescription() + " $" + beverage.cost());

How about a double mocha dark roast with whipped milk? Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); System.out.println(beverage2.getDescription() + " $" + beverage2.cost());

House blend with soy, mocha and whip, please Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); System.out.println(beverage3.getDescription() + " $" + beverage3.cost()); }

Running the application Espresso $1.99 Dark Roast Coffee, Mocha, Mocha, Whip $1.49 House Blend Coffee, Soy, Mocha, Whip $1.34