-
Notifications
You must be signed in to change notification settings - Fork 56
/
command.py
61 lines (43 loc) · 1.34 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
56
57
58
59
60
61
import abc
class Barbecuer(object):
def bake_mutton(self):
print "bake mutton"
def bake_chicken(self):
print "bake chicken"
class Command(object):
__metaclass__ = abc.ABCMeta
def __init__(self, r):
self.receiver = r
@abc.abstractmethod
def execute_command(self):
pass
class BakeMuttonCommand(Command):
def execute_command(self):
self.receiver.bake_mutton()
class BakeChickenCommand(Command):
def execute_command(self):
self.receiver.bake_chicken()
class Waiter(object):
def __init__(self):
self.commands = []
def set_order(self, o):
if isinstance(o, BakeChickenCommand):
print "chicken sold out"
else:
print "add order: " + type(o).__name__
self.commands.append(o)
def cancel_order(self, o):
print "cancel order: " + type(o).__name__
self.commands.remove(o)
def notify(self):
for command in self.commands:
command.execute_command()
if __name__ == "__main__":
barbecuer = Barbecuer()
bake_mutton_command = BakeMuttonCommand(barbecuer)
bake_chicken_command = BakeChickenCommand(barbecuer)
waiter = Waiter()
waiter.set_order(bake_mutton_command)
waiter.set_order(bake_mutton_command)
waiter.set_order(bake_chicken_command)
waiter.notify()