-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.py
48 lines (30 loc) · 861 Bytes
/
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
from abc import ABC, abstractmethod
class Light:
def turn_on(self):
print("Light ON")
def turn_off(self):
print("Light OFF")
class Command(ABC):
@abstractmethod
def execute(self):
pass
class TurnOnCommand(Command):
def __init__(self, light: Light):
self.light = light
def execute(self):
self.light.turn_on()
class TurnOffCommand(Command):
def __init__(self, light: Light):
self.light = light
def execute(self):
self.light.turn_off()
class RemoteControl:
def submit(self, command: Command):
command.execute()
if __name__ == "__main__":
light = Light()
turn_on_command = TurnOnCommand(light)
turn_off_command = TurnOffCommand(light)
remote = RemoteControl()
remote.submit(turn_on_command)
remote.submit(turn_off_command)