Patrones de diseño

Páginas: 27 (6622 palabras) Publicado: 14 de marzo de 2011
3

Decorating Objects
I used to think real men subclassed everything. That was until I learned the power of extension at runtime, rather than at compile time. Now look at me!

Just call this chapter “Design Eye for the Inheritance Guy.”
We’ll re-examine the typical overuse of inheritance and you’ll learn how to decorate your classes at runtime using a form of object composition. Why? Onceyou know the techniques of decorating, you’ll be able to give your (or someone else’s) objects new responsibilities without making any code changes to the underlying classes.

this is a new chapter

79

the starbuzz story

Welcome to Starbuzz Coffee
Starbuzz Coffee has made a name for itself as the fastest growing coffee shop around. If you’ve seen one on your local corner, look acrossthe street; you’ll see another one. Because they’ve grown so quickly, they’re scrambling to update their ordering systems to match their beverage offerings. When they first went into business they designed their classes like this...

stract class, Beverage is an abbeverages subclassed by all ffee shop. offered in the co
Beverage
description

The cost() method is abstract; subclassses need todefine their own implementation.

getDescription() cost() // Other useful methods...

The description instance variable is set in each subclass and holds a description of the beverage, like “Most Excellent Dark Roast”. The getDescription() method returns the description.

HouseBlend
cost()

DarkRoast
cost() cost()

Decaf
cost()

Espresso

Each subclass implements cost() to returnthe cost of the beverage.

80

Chapter 3

the decorator pattern

In addition to your coffee, you can also ask for several condiments like steamed milk, soy, and mocha (otherwise known as chocolate), and have it all topped off with whipped milk. Starbuzz charges a bit for each of these, so they really need to get them built into their order system. Here’s their first attempt...

Beveragedescription getDescription() cost() // Other useful methods...

cost()

HouseBlendWithSteamedMilk andMocha n ndWithSteamedMilk HouseBlendWithSteamedMilk en andCaramel an cost() andCaramel n HouseBlendWithWhipandMocha
cost()

M DarkRoastWithSteamedMilk andMocha
cost()

DecafWithSteamedMilk andMocha

EspressoWithSteamedMilk andMocha
cost()

cost()

dMilk WithStDarkRoastWithSteamedMilk mel andCaramel

DecafWithSteamedMilk l dC Caramel andCaramel

EspressoWithSteamedMilk l dC andCaramel
cost() t()

EspressoWithWhipandMocha
cost()

cost() t()

Mocha Mo HouseBlendWithMocha ouseBlendWithMocha seBlendWithM cost() HouseBlendWithSoyandMocha len n

hW cost() W DarkRoastWithWhipandMocha
cost()

hWhipan Whipan p ndMocha DecafWithWhipandMocha

EspressoWithMocha

Witt HouseBlendWithSteamedMilk andSoy andS cost() dS
cost()

Wit t DarkRoastWithMocha
cost() t()

DecafWithMocha afW cost() W

cost() ost()

HouseBlendW cost() HouseBlendWithSoy dW t()

HouseBlendWithSteamedMilk thS cost() S t()
c cost()

DarkRoastWithSteamedMilk andSoy
cost() dM M DarkRoastWithSteamedMilk

EspressoWithSteamedMilk DecafWithSoy andSoy hSteamed dMilkDecafWithSteamedMilk cost() an Soy ndS ndSoy andSoy EspressoWithSteamedMilk cost() hSteamedMilk SteamedMilk

ip HouseBlendWithWhip

cost()

HouseBlendWithSteamedMilk Bl cost() andWhip p

cost()

DarkRoastWithSoy cost() k DecafWithSteamedMilk cost() DecafWithSoyandMocha DarkRoastWithSoy DarkRoastWithSoyandMocha as s co cost() o DecafWithSoy cost() DecafWithSoyandMocha cafW afW y cost() EspressoWhipcost() cost()
De cost() DecafWithWhip e
cost()

cost() dSoy HouseBlendWithWhipandSoy

WithWhip ithWhip DarkRoastWithWhip
cost()

cost()

cost()

DarkRoastWithWhipandSoy cost()
cost()

SteamedMilk DarkRoastWithSteamedMilk an Whi ndWhip andWhip DecafWithWhipandSoy cost()
cost()

EspressoWithSteamedMilk andWhip fWithSteam d D fWithSt med DecafWithSteamedMilk cost() an andWhip...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Patrones De Diseño
  • Patrones de Diseño
  • patrones de diseño
  • Patrones De Diseño
  • patron de diseño
  • Patrones de Diseño
  • Patrones de Diseño
  • Patrones De Diseño

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS