18-decoratorfactory

18-decoratorfactory - Decorator and Factory Kenneth M....

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Decorator and Factory Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 — Lecture 18 — 10/22/2009 © University of Colorado, 2009 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Goals • Cover Material from Chapters 3 and 4 of the Design Patterns Textbook • Decorator Pattern • Factory and Abstract Factory Pattern 2
Background image of page 2
Decorator Pattern • The Decorator Pattern provides a powerful mechanism for adding new behaviors to an object at run-time • The mechanism is based on the notion of “wrapping” which is just a fancy way of saying “delegation” but with the added twist that the delegator and the delegate both implement the same interface • You start with object A that implements abstract type X • You then create object B that also implements abstract type X • You pass A into B’s constructor and then pass B to A’s client • The client thinks its talking to A but its actually talking to B • B’s methods augment A’s methods to provide new behavior 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Why? Open-Closed Principle • The decorator pattern provides yet another way in which a class’s runtime behavior can be extended without requiring modifcation to the class • This supports the goal oF the open-closed principle: • Classes should be open For extension but closed to modifcation • Inheritance is one way to do this, but composition and delegation are more ±exible (and Decorator takes advantage oF delegation) • Chapter 3’s “Starbuzz CoFFee” example clearly demonstrates why inheritance can get you into trouble and why delegation/composition provides greater run-time ±exibility 4
Background image of page 4
Starbuzz Coffee • Under pressure to update their “point of sale” system to keep up with their expanding set of beverage products • Started with a Beverage abstract base class and four implementations: HouseBlend, DarkRoast, Decaf, and Espresso • Each beverage can provide a description and compute its cost • But they also offer a range of condiments including: steamed milk, soy, and mocha • These condiments alter a beverage’s description and cost • “Alter” is a key word here since it provides a hint that we might be able to use the Decorator pattern 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Initial Starbuzz System getDescription() cost() description Beverage «Abstract» cost() HouseBlend cost() DarkRoast cost() Decaf cost() Espresso With inheritance on your brain, you may add condiments to this design in one of two ways 1. One subclass per combination of condiment ( wont work in general but especially not in Boulder! ) 2. Add condiment handling to the Beverage superclass 6
Background image of page 6
Approach One: One Subclass per Combination getDescription() cost() description Beverage «Abstract» cost() HouseBlend cost() DarkRoast cost() Decaf cost() Espresso cost() HouseBlendWithSteamedMilkandMocha cost() HouseBlendWithSoyandMocha cost() EspressoWithSoyAndMocha cost() DecafWithWhipandSoy This is incomplete, but you can see the problem… (see page 81 for a more complete picture) 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Approach Two: Let Beverage Handle Condiments getDescription()
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/11/2010 for the course CSCI 5448 taught by Professor Anderson during the Fall '09 term at Colorado.

Page1 / 39

18-decoratorfactory - Decorator and Factory Kenneth M....

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online