github地址:https://github.com/cheesezh/python_design_patterns
设计一个控制台程序,可以给人搭配嘻哈风格(T恤,垮裤,运动鞋)或白领风格(西装,领带,皮鞋)的衣服并展示,类似QQ秀那样的。
客户端代码
def main(): hezhang = Person("张贺") print("第一种装扮") hezhang.wear_T_shirts() hezhang.wear_big_trouser() hezhang.wear_sneakers() hezhang.show() print("第二种装扮") hezhang.wear_suit() hezhang.wear_tie() hezhang.wear_leather_shoes() hezhang.show() main() 第一种装扮 T恤 垮裤 运动鞋 装扮的张贺 第二种装扮 西装 领带 皮鞋 装扮的张贺客户端代码
def main(): hezhang = Person("张贺") print("第一种装扮") t_shirts = TShirts() big_trouser = BigTrouser() sneakers = Sneakers() t_shirts.show() big_trouser.show() sneakers.show() hezhang.show() print("第二种装扮") suit = Suit() tie = Tie() leather_shoes = LeatherShoes() suit.show() tie.show() leather_shoes.show() hezhang.show() main() 第一种装扮 T恤 垮裤 运动鞋 装扮的张贺 第二种装扮 西装 领带 皮鞋 装扮的张贺分析以下代码:
hezhang = Person("张贺") t_shirts = TShirts() big_trouser = BigTrouser() sneakers = Sneakers() t_shirts.show() big_trouser.show() sneakers.show() hezhang.show()用自然语言描述就是:
先实例化出hezhang这个人类,准确的说,是没有穿衣服的人类;再实例化并穿上出各种衣服,T恤,垮裤,运动鞋等;再展示出来hezhang;但是服饰和人之间好像没有任何关系,那么如何用代码表示:
实例化没穿衣服的人类为没穿衣服的人类穿上T恤为穿着T恤的人类穿上垮裤为穿着T恤,垮裤的人类穿上运动鞋这需要用到装饰模式。
装饰模式,为了动态的给一个对象添加一些额外的职责,就增加功能而言,装饰模式比生成子类更为灵活[DP]。
装饰模式有以下几个主要组成部分:
组件类Component:定义一个对象接口, 可以给这些对象动态的添加职责;具体组件类ConcretComponent:定义了一个具体对象,也可以给这个对象添加一些职责;装饰类Decorator:装饰抽象类,继承Component,从外类来扩展Component类的功能,对于Component而言,无需知道Decorator的存在;具体装饰类ConcretDecorator:具体装饰类,为Component添加职责用代码表示:
from abc import ABCMeta, abstractmethod class Component(metaclass=ABCMeta): """ 组件类Component:定义一个对象接口, 可以给这些对象动态的添加职责 """ @abstractmethod def operation(self): pass class ConcreteComponent(Component): """ 具体组件类ConcretComponent:定义了一个具体对象,也可以给这个对象添加一些职责 """ def operation(self): print("具体组件的操作") class Decorator(Component): """ 装饰类Decorator:装饰抽象类,继承Component,从外类来扩展Component类的功能,对于Component而言,无需知道Decorator的存在; """ def __init__(self): self.component = None def set_component(self, component): self.component = component def operation(self): if self.component != None: self.component.operation() class ConcreteDecratorA(Decorator): """ 具体装饰类ConcretDecorator:具体装饰类,为Component添加职责 """ def __init__(self): self.added_operation = "A:具体装饰类A独有操作" def operation(self): super().operation() print(self.added_operation) print("A:具体装饰对象A的操作") class ConcreteDecratorB(Decorator): """ 具体装饰类ConcretDecorator:具体装饰类,为Component添加职责 """ def __init__(self): self.added_operation = "B:具体装饰类B独有操作" def operation(self): super().operation() print(self.added_operation) print("B:具体装饰对象B的操作") def main(): component = ConcreteComponent() decorator_a = ConcreteDecratorA() decorator_b = ConcreteDecratorB() decorator_a.set_component(component) decorator_b.set_component(decorator_a) decorator_b.operation() main() 具体组件的操作 A:具体装饰类A独有操作 A:具体装饰对象A的操作 B:具体装饰类B独有操作 B:具体装饰对象B的操作客户端代码
def main(): hezhang = Person("张贺") t_shirts = TShirts() big_trouser = BigTrouser() sneakers = Sneakers() t_shirts.decorate(hezhang) big_trouser.decorate(t_shirts) sneakers.decorate(big_trouser) sneakers.show() main() 运动鞋 垮裤 T恤 装扮的张贺上述客户端代码可以用自然语言描述为:
实例化没穿衣服的人类为没穿衣服的人类穿上T恤为穿着T恤的人类穿上垮裤为穿着T恤,垮裤的人类穿上运动鞋所以,装饰模型是为已有功能动态的添加更多功能的一种方式,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户端代码可以在运行时根据需要有选择的,按顺序的使用装饰功能包装对象。
装饰模式的优点在于,把类中的装饰功能从类中搬移出去,这样可以简化原有的类,有效的把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑,即一个装饰功能可以给多个不同的类使用。
转载于:https://www.cnblogs.com/CheeseZH/p/9374617.html