Skip to content

Commit 2ddfdba

Browse files
committed
Implemented remote control with lights and undo
1 parent 495ff6a commit 2ddfdba

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

chapter06_command/remote_undo.py

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, light):
2222
self.light = light
2323

2424
def execute(self):
25-
self.level = self.light.getLevel()
25+
self.level = self.light.get_level()
2626
self.light.on()
2727

2828
def undo(self):
@@ -34,7 +34,7 @@ def __init__(self, light):
3434
self.light = light
3535

3636
def execute(self):
37-
self.level = self.light.getLevel()
37+
self.level = self.light.get_level()
3838
self.light.off()
3939

4040
def undo(self):
@@ -44,6 +44,7 @@ def undo(self):
4444
class Light():
4545
def __init__(self, location:str):
4646
self.location = location
47+
self.level = 0
4748

4849
def on(self):
4950
self.level = 100
@@ -55,7 +56,7 @@ def off(self):
5556

5657
def dim(self, level):
5758
self.level = level
58-
if level == 0:
59+
if self.level == 0:
5960
self.off()
6061
else:
6162
print(f"Light is dimmed to {self.level}%")
@@ -64,3 +65,56 @@ def get_level(self):
6465
return self.level
6566

6667

68+
class RemoteControlWithUndo():
69+
"""The Invoker"""
70+
71+
def __init__(self) -> None:
72+
self.on_commands = [NoCommand() for _ in range(7)]
73+
self.off_commands = [NoCommand() for _ in range(7)]
74+
self.undo_command = NoCommand()
75+
76+
def set_command(self, i, on_command, off_command):
77+
self.on_commands[i] = on_command
78+
self.off_commands[i] = off_command
79+
80+
def on_button_was_pushed(self, i):
81+
self.on_commands[i].execute()
82+
self.undo_command = self.on_commands[i]
83+
84+
def off_button_was_pushed(self, i):
85+
self.off_commands[i].execute()
86+
self.undo_command = self.off_commands[i]
87+
88+
def undo_button_was_pushed(self):
89+
self.undo_command.undo()
90+
91+
def __str__(self) -> str:
92+
buffer = []
93+
buffer.append("\n------ Remote Control -------\n")
94+
for i, (on_command, off_command) in enumerate(zip(self.on_commands, self.off_commands)):
95+
buffer.append(f"[slot {i}] {on_command.__class__.__name__}" +
96+
f" {off_command.__class__.__name__}\n")
97+
98+
buffer.append(f"[undo] {self.undo_command.__class__.__name__}\n")
99+
return "".join(buffer)
100+
101+
102+
def remote_loader():
103+
remoteControl = RemoteControlWithUndo()
104+
living_room_light = Light("Living Room")
105+
living_room_light_on = LightOnCommand(living_room_light)
106+
living_room_light_off = LightOffCommand(living_room_light)
107+
108+
remoteControl.set_command(0, living_room_light_on, living_room_light_off)
109+
110+
remoteControl.on_button_was_pushed(0)
111+
remoteControl.off_button_was_pushed(0)
112+
print(remoteControl)
113+
remoteControl.undo_button_was_pushed()
114+
remoteControl.off_button_was_pushed(0)
115+
remoteControl.on_button_was_pushed(0)
116+
print(remoteControl)
117+
remoteControl.undo_button_was_pushed()
118+
119+
if __name__ == '__main__':
120+
remote_loader()

0 commit comments

Comments
 (0)