#MOC ## 例 - コーヒーショップの注文システム - コーヒーにいくつかの追加オプション(例えば、ミルク、シュガー、ホイップクリームなど)がある - 追加オプションは、コーヒーのコストに影響を及ぼす ```mermaid classDiagram Coffee <|.. SimpleCoffee Coffee <|.. MilkCoffee Coffee <|.. WhipCoffee MilkCoffee o-- Coffee WhipCoffee o-- Coffee class Coffee{ +getCost()(abstract) +getDescription()(abstract) } class SimpleCoffee{ +getCost() +getDescription() } class MilkCoffee{ -coffee +getCost() +getDescription() } class WhipCoffee{ -coffee +getCost() +getDescription() } ``` ```php interface Coffee { public function getCost(); public function getDescription(); } class SimpleCoffee implements Coffee { public function getCost() { return 10; } public function getDescription() { return 'Simple coffee'; } } class MilkCoffee implements Coffee { protected $coffee; public function __construct(Coffee $coffee) { $this->coffee = $coffee; } public function getCost() { return $this->coffee->getCost() + 2; } public function getDescription() { return $this->coffee->getDescription() . ', milk'; } } class WhipCoffee implements Coffee { protected $coffee; public function __construct(Coffee $coffee) { $this->coffee = $coffee; } public function getCost() { return $this->coffee->getCost() + 5; } public function getDescription() { return $this->coffee->getDescription() . ', whip'; } } // Client code $someCoffee = new SimpleCoffee(); echo $someCoffee->getCost(); // output: 10 echo $someCoffee->getDescription(); // output: Simple coffee $someCoffee = new MilkCoffee($someCoffee); echo $someCoffee->getCost(); // output: 12 echo $someCoffee->getDescription(); // output: Simple coffee, milk $someCoffee = new WhipCoffee($someCoffee); echo $someCoffee->getCost(); // output: 17 echo $someCoffee->getDescription(); // output: Simple coffee, milk, whip ``` ## 📚ドキュメント - [12. Decorator パターン | TECHSCORE(テックスコア)](https://www.techscore.com/tech/DesignPattern/Decorator) ## 📖ノウハウ ## 💁トラブルシューティング