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.

Slides:



Advertisements
Similar presentations
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
Advertisements

Chapter 3: The Decorator Pattern
02 - Structural Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
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.
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.
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Algorithm Programming I/O via Java Streams Bar-Ilan University תשס"ח by Moshe Fresko.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
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.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns.
Lecture 16 Composition vs Inheritance: The Final Chapter.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.
Session 21 Chapter 10: Mechanisms for Software Reuse.
Jan Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
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.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
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.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
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?
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
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.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
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.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
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.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
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.
Java IO Exploring the java.io package and living to talk about it.
Object-Orientated Analysis, Design and Programming
Sections Inheritance and Abstract Classes
Module Road Map Refactoring Why Refactoring? Examples
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Abstract Classes.
7. Decorator, Façade Patterns
Decorator Pattern.
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
OO Design Patterns - Decorator
Decorator Pattern Richard Gesick.
7. Decorator SE2811 Software Component Design
7. Decorator, Façade Patterns
Decorator.
Chapter 14 Abstract Classes and Interfaces
Week 8, Class 3: Model-View-Controller
Decorator Pattern.
Presentation transcript:

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 combination that was ordered  Want to offer a variety of combinations of coffee and condiments  Cost of a cup depends on the combination that was ordered

First Design  Make a beverage class and a subclass for each legal combination

HouseBlend cost() HouseBlend cost() DarkRoast cost() DarkRoast cost() Decaf cost() Decaf cost() Espresso cost() Espresso cost() Beverage String description getDescription() > cost() … > But where are the fancy coffees? I could make this at home!

HouseBlend cost() HouseBlend cost() DarkRoast cost() DarkRoast cost() Decaf cost() Decaf cost() Espresso cost() Espresso cost() HouseBlendWith SteamedMilk cost() HouseBlendWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DecafWith SteamedMilk cost() DecafWith SteamedMilk cost() EspressoWith SteamedMilk cost() EspressoWith SteamedMilk cost() Beverage String description getDescription() > cost() … >

HouseBlend cost() HouseBlend cost() DarkRoast cost() DarkRoast cost() Decaf cost() Decaf cost() Espresso cost() Espresso cost() HouseBlendWith SteamedMilk cost() HouseBlendWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DecafWith SteamedMilk cost() DecafWith SteamedMilk cost() EspressoWith SteamedMilk cost() EspressoWith SteamedMilk cost() HouseBlend Mocha cost() HouseBlend Mocha cost() DarkRoast Mocha cost() DarkRoast Mocha cost() Decaf Mocha cost() Decaf Mocha cost() Espresso Mocha cost() Espresso Mocha cost() Beverage String description getDescription() > cost() … >

HouseBlend cost() HouseBlend cost() DarkRoast cost() DarkRoast cost() Decaf cost() Decaf cost() Espresso cost() Espresso cost() HouseBlendWith SteamedMilk cost() HouseBlendWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DarkRoastWith SteamedMilk cost() DecafWith SteamedMilk cost() DecafWith SteamedMilk cost() EspressoWith SteamedMilk cost() EspressoWith SteamedMilk cost() HouseBlend Mocha cost() HouseBlend Mocha cost() DarkRoast Mocha cost() DarkRoast Mocha cost() Decaf Mocha cost() Decaf Mocha cost() Espresso Mocha cost() Espresso Mocha cost() HouseBlend WithWhip cost() HouseBlend WithWhip cost() DarkRoast WithWhip cost() DarkRoast WithWhip cost() Decaf WithWhip cost() Decaf WithWhip cost() Espresso WithWhip cost() Espresso WithWhip cost() Beverage String description getDescription() > cost() … >

Second Design  Make the superclass contain booleans to specify which condiments are included and subclasses for each type of coffee  How do we compute cost?  What does it take to add a new condiment?  Make the superclass contain booleans to specify which condiments are included and subclasses for each type of coffee  How do we compute cost?  What does it take to add a new condiment?

Beverage String description milk soy mocha whip getDescription() >cost() hasMilk() setMilk() hasSoy() setSoy() hasMocha() setMocha() hasWhip() setWhip() … HouseBlend cost() HouseBlend cost() DarkRoast cost() DarkRoast cost() Decaf cost() Decaf cost() Espresso cost() Espresso cost() >

Design Principle  Classes should be open for extension, but closed for modification  “extension” is NOT subclassing  It means the addition of new behavior (without modifying the code!)  Classes should be open for extension, but closed for modification  “extension” is NOT subclassing  It means the addition of new behavior (without modifying the code!)

Decorator Pattern  Start with an instance of the “basic”classes and then decorate it with new capabilities Dark Roast Mocha cost() Whip cost()

Key Points  Decorators have the same supertypes as the objects they decorate  This lets us pass around the decorated object instead of the original (unwrapped) object  Decorator add behavior by delegating to the object it decorates and then adding its own behavior  Can add decorations at any time  Decorators have the same supertypes as the objects they decorate  This lets us pass around the decorated object instead of the original (unwrapped) object  Decorator add behavior by delegating to the object it decorates and then adding its own behavior  Can add decorations at any time

Decorator Pattern

> Component methodA() ConcreteComponent methodA() ConcreteComponent methodA() ConcreteDecoratorA Component wrappedObj methodA() newBehavior() ConcreteDecoratorA Component wrappedObj methodA() newBehavior() > Decorator methodA() > Decorator methodA() ConcreteDecoratorB Component wrappedObj Object newState() methodA() ConcreteDecoratorB Component wrappedObj Object newState() methodA() HAS-A

Let’s Look at the Code

public abstract class Beverage{ String description = “Unknown”; public String getDescription() { return description; } public abstract double cost(); } public abstract class CondimentDecorator extends Beverage { public abstract String getDescription(); public abstract double cost(); } public class Espresso extends Beverage { public Espresso() { description = “Espresso”; } public double cost() { return 1.99; }

public class Mocha extends CondimentDecorator { Beverage bev; public Mocha(Beverage bev) { this.bev = bev; } public String getDescription { return bev.getDescription() + “, Mocha”; } public double cost() { return.20 + bev.cost(); }

public class StarBuzz { public static void main(String args[]) { Beverage bev = new Espresso); bev = new Mocha(bev); bev = new Whip(bev); bev = new Soy(bev); System.out.println(bev.getDescription() + “ $”+ bev.getCost()); }

Decorators in Java  File I/O Example FileInputStreamBufferedInputStream LineNumberInputStream Component Concrete decorators

InputStream FileInputStream StringBufferInputStream FilterInputStream BufferedInputStream LineNumberInputStream concrete components abstract decorator concrete decorators abstract component

Lab Set Up  Using the Decorator Pattern to customize weapons