责任链模式

 

 

 

class Manager(object):
    # 设置管理者的上级
    def set_superior(self,successor):
        self.successor = successor
    def request(self,req):
        pass
class CommonManager(Manager):
    def request(self,req):
        if req > 0 and req < 3:
            print("%s 经理处理"%req)
        else:
            return self.successor.request(req)
class Majordomo(Manager):
    def request(self, req):
        if req > 3 and req < 10:
            print("%s 总监处理" %req)
        else:
            return self.successor.request(req)
class GeneralManager(Manager):
    def request(self, req):
        if req >= 10:
            print("%s 总经理处理" %req)
if __name__ == '__main__':
    h1 = CommonManager()
    h2 = Majordomo()
    h3 = GeneralManager()
    # 设置责任链
    h1.set_superior(h2)
    h2.set_superior(h3)
    # 提交申请
    h1.request(2)
    h1.request(20)
    h1.request(7)

 

 

posted @ 2017-06-18 18:33  跟随心走  阅读(105)  评论(0编辑  收藏  举报