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
|
class NoCommand(Command):
def execute(self):
print('Command Not Found')
def undo(self):
print('Command Not Found')
class Invoker:
"""
The Invoker is associated with one or several commands. It sends a request
to the command.
"""
"""
Initialize commands.
"""
def __init__(self) -> None:
self.on_commands = [NoCommand() for i in range(7)]
self.off_commands = [NoCommand() for i in range(7)]
self.undo_command = None
def set_command(self, slot, on_command, off_command):
self.on_commands[slot] = on_command
self.off_commands[slot] = off_command
def on_button_was_pressed(self, slot):
command = self.on_commands[slot]
command.execute()
self.undo_command = command
def off_button_was_pressed(self, slot):
command = self.off_commands[slot]
command.execute()
self.undo_command = command
def undo_button_was_pressed(self):
self.undo_command.undo()
def __str__(self):
for i in range(7):
print('[slot %d] %s %s' % (i,
self.on_commands[i].__class__.__name__,
self.off_commands[i].__class__.__name__))
return ''
|