美文网首页
Python责任链模式

Python责任链模式

作者: 虾想家 | 来源:发表于2017-03-19 14:18 被阅读25次

责任链模式,子对象都有一个父对象,请求从一级一级从下向顶级父对象传递。

class One(object):
    def __init__(self, name, next=None):
        super().__init__()
        self.name = name
        self.next = next

    def do(self):
        print(self.name, "do")
        self.top()

    def top(self):
        if self.next:
            self.next.do()


def main():
    one_a = One("a")
    one_b = One("b", one_a)
    one_c = One("c", one_b)
    one_c.do()


if __name__ == '__main__':
    main()

相关文章

网友评论

      本文标题:Python责任链模式

      本文链接:https://www.haomeiwen.com/subject/cnbmnttx.html