zoukankan      html  css  js  c++  java
  • 结构型模式:Decorator——装饰模式

    1、意图

    装饰模式:动态的给一个对象添加一些额外的职责(不重要的功能,只是偶然一次要执行)。就增加功能来说,Decorator模式比使用继承生成子类更为灵活。

         建造过程不稳定,需要把所需的功能按正确的顺序串联起来进行控制。

    2、优缺点分析

    GOOD:当你向旧的类中添加新代码时,一般是为了添加核心职责或主要行为。而当需要加入的仅仅是一些特定情况下才会执行的特定的功能时(简单点就是不是核心应用的功能),就会增加类的复杂度。

    装饰模式就是把要添加的附加功能分别放在单独的类中,并让这个类包含它要装饰的对象,

    当需要执行时,客户端就可以有选择地、按顺序地使用装饰功能包装对象。

    3、UML标准图

    2KC4ZGHDQX@8`U%KS0_Q{L7

    1)Component:定义一个对象接口,可以给这些对象动态的添加职责。

    2)Decorator:装饰抽象类,继承了Component,从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的。

        实际上是维持了一个指向Component的指针,并且有一个和Componet一致的接口函数

    3)ConcreteDecorator:具体的装饰对象,起到给Component添加职责的功能。

    注意:接口函数Operation()是关键,它由Component声明,因此Component的派生类都需要实现,可以在这个接口函数的基础上给它动态的添加职责。

    解释:Decorator的派生类ConcreateComponent类的对象的实现过程是:首先初始化一个ConcreateComponent类对象(被装饰者),采用这个对象去生成一个Decorator对象(装饰者),之后对Operation函数的调用则是对这个Decorator对象成员函数的多态调用。

    要点:Decorator类和ConcreateComponent类都继承自Component,从而两者的接口函数是一致的。

            其次,Decorator维护了一个指向Component的指针,从而可以对Component::Operation函数作多态调用

    变通:

    (1)如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。

    (2)如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类

    4、标准源码

       1: #ifndef DECORATOR_H
       2: #define DECORATOR_H
       3:  
       4: // 抽象基类,定义一个对象接口,可以为这个接口动态的添加职责.
       5: class Component
       6: {
       7: public:
       8:     Component(){}
       9:     virtual ~Component(){}
      10:  
      11:     // 纯虚函数,由派生类实现
      12:     virtual void Operation() = 0;
      13: };
      14:  
      15: // 抽象基类,维护一个指向Component对象的指针
      16: class Decorator
      17:     : public Component
      18: {
      19: public:
      20:     Decorator(Component* pComponent) : m_pComponent(pComponent){}
      21:     virtual ~Decorator();
      22:  
      23: protected:
      24:     Component* m_pComponent;
      25: };
      26:  
      27: // 派生自Component,在这里表示需要给它动态添加职责的类
      28: class ConcreateComponent
      29:     : public Component
      30: {
      31: public:
      32:     ConcreateComponent(){}
      33:     virtual ~ConcreateComponent(){}
      34:  
      35:     virtual void Operation();
      36: };
      37:  
      38: // 派生自Decorator,这里代表为ConcreateComponent动态添加职责的类
      39: class ConcreateDecorator
      40:     : public Decorator
      41: {
      42: public:
      43:     ConcreateDecorator(Component* pComponent) : Decorator(pComponent){}
      44:     virtual ~ConcreateDecorator(){}
      45:  
      46:     virtual void Operation();
      47:  
      48: private:
      49:     void AddedBehavior();
      50: };
      51:  
      52: #endif
       1: #include "Decorator.h"
       2: #include <iostream>
       3:  
       4: Decorator::~Decorator()
       5: {
       6:     delete m_pComponent;
       7:     m_pComponent = NULL;
       8: }
       9:  
      10: void ConcreateComponent::Operation()
      11: {
      12:     std::cout << "Operation of ConcreateComponent\n";
      13: }
      14:  
      15: void ConcreateDecorator::Operation()
      16: {
      17:     m_pComponent->Operation();
      18:     AddedBehavior();
      19: }
      20:  
      21: void ConcreateDecorator::AddedBehavior()
      22: {
      23:     std::cout << "AddedBehavior of ConcreateDecorator\n";
      24: }
       1: #include "Decorator.h"
       2: #include <stdlib.h>
       3:  
       4: int main()
       5: {
       6:     // 初始化一个Component对象
       7:     Component* pComponent = new ConcreateComponent();
       8:     // 采用这个Component对象去初始化一个Decorator对象,
       9:     // 这样就可以为这个Component对象动态添加职责
      10:     Decorator* pDecorator = new ConcreateDecorator(pComponent);
      11:  
      12:     pDecorator->Operation();
      13:  
      14:     delete pDecorator;
      15:  
      16:     system("pause");
      17:  
      18:     return 0;
      19: }
  • 相关阅读:
    opencv实现连通域
    C Tips:显示点阵汉字的小样例
    协方差的意义
    HashTable类模板_C++
    Java实现 蓝桥杯VIP 算法提高 选择排序
    Java实现 蓝桥杯VIP 算法提高 笨小猴
    Java实现 蓝桥杯VIP 算法提高 笨小猴
    Java实现 蓝桥杯VIP 算法提高 笨小猴
    Java实现 蓝桥杯VIP 算法提高 笨小猴
    Java实现 蓝桥杯VIP 算法提高 笨小猴
  • 原文地址:https://www.cnblogs.com/steven_oyj/p/1744688.html
Copyright © 2011-2022 走看看