-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathchain_of_responsibility.py
63 lines (46 loc) · 1.61 KB
/
chain_of_responsibility.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
62
63
import abc
class Request:
def __init__(self, t, n):
self.type = t
self.number = n
class Manager:
__metaclass__ = abc.ABCMeta
def __init__(self, n):
self.name = n
self.superior = None
def set_superior(self, s):
self.superior = s
@abc.abstractmethod
def request_applications(self, r):
"""resolve request"""
class CommonManager(Manager):
def request_applications(self, r):
if r.type == "leave application" and r.number <= 2:
print self.name + " : approve"
else:
self.superior.request_applications(r)
class Majordomo(Manager):
def request_applications(self, r):
if r.type == "leave application" and r.number <= 5:
print self.name + " : approve"
else:
self.superior.request_applications(r)
class GeneralManager(Manager):
def request_applications(self, r):
if r.type == "leave application":
print self.name + " : approve"
elif r.type == "salary increase":
if r.number <= 500:
print self.name + " : approve"
else:
print self.name + " : not approve"
if __name__ == "__main__":
common_manager = CommonManager("JingLi")
majordomo = Majordomo("ZongJian")
general_manager = GeneralManager("ZongJingLi")
common_manager.set_superior(majordomo)
majordomo.set_superior(general_manager)
request = Request("leave application", 4)
common_manager.request_applications(request)
request = Request("salary increase", 1000)
common_manager.request_applications(request)