-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator.rb
66 lines (54 loc) · 1.01 KB
/
decorator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Design pattern: Decorator
# Description: Attach additional responsibilities to an object dynamically.
# Decorators provide a flexible alternative to subclassing for extending functionality.
# use SimpleDelegator
require 'delegate'
class Coffee
def cost
2
end
end
class Milk < SimpleDelegator
def cost
get_base.cost + 0.4
end
alias get_base __getobj__
end
class Sugar < SimpleDelegator
def cost
get_base.cost + 0.1
end
alias get_base __getobj__
end
coffee = Coffee.new
sugar_coffe = Sugar.new(coffee)
puts sugar_coffe.cost
# => 2.1
milk_coffee = Milk.new(coffee)
puts milk_coffee.cost
# => 2.4
double_milk = Milk.new(milk_coffee)
puts double_milk.cost
# => 2.8
# inspired by https://robots.thoughtbot.com/evaluating-alternative-decorator-implementations-in
<<-EXAMPLE
class Coffee
def cost
2
end
end
module Milk
def cost
super + 0.4
end
end
module Sugar
def cost
super + 0.2
end
end
coffee = Coffee.new
coffee.extend(Milk)
coffee.extend(Sugar)
coffee.cost # 2.6
EXAMPLE