Documentacionandroid

Páginas: 28 (6894 palabras) Publicado: 15 de octubre de 2012
3 the DecoratorPattern

g Decorating

h

Objects g

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 usinga form of object composition. Why? Once you 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’veseen one
on your local corner, look across the 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

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

Beverage
description

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

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

DarkRoast

HouseBlend
cost()

cost()

Decafcost()

Espresso
cost()

Each subclass implements cost() to return the 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 thembuilt into their order system.
Here’s their first attempt...
Beverage
description
getDescription()
cost()
// Other useful methods...

DarkRoastWithSteamedMilk
andMocha

HouseBlendWithSteamedMilk
andMocha
HouseBlendWithSteamedMilk
cost()
andCaramel
HouseBlendWithWhipandMocha

cost()

cost()

cost()

DecafWithSteamedMilk
andCaramel

DarkRoastWithSteamedMilk
andCaramelcost()

HouseBlendWithMocha
cost()
HouseBlendWithSoyandMocha

EspressoWithSteamedMilk
andMocha

DecafWithSteamedMilk
andMocha

cost()
DarkRoastWithWhipandMocha

cost()

EspressoWithSteamedMilk
andCaramel

EspressoWithWhipandMocha

cost()

DecafWithWhipandMocha
cost()

cost()

HouseBlendWithSteamedMilk
cost()
andSoy
cost()

HouseBlendWithWhip

cost()

cost()HouseBlendWithSoy
cost()

DarkRoastWithSteamedMilk
andSoy

HouseBlendWithSteamedMilk
cost()
cost()

cost()
DarkRoastWithSteamedMilk

cost()
HouseBlendWithSteamedMilk
andWhip

cost()
HouseBlendWithWhipandSoy

cost()

EspressoWithSteamedMilk
DecafWithSoy
andSoy
DecafWithSteamedMilk
cost()
andSoy EspressoWithSteamedMilk
cost()

DarkRoastWithSoy
cost()DecafWithSteamedMilk
cost()
DecafWithSoyandMocha
DarkRoastWithSoy
DarkRoastWithSoyandMocha
cost()
DecafWithSoy
cost()
DecafWithSoyandMocha
cost()
EspressoWhip
cost()
cost()

cost()

DecafWithWhip
cost()

DarkRoastWithWhip

cost()

cost()

EspressoWithSteamedMilk
andWhip
DecafWithSteamedMilk
DarkRoastWithSteamedMilk
cost()
andWhip
andWhip
EspressoWithWhipandSoy
DecafWithWhipandSoycost()
DarkRoastWithWhipandSoy
cost()
cost()

cost()

EspressoWithMocha

cost()
DecafWithMocha

cost()

DarkRoastWithMocha

cost()
cost()

Whoa!
Can you say
“class explosion?”

cost()

mputes the
Each cost method coalong with the
cost of the coffee the order.
other condiments in

you are here 4

81

violating design principles

A

brain
power

It’s pretty...
Leer documento completo

Regístrate para leer el documento completo.

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS