责任链模式(python)

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)

测试:



来自为知笔记(Wiz)


posted on 2021-02-17 22:59  白衣风云  阅读(75)  评论(0编辑  收藏  举报

导航