rom abc import ABCMeta, abstractmethod
class Handler(metaclass=ABCMeta):
@abstractmethod
def handle_leave(self,day):
pass
class GeneralManager(Handler):
def handle_leave(self,day):
if day <= 10:
print(f'总经理准假{day}天')
else:
print('不予批准!')
class DepartmentManager(Handler):
def __init__(self):
self.next = GeneralManager()
def handle_leave(self, day):
if day <= 5:
print(f'部门经理准假{day}天')
else:
print('权限不足,请求上级审批!!')
self.next.handle_leave(day)
class ProjectDirector(Handler):
def __init__(self):
self.next = DepartmentManager()
def handle_leave(self, day):
if day <= 3:
print(f'项目主管准假{day}天')
else:
print("权限不足,请求上级审批!!")
self.next.handle_leave(day)
测试:
![](https://img2020.cnblogs.com/blog/892740/202102/892740-20210217225913287-1034979592.png)