-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand-pattern.py
More file actions
89 lines (62 loc) · 1.9 KB
/
Copy pathcommand-pattern.py
File metadata and controls
89 lines (62 loc) · 1.9 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
식당으로 비유하면,
Command Interface -> menu
Concrete Command -> Dish order
Receiver -> Kitchen staff
Waiter -> Invoker
Receiver 에 필요한 로직을 담고, 로직의 조합을 Command에 넣은 후
Client 에서 객체 생성 후 invoke 를 통해서 넣어줌
Invoker는 Command 호출 외 필요한 작업을 수행
"""
from abc import ABC, abstractmethodㄹ
from typing import List
# Command Interface
class Command(ABC):
"""
Command Class Interface
"""
@abstractmethod
def execute(self):
pass
class LightOnCommand(Command):
def __init__(self, light):
self.light: Light = light # Client에서 Receiver를 주입받음
def execute(self):
self.light.turn_on()
class LightOffCommand(Command):
def __init__(self, light):
self.light: Light = light
def execute(self):
self.light.turn_off()
# Receiver, 실제 Logic을 포함하고 있음
class Light:
def __init__(self):
self.status = False
def turn_on(self):
if not self.status:
print("Light is ON")
self.status = True
def turn_off(self):
if self.status:
print("Light is OFF")
self.status = False
# Invoker
# command를 실행시켜주고, history 나 로깅 등 의 기능을 사용할 수 있음
class LightController:
def __init__(self):
self.history: List[str] = []
def execute(self, command):
command.execute()
self.history.append(command)
def main():
# client에서 receiver 객체 생성
light = Light()
com_light_on = LightOnCommand(light=light)
com_light_off = LightOffCommand(light=light)
# Create Invoker
# 하나의 invoker에서 필요한 command를 받아서 수행해주는 느낌
invoker = LightController()
invoker.execute(com_light_on)
invoker.execute(com_light_off)
if __name__ == "__main__":
main()