装饰模式(python)

"""
装饰模式:
在不修改原有功能的基础上很容易的添加新的功能
新功能的添加不影响原有功能的使用
能够把核心功能和装饰功能分开,去除装饰功能的重复逻辑
"""
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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值