# Decorator Pattern
* 需求:飲料店內有紅茶/奶茶/綠茶
* 解決辦法:繼承/模組
* 造成問題?
Decorator Pattern概念:
裝飾模式是一種結構型設計模式, 允許你通過將對象放入包含行為的特殊封裝對像中來為原對象綁定新的行為。
---

```ruby=
class DrinkShop
def make
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end
end
class BlackTea < DrinkShop
def make
"紅茶"
end
def calculate
price = 25
end
end
class GreenTea < DrinkShop
def make
"綠茶"
end
def calculate
price = 20
end
end
class Ingredients < DrinkShop
attr_accessor :component
def initialize(component)
@component = component
end
def make
@component.make
end
def calculate
@component.calculate
end
end
class TapiocaBall < Ingredients
def make
"珍珠+#{@component.make}"
end
def calculate
@component.calculate + 5
end
end
class KonjacJelly < Ingredients
def make
"寒天+#{@component.make}"
end
def calculate
@component.calculate + 8
end
end
class CoconutJelly < Ingredients
def make
"椰果+#{@component.make}"
end
def calculate
@component.calculate + 7
end
end
black_tea = BlackTea.new
black_tea_T = TapiocaBall.new(black_tea)
black_tea_D_T=TapiocaBall.new(black_tea_T)
black_tea_T_K = KonjacJelly.new(black_tea_T)
black_tea_T_K_C = CoconutJelly.new(black_tea_T_K)
green_tea = GreenTea.new
green_tea_T = TapiocaBall.new(green_tea)
puts "紅茶:#{black_tea.make},價格#{black_tea.calculate}"
puts "珍珠加紅茶:#{black_tea_T.make},價格:#{black_tea_T.calculate}"
puts "雙倍珍珠加紅茶:#{black_tea_D_T.make},價格#{black_tea_D_T.calculate}"
puts "珍珠加寒天加椰果紅茶:#{black_tea_T_K_C.make},價格#{black_tea_T_K_C.calculate}"
puts "------------------------------------------------------------------------"
puts "綠茶:#{green_tea.make},價格#{green_tea.calculate}"
puts "珍珠加綠茶:#{green_tea_T.make},價格:#{green_tea_T.calculate}"
```
[額外補充](https://ithelp.ithome.com.tw/articles/10218692)