-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.py
58 lines (39 loc) · 937 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
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
"""
命令模式
"""
from abc import ABCMeta, abstractmethod
class Receiver:
def run(self):
print("run...")
def eat(self):
print("eat...")
class Command:
__metaclass__ = ABCMeta
@abstractmethod
def execute(self):
pass
# 跑命令
class RunCommand(Command):
def __init__(self, receiver):
self.__receiver = receiver
def execute(self):
self.__receiver.run()
# 吃命令
class EatCommand(Command):
def __init__(self, receiver):
self.__receiver = receiver
def execute(self):
self.__receiver.eat()
class Client:
def __init__(self, command):
self.command = command
def exe_cmd(self):
self.command.execute()
if __name__ == "__main__":
recv = Receiver()
run_cmd = RunCommand(recv)
eat_cmd = EatCommand(recv)
ca = Client(run_cmd)
# ca = Client(eat_cmd)
ca.exe_cmd()