- Notifications
You must be signed in to change notification settings - Fork 42
/
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
importabc
classCommand(abc.ABC):
defexecute(self):
raiseNotImplementedError
defundo(self):
raiseNotImplementedError
classNoCommand(Command):
defexecute(self):
pass
defundo(self):
pass
classLightOnCommand(Command):
def__init__(self, light):
self.light=light
defexecute(self):
self.level=self.light.get_level()
self.light.on()
defundo(self):
self.light.dim(self.level)
classLightOffCommand(Command):
def__init__(self, light):
self.light=light
defexecute(self):
self.level=self.light.get_level()
self.light.off()
defundo(self):
self.light.dim(self.level)
classLight:
def__init__(self, location: str):
self.location=location
self.level=0
defon(self):
self.level=100
print("Light is on")
defoff(self):
self.level=0
print("Light is off")
defdim(self, level):
self.level=level
ifself.level==0:
self.off()
else:
print(f"Light is dimmed to {self.level}%")
defget_level(self):
returnself.level
classRemoteControlWithUndo:
"""The Invoker"""
def__init__(self) ->None:
self.on_commands= [NoCommand() for_inrange(7)]
self.off_commands= [NoCommand() for_inrange(7)]
self.undo_command=NoCommand()
defset_command(self, i, on_command, off_command):
self.on_commands[i] =on_command
self.off_commands[i] =off_command
defon_button_was_pushed(self, i):
self.on_commands[i].execute()
self.undo_command=self.on_commands[i]
defoff_button_was_pushed(self, i):
self.off_commands[i].execute()
self.undo_command=self.off_commands[i]
defundo_button_was_pushed(self):
self.undo_command.undo()
def__str__(self) ->str:
buffer= []
buffer.append("\n------ Remote Control -------\n")
fori, (on_command, off_command) inenumerate(
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)
defremote_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()