-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcommand.py
45 lines (30 loc) · 969 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
class Light(object):
def turn_on(self):
print('Включить свет')
def turn_off(self):
print('Выключить свет')
class CommandBase(object):
def execute(self):
raise NotImplementedError()
class LightCommandBase(CommandBase):
def __init__(self, light):
self.light = light
class TurnOnLightCommand(LightCommandBase):
def execute(self):
self.light.turn_on()
class TurnOffLightCommand(LightCommandBase):
def execute(self):
self.light.turn_off()
class Switch(object):
def __init__(self, on_cmd, off_cmd):
self.on_cmd = on_cmd
self.off_cmd = off_cmd
def on(self):
self.on_cmd.execute()
def off(self):
self.off_cmd.execute()
light = Light()
switch = Switch(on_cmd=TurnOnLightCommand(light),
off_cmd=TurnOffLightCommand(light))
switch.on() # Включить свет
switch.off() # Выключить свет