"""
装饰模式:
在不修改原有功能的基础上很容易的添加新的功能
新功能的添加不影响原有功能的使用
能够把核心功能和装饰功能分开,去除装饰功能的重复逻辑
"""
from abc import ABCMeta
from abc import abstractmethod
class Human(object):
__metaclass__ = ABCMeta
def __init__(self, name=None):
if name:
self.name = name
@abstractmethod
def show(self):
pass
class 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()
装饰模式(python)
最新推荐文章于 2022-07-26 22:22:29 发布