
飲み物を装飾する
Decoratorパターンを使ってスターバズの飲み物を手直ししましょう。
Beverage beverage
CondimentDeco
rator
getDescription()
cost()
//
他の便利なメソッド
Beverage
description
cost()
getDescription()
Milk
cost()
HouseBlend
コンポーネント
cost()
DarkRoast
cost()
Decaf
cost()
Espresso
Mocha
コーヒーの種類を表す
4
つの具象コンポーネント
コンディメントデコレータ。これらは
cost()
だけでなく
getDescription()
も実装する必要があります。その理
由はすぐに説明します。
Beverage
は抽象コンポーネ
ントクラスの役割を務めます。
getDescription()
Soy Whip
cost()
getDescription()
cost()
getDescription()
cost()
getDescription()
デコレータがラップする
Beverage
への参照
先に進む前に、コーヒーとコンディメントの
cost()
メソッドの実装方法を考えてみましょう。
また、コンディメントの
getDescription()
メソッドの実装方法も考えてみましょう。
92
3
章
飲み物を装飾する