-
Notifications
You must be signed in to change notification settings - Fork 3
/
command.py
123 lines (82 loc) · 2.49 KB
/
command.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
from abc import abstractmethod
# Receivers
class Light:
def on(self):
print("light is on")
def off(self):
print("light is off")
class GarageDoor:
def up(self):
print("garage door is up")
def down(self):
print("garage door is down")
# Command Pattern
class Command:
@abstractmethod
def execute(self):
pass
def undo(self):
pass
class LightOnCommand(Command):
def __init__(self, light):
self.light = light
def execute(self):
self.light.on()
def undo(self):
self.light.off()
class LightOffCommand(Command):
def __init__(self, light):
self.light = light
def execute(self):
self.light.off()
def undo(self):
self.light.on()
class GarageDoorUpCommand(Command):
def __init__(self, garage_door):
self.garage_door = garage_door
def execute(self):
self.garage_door.up()
def undo(self):
self.garage_door.down()
class GarageDoorDownCommand(Command):
def __init__(self, garage_door):
self.garage_door = garage_door
def execute(self):
self.garage_door.down()
def undo(self):
self.garage_door.up()
class NoCommand(Command):
pass
# Invoker
class RemoteControl:
def __init__(self, n_buttons):
self.list_on = [NoCommand()] * n_buttons
self.list_off = [NoCommand()] * n_buttons
self.undo_command = NoCommand()
def set_command(self, index, on_command, off_command):
self.list_on[index] = on_command
self.list_off[index] = off_command
def push_on_button(self, index):
self.list_on[index].execute()
self.undo_command = self.list_on[index]
def push_off_button(self, index):
self.list_off[index].execute()
self.undo_command = self.list_off[index]
def push_undo_button(self):
self.undo_command.undo()
light = Light()
garage_door = GarageDoor()
light_on = LightOnCommand(light)
light_off = LightOffCommand(light)
garage_door_up = GarageDoorUpCommand(garage_door)
garage_door_down = GarageDoorDownCommand(garage_door)
remote_control = RemoteControl(n_buttons=7)
remote_control.set_command(0, light_on, light_off)
remote_control.set_command(1, garage_door_up, garage_door_down)
remote_control.push_on_button(0)
remote_control.push_off_button(0)
remote_control.push_undo_button()
remote_control.push_on_button(1)
remote_control.push_off_button(1)
remote_control.push_undo_button()
remote_control.push_on_button(2)