-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-02.rb
85 lines (66 loc) · 1.24 KB
/
06-02.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Command Interface
class Command
def execute
raise NotImplementedError
end
end
class Light
def on
puts "light is on"
end
def off
puts "light is off"
end
end
class LightOnCommand < Command
attr_reader :light
def initialize(light)
@light = light
end
def execute
@light.on
end
end
class LightOffCommand < Command
attr_reader :light
def initialize(light)
@light = light
end
def execute
@light.off
end
end
class SimpleRemoteControl
attr_reader :slot
def initialize(slot)
@slot = slot
end
def button_was_pressed
@slot.execute
end
end
class RemoteControl
attr_reader :on_commands
attr_reader :off_commands
def initialize
@on_commands = Array.new
@off_commands = Array.new
end
def set_command(on_command, off_command)
@on_commands << on_command
@off_commands << off_command
end
def on_button_was_pushed(index)
@on_commands[index].execute
end
def off_button_was_pushed(index)
@off_commands[index].execute
end
end
light = Light.new
light_on = LightOnCommand.new(light)
light_off = LightOffCommand.new(light)
remote = RemoteControl.new
remote.set_command(light_on, light_off)
remote.on_button_was_pushed(0)
remote.off_button_was_pushed(0)