Professional Documents
Culture Documents
Lecture w4 01 PDF
Lecture w4 01 PDF
Goals
cost () is abstract:
• description variable will be
• every subclass has its set in each subclass
• maintenance headache
• include new methods; add set & get for the new condiment
• adding new beverages might inherit features (condiments) not suitable for them
• how to deal with double/triple amount of a specific condiment (e.g., 2 milks) in the
same order
The inheritance problem
How?
o n
l osi
xp
s e
la s
C
Coffee Shop Design 2
l a ss
p erc
e su e
t h riat s
i n p s e
l it y p ro las
i o na t ap ubc
n ct s no he s
d fu at i of t on
A d t h e a t i
o m f ic
o r s o di
f r m
f o
ib l e
l e x
Inf
Apply the Decorator Pattern
• Mocha is a decorator
cost()
cost()
DarkRoast
Mocha
Whip
• so it has cost()
Step-by-Step (4)
4. Compute Cost
add behavior
to it dynamically
concrete components
decorator
concrete decorators
Apply Decorator on the
Coffee Shop Ordering System
abstract component
concrete components
decorator
concrete decorators
Inheritance & Composition
• The decorator (CondimentDecorator) has two relation with the
component (Beverage)