-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-06.rb
163 lines (126 loc) · 2.85 KB
/
06-06.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Command Interface
class Command
def execute
raise NotImplementedError
end
end
class Light
attr_reader :location
def initialize(location)
@location = location
end
def on
puts "#{@location} light is on"
end
def off
puts "#{@location} 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 Stereo
attr_reader :location
def initialize(location)
@location = location
end
def on
puts "#{@location} stereo is on"
end
def off
puts "#{@location} stereo is off"
end
def set_cd
puts "#{@location} stereo is set for CD input"
end
def set_dvd
puts "#{@location} stereo is set for DVD input"
end
def set_radio
puts "#{@location} stereo is set for Radio"
end
def set_volume(volume)
puts "#{@location} stereo volume set to #{volume}"
end
end
class StereoOnWithCDCommand < Command
attr_reader :stereo
def initialize(stereo)
@stereo = stereo
end
def execute
@stereo.on
@stereo.set_cd
@stereo.set_volume(11)
end
end
class StereotOffCommand < Command
attr_reader :stereo
def initialize(stereo)
@stereo = stereo
end
def execute
@stereo.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
living_room_light = Light.new("living room")
living_room_light_on = LightOnCommand.new(living_room_light)
living_room_light_off = LightOffCommand.new(living_room_light)
kitchen_light = Light.new("kitchen")
kitchen_light_on = LightOnCommand.new(kitchen_light)
kitchen_light_off = LightOffCommand.new(kitchen_light)
living_room_stereo = Stereo.new("living room")
living_room_stereo_on = StereoOnWithCDCommand.new(living_room_stereo)
living_room_stereo_off = StereotOffCommand.new(living_room_stereo)
remote = RemoteControl.new
remote.set_command(living_room_light_on, living_room_light_off)
remote.set_command(kitchen_light_on, kitchen_light_off)
remote.set_command(living_room_stereo_on, living_room_stereo_off)
remote.on_button_was_pushed(0)
remote.off_button_was_pushed(0)
remote.on_button_was_pushed(1)
remote.off_button_was_pushed(1)
remote.on_button_was_pushed(2)
remote.off_button_was_pushed(2)