大话设计模式(Python版)--装饰器模式

第一版

#!/usr/bin/env python

class Person:
    def __init__(self, name):
        self.__name = name
    def WearTShits(self):
        print("Big T-Shits")
    def WearBigTrouser(self):
        print("Big Trouser")
    def WearSneakers(self):
        print("Seankers")
    def WearSuit(self):
        print("Suit")
    def WearTie(self):
        print("Tie")
    def WearLeatherShoes(self):
        print("Shoes")
    def Show(self):
        print("装扮的",self.__name)

if __name__ == '__main__':
    xc = Person('小菜')
    print('第一种装扮: ')
    xc.WearTShits()
    xc.WearBigTrouser()
    xc.WearSneakers()
    xc.Show()
    print('第二种装扮: ')
    xc.WearSuit()
    xc.WearTie()
    xc.WearLeatherShoes()
    xc.Show()
    

第二版

#!/usr/bin/env python
#import abc
from abc import ABCMeta,abstractmethod
class Person:
    def __init__(self,name):
        self.__name = name
    def Show(self):
        print("装扮的",self.__name)

class Finery:
    __metaclass__ = ABCMeta
    @abstractmethod
    def Show(self):
        pass

class TShits(Finery):
    def Show(self):
        print("大T恤")

class BigTrouser(Finery):
    def Show(self):
        print("垮裤")

class Sneakers(Finery):
    def Show(self):
        print("破球鞋")

class Suit(Finery):
    def Show(self):
        print("西装")

class Tie(Finery):
    def Show(self):
        print("领带")

class LeatherShoes(Finery):
    def Show(self):
        print("皮鞋")

if __name__ == '__main__':
    xc = Person('小菜')
    print('第一种装扮:')
    dtx = TShits()
    kk = BigTrouser()
    pqx = Sneakers()
    dtx.Show()
    kk.Show()
    pqx.Show()
    xc.Show()
    print('第二种装扮:')
    xz = Suit()
    ld = Tie()
    px = LeatherShoes()
    xz.Show()
    ld.Show()
    px.Show()
    xc.Show()

第三版

#!/usr/bin/env python
import abc

class Component:
    __metaclass__=abc.ABCMeta
    @abc.abstractmethod
    def Operation(self):
        pass

class ConcreteComponent(Component):
    def Operation(self):
        print("具体对象的操作")

class Decorator(Component):
    def __init__(self):
        self._component = Component()
    def SetComponent(self,component):
        self._component = component
    def Operation(self):
        if(self._component != None):
            self._component.Operation()            

class ConcreteDecoratorA(Decorator):
    def __init__(self):
        self.__addedState = None
    def Operation(self):
        super(ConcreteDecoratorA,self).Operation()
        self.__addedState = "New State"
        print("具体装饰对象A的操作")

class ConcreteDecoratorB(Decorator):
    def Operation(self):
        super(ConcreteDecoratorB,self).Operation()
        self.AddedBehavior()
        print("具体装饰对象A的操作")
    def AddedBehavior(self):
        pass

if __name__ == '__main__':
    c = ConcreteComponent()
    d1 = ConcreteDecoratorA()
    d2 = ConcreteDecoratorB()
    d1.SetComponent(c)
    d2.SetComponent(d1)
    d2.Operation()
              
         

第四版:

#!/usr/bin/env python
import abc

class Person:
    def __init__(self):
        pass
    def __init__(self,name):
        self.__name = name
    @abc.abstractmethod
    def Show(self):
        print("装扮的",self.__name)

class Finery(Person):
    def __init__(self):
        self._component = super(Finery,self).__init__(self)
    def Decorate(self,component):
        self._component = component
    def Show(self):
        if(self._component != None):
            self._component.Show()

class TShits(Finery):
    def Show(self):
        print("大T恤")
        super(TShits,self).Show()

class BigTrouser(Finery):
    def Show(self):
        print("垮裤")
        super(BigTrouser,self).Show()

class Sneakers(Finery):
    def Show(self):
        print("破球鞋")
        super(Sneakers,self).Show()
        
class Suit(Finery):
    def Show(self):
        print("西装")
        super(Suit,self).Show()

class Tie(Finery):
    def Show(self):
        print("领带")
        super(Tie,self).Show()
        
class LeatherShoes(Finery):
    def Show(self):
        print("皮鞋")
        super(LeatherShoes,self).Show()


if __name__ == '__main__':
    xc = Person("小菜")
    print("第一种装扮:")
    pqx = Sneakers()
    kk = BigTrouser()
    dtx = TShits()
    pqx.Decorate(xc)
    kk.Decorate(pqx)
    dtx.Decorate(kk)
    dtx.Show()
    print("第二种装扮:")
    dtx.Decorate(xc)
    kk.Decorate(dtx)
    pqx.Decorate(kk)
    pqx.Show()   
       


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值