示例
from abc import ABCMeta, abstractmethod# ----------------------------------------------# 抽象处理者(Handler)# ----------------------------------------------class Handler(metaclass=ABCMeta): @abstractmethod def handle_leave(self, day): pass# ----------------------------------------------# 具体处理者(ConcreteHandler)# ----------------------------------------------class GeneralManager(Handler): def handle_leave(self, day): if day <= 10: print(f'公司总经理准假:{day} 天') else: print(f'公司总经理不准假,请走辞职流程!')class DepartmentManager(Handler): def __init__(self): self.next = GeneralManager() def handle_leave(self, day): if day <= 5: print(f'部门经理准假:{day} 天') else: print(f'部门经理权限不足!') self.next.handle_leave(day)class ProjectDirector(Handler): def __init__(self): self.next = DepartmentManager() def handle_leave(self, day): if day <= 5: print(f'项目经理准假:{day} 天') else: print(f'项目经理权限不足!') self.next.handle_leave(day)# ----------------------------------------------# client# ----------------------------------------------day = 12hc = ProjectDirector()hc.handle_leave(day)运行结果
