-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommand.py
56 lines (45 loc) · 1.21 KB
/
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
#!/usr/bin/python
# -*- coding : utf-8 -*-
"""
brief:
Command.
Encapsulate a request as an object,thereby
letting you parameterize clients with different requests,
queue or log requests,and support undoable operations.
Invoker => ( Command => Receiver )
|
ConcreteCommand
"""
# Receiver
class Engineer:
def __init__(self, name):
self.name = name
def coding(self):
print("%s is coding." % (self.name))
def meeting(self):
print("%s is in a meeting." % (self.name))
# Command.
class Command:
def __init__(self, receiver):
self.receiver = receiver
class CodingCommand(Command):
def execute(self):
self.receiver.coding()
class MeetingCommand(Command):
def execute(self):
self.receiver.meeting()
# Invoker
class Manager:
def __init__(self):
self.command_stack = []
def add_command(self, command):
self.command_stack.append(command)
def action(self):
for cmd in self.command_stack:
cmd.execute()
if __name__ == '__main__':
engineer = Engineer("Tom")
manager = Manager()
manager.add_command(CodingCommand(engineer))
manager.add_command(MeetingCommand(engineer))
manager.action()