forked from dancergraham/HeadFirstDesignPatterns_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_undo.py
125 lines (90 loc) · 3.02 KB
/
remote_undo.py
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
import abc
class Command(abc.ABC):
def execute(self):
raise NotImplementedError
def undo(self):
raise NotImplementedError
class NoCommand(Command):
def execute(self):
pass
def undo(self):
pass
class LightOnCommand(Command):
def __init__(self, light):
self.light = light
def execute(self):
self.level = self.light.get_level()
self.light.on()
def undo(self):
self.light.dim(self.level)
class LightOffCommand(Command):
def __init__(self, light):
self.light = light
def execute(self):
self.level = self.light.get_level()
self.light.off()
def undo(self):
self.light.dim(self.level)
class Light:
def __init__(self, location: str):
self.location = location
self.level = 0
def on(self):
self.level = 100
print("Light is on")
def off(self):
self.level = 0
print("Light is off")
def dim(self, level):
self.level = level
if self.level == 0:
self.off()
else:
print(f"Light is dimmed to {self.level}%")
def get_level(self):
return self.level
class RemoteControlWithUndo:
"""The Invoker"""
def __init__(self) -> None:
self.on_commands = [NoCommand() for _ in range(7)]
self.off_commands = [NoCommand() for _ in range(7)]
self.undo_command = NoCommand()
def set_command(self, i, on_command, off_command):
self.on_commands[i] = on_command
self.off_commands[i] = off_command
def on_button_was_pushed(self, i):
self.on_commands[i].execute()
self.undo_command = self.on_commands[i]
def off_button_was_pushed(self, i):
self.off_commands[i].execute()
self.undo_command = self.off_commands[i]
def undo_button_was_pushed(self):
self.undo_command.undo()
def __str__(self) -> str:
buffer = []
buffer.append("\n------ Remote Control -------\n")
for i, (on_command, off_command) in enumerate(
zip(self.on_commands, self.off_commands)
):
buffer.append(
f"[slot {i}] {on_command.__class__.__name__}"
+ f" {off_command.__class__.__name__}\n"
)
buffer.append(f"[undo] {self.undo_command.__class__.__name__}\n")
return "".join(buffer)
def remote_loader():
remoteControl = RemoteControlWithUndo()
living_room_light = Light("Living Room")
living_room_light_on = LightOnCommand(living_room_light)
living_room_light_off = LightOffCommand(living_room_light)
remoteControl.set_command(0, living_room_light_on, living_room_light_off)
remoteControl.on_button_was_pushed(0)
remoteControl.off_button_was_pushed(0)
print(remoteControl)
remoteControl.undo_button_was_pushed()
remoteControl.off_button_was_pushed(0)
remoteControl.on_button_was_pushed(0)
print(remoteControl)
remoteControl.undo_button_was_pushed()
if __name__ == "__main__":
remote_loader()