- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
Latest commit
67 lines (51 loc) · 1.33 KB
/
Copy pathcommand.py
File metadata and controls
67 lines (51 loc) · 1.33 KB
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
fromabcimportABC, abstractmethod
# Receiver
classLight:
defturn_on(self):
print("The light is on")
defturn_off(self):
print("The light is off")
# Command Interface
classCommand(ABC):
@abstractmethod
defexecute(self):
pass
# Concrete Command for turning on the light
classLightOnCommand(Command):
def__init__(self, light):
self.light=light
defexecute(self):
self.light.turn_on()
# Concrete Command for turning off the light
classLightOffCommand(Command):
def__init__(self, light):
self.light=light
defexecute(self):
self.light.turn_off()
# Invoker
classRemoteControl:
def__init__(self):
self.command=None
defset_command(self, command):
self.command=command
defpress_button(self):
self.command.execute()
# Client code
defclient_code():
light=Light()
light_on=LightOnCommand(light)
light_off=LightOffCommand(light)
remote=RemoteControl()
remote.set_command(light_on)
print("Client: Turning the light on.")
remote.press_button()
remote.set_command(light_off)
print("Client: Turning the light off.")
remote.press_button()
# Usage
client_code()
## Output
# Client: Turning the light on.
# The light is on
# Client: Turning the light off.
# The light is off