装饰模式(python)

来源:互联网 发布:淘宝刷単平台 编辑:程序博客网 时间:2024/09/21 06:42
"""装饰模式:在不修改原有功能的基础上很容易的添加新的功能新功能的添加不影响原有功能的使用能够把核心功能和装饰功能分开,去除装饰功能的重复逻辑"""from abc import ABCMetafrom abc import abstractmethodclass Human(object):    __metaclass__ = ABCMeta    def __init__(self, name=None):        if name:            self.name = name    @abstractmethod    def show(self):        passclass Chinese(Human):    def __init__(self, name):        super(Chinese, self).__init__(name)    def show(self):        print(self.name)class Clothes(Human):    def __init__(self):        super(Clothes, self).__init__()        self.people = None    def get_people(self, people):        self.people = people    def show(self):        self.people.show()class Hat(Clothes):    def __init__(self):        super(Hat, self).__init__()    def show(self):        super(Hat, self).show()        # self.people.show()        print("Hat")class Shoes(Clothes):    def __init__(self):        super(Shoes, self).__init__()    def show(self):        super(Shoes, self).show()        print("Shoes")if __name__ == '__main__':    chinese = Chinese("zzh")    chinese.show()    print("")    hat = Hat()    hat.get_people(chinese)    hat.show()    print("")    shoes = Shoes()    shoes.get_people(hat)    shoes.show()    print("")    chinese.show()
0 0
原创粉丝点击