装饰者模式

来源:互联网 发布:ipad淘宝卖家 编辑:程序博客网 时间:2024/06/05 08:15

设计理念

动态的将责任附加到对象身上,若要拓展功能,装饰者提供了比继承更有弹性的替代方案。

与继承关系相比,关联关系的主要优势在于不会破坏类的封装性,而且继承是一种耦合度较大的静态关系,无法在程序运行时动态扩展。在软件开发阶段,关联关系虽然不会比继承关系减少编码量,但是到了软件维护阶段,由于关联关系使系统具有较好的松耦合性,因此使得系统更加容易维护。当然,关联关系的缺点是比继承关系要创建更多的对象。

使用装饰模式来实现扩展比继承更加灵活,它以对客户透明的方式动态地给一个对象附加更多的责任。装饰模式可以在不需要创造更多子类的情况下,将对象的功能加以扩展。

装饰者可以在所委托被装饰者的行为之前或之后,加上自己的行为,以达到特定的目的。例如现在有一个奶茶的对象,我们现在想通过拓展一些功能来满足用户的订单,比如加冰,加珍珠的奶茶…如果用组合的方式来实现将会增加过多的冗余且违背了设计模式中的开闭原则,如果在其上通过加装一层一层wapper来拓展功能的话会大大提高代码的可重用性,即所谓的“对拓展开放对修改关闭”。

UML类图

装饰者模式

  • Component: 抽象构件,定义一个对象接口,可以给这些对象动态地添加职责;
  • ConcreteComponent:
    具体构件,定义一个具体的Component,继承自Component,重写了Component类的虚函数;
  • Decorator:
    抽象装饰类,维持一个指向Component对象的指针,该指针指向需要被装饰的对象;并定义一个与Component接口一致的接口;
  • ConcreteDecorator: 具体装饰类,向组件添加职责。

实现代码

#include <iostream>using namespace std;class Component{public:     virtual void Operation() = 0;};class ConcreteComponent : public Component{public:     void Operation()     {          cout<<"I am no decoratored ConcreteComponent"<<endl;     }};class Decorator : public Component{public:     Decorator(Component *pComponent) : m_pComponentObj(pComponent) {}     void Operation()     {          if (m_pComponentObj != NULL)          {               m_pComponentObj->Operation();          }     }protected:     Component *m_pComponentObj;};class ConcreteDecoratorA : public Decorator{public:     ConcreteDecoratorA(Component *pDecorator) : Decorator(pDecorator){}     void Operation()     {          AddedBehavior();          Decorator::Operation();     }     void  AddedBehavior()     {          cout<<"This is added behavior A."<<endl;     }};class ConcreteDecoratorB : public Decorator{public:     ConcreteDecoratorB(Component *pDecorator) : Decorator(pDecorator){}     void Operation()     {          AddedBehavior();          Decorator::Operation();     }     void  AddedBehavior()     {          cout<<"This is added behavior B."<<endl;     }};int main(){     Component *pComponentObj = new ConcreteComponent();     Decorator *pDecoratorAOjb = new ConcreteDecoratorA(pComponentObj);     pDecoratorAOjb->Operation();     cout<<"============================================="<<endl;     Decorator *pDecoratorBOjb = new ConcreteDecoratorB(pComponentObj);     pDecoratorBOjb->Operation();     cout<<"============================================="<<endl;     Decorator *pDecoratorBAOjb = new ConcreteDecoratorB(pDecoratorAOjb);     pDecoratorBAOjb->Operation();     cout<<"============================================="<<endl;     delete pDecoratorBAOjb;     pDecoratorBAOjb = NULL;     delete pDecoratorBOjb;     pDecoratorBOjb = NULL;     delete pDecoratorAOjb;     pDecoratorAOjb = NULL;     delete pComponentObj;     pComponentObj = NULL;}

总结

使用装饰模式来实现扩展比继承更加灵活,它以对客户透明的方式动 态地给一个对象附加更多的责任。装饰模式可以在不需要创造更多子 类的情况下,将对象的功能加以扩展。

装饰模式的主要优点在于可以提供比继承更多的灵活性,可以通过一种动态的 方式来扩展一个对象的功能,并通过使用不同的具体装饰类以及这些装饰类的 排列组合,可以创造出很多不同行为的组合,而且具体构件类与具体装饰类可 以独立变化,用户可以根据需要增加新的具体构件类和具体装饰类;其主要缺 点在于使用装饰模式进行系统设计时将产生很多小对象,而且装饰模式比继承 更加易于出错,排错也很困难,对于多次装饰的对象,调试时寻找错误可能需 要逐级排查,较为烦琐。

参考链接:

http://design-patterns.readthedocs.io/zh_CN/latest/structural_patterns/decorator.html
http://www.jellythink.com/archives/171
http://www.cnblogs.com/god_bless_you/archive/2010/06/10/1755212.html

原创粉丝点击