【设计模式】装饰模式
一、什么是装饰模式
定义:允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。
意图:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰器模式相比生成子类更为灵活。
二、装饰模式的结构
Component:定义一个对象接口,可以给这些对象动态地添加职责
ConcreteComponent:定义了一个具体的对象,也可以给这个对象添加一些职责
Decorator:装饰抽象类,继承Component,从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在
ConcreteDecorator:具体的装饰对象,起到给Component添加职责的功能
如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。
三、装饰模式的实例
创建一个接口
class Person: def __init__(self, name): self.name = name def show(self): pass |
创建实现接口的实体类
class OfficeLady(Person): def __init__(self, name): super().__init__(name) def show(self): print("装扮的白领%s女士" % self.name) class OfficeMale(Person): def __init__(self, name): self.name = name def show(self): print("装扮的白领%s男士" % self.name) |
创建实现了Person 接口的装饰类
class Decorator(Person): def __init__(self, person): self.person = person def show(self): self.person.show() |
创建扩展了Decorator类的实体装饰类
class LadiesSuit(Decorator): def __init__(self, person): self.__ladies_suit = '女士西装' super().__init__(person) def show(self): print(self.__ladies_suit) self.person.show() class MenSuit(Decorator): def __init__(self, person): self.__men_suit = '男士西装' super().__init__(person) def show(self): print(self.__men_suit) self.person.show() class HighHeeledShoes(Decorator): def __init__(self, person): self.__high_heeled_shoes = '高跟鞋' super().__init__(person) def show(self): print(self.__high_heeled_shoes) self.person.show() class LeatherShoes(Decorator): def __init__(self, person): self.__leather_shoes = '皮鞋' super().__init__(person) def show(self): print(self.__leather_shoes) self.person.show() |
使用 实体装饰类 来装饰 Person对象
if __name__ == '__main__': lady = OfficeLady('小红') lady_decorator = Decorator(lady) lady_clothes = LadiesSuit(lady_decorator) lady_shoes = HighHeeledShoes(lady_clothes) lady_shoes.show() gentleman = OfficeMale('小明') gentleman_decorator = Decorator(gentleman) gentleman_shoes = LeatherShoes(gentleman_decorator) gentleman_clothes = MenSuit(gentleman_shoes) gentleman_clothes.show() |
三、装饰模式的总结
1、装饰是已有功能动态地添加更多的一种方式。
2、起初的设计中,当系统需要新功能的时候,是向旧的类中添加新的代码。这些新加的代码通常装饰了原有类的核心职责或主要行为。但问题在于,它们在主类中加入了新的字段、新的方法和新的逻辑,从而增加了主类的复杂度,而这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为的需要。而装饰模式却提供了一个非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。
3、装饰模式的优点是把类中的装饰功能从类中搬移去除,这样可以简化原有的类。这样的好处是有效地把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。
以下为复制内容: 1. 何时使用
2. 方法
3. 优点
4. 缺点
5. 使用场景
6. 应用实例
|
装饰器模式 |