Decorator 装饰对象结构模式

      Decorator 装饰对象结构模式:动态地给一个对象添加一些额外的职责,也称包装器 Wrapper。不过和 Adapter 模式不同,Decorator 模式仅改变对象的职责而不改变接口;而适配器则给对象一个全新的接口。

     问题:要使用的对象将执行所需的基本功能。但是,可能需要为这个对象将添加某些功能,这些附加,功能可能发生在对象的基础功能之前或之后。所以,Decorator 适用于如下情况:1)在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。2)处理那些可以撤消的职责。3)当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。

Decorator 装饰模式的通用结构图如下:

Decorator

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

ConcreteComponent:定义一个对象,可以给这个对象添加一些职责。

Decorator:维持一个指向 Component 对象的指针,并定义一个与 Component 接口一致的接口。

ConcreteDecorator:向组件添加职责。

Decorator 将请求转发给它的 Component 对象,并有可能在转发请求前后执行一些附加的动作。

参与者与协作者:ConcreteComponent 让 Decorator 对象为自己添加功能。有时候用 ConcreteComponent 的派生类提供核心功能,在这种情况下 ConcreteComponent 类就不再具体的,而是抽象的。Component 类定义了所有这些类所使用的接口。

Decorator 装饰模式,示例代码如下:

   1:  //Decorator.h
   2:  #pragma once
   3:   
   4:  #include <iostream>
   5:  #include <string>
   6:   
   7:  // Component: 抽象基类, 定义一个对象接口, 可以为这个接口动态的添加职责.
   8:  class Component
   9:  {
  10:  public:
  11:      Component(){    }
  12:      virtual ~Component(){    }
  13:   
  14:      //
  15:      virtual void Operation() = 0;
  16:  };
  17:   
  18:  // 派生自 Component, 在这里表示需要给它动态添加职责的类
  19:  class ConcreteComponent : public Component
  20:  {
  21:  public:
  22:      ConcreteComponent(){    }
  23:      virtual ~ConcreteComponent(){    }
  24:   
  25:      //
  26:      virtual void Operation()
  27:      {
  28:          std::cout << "Operation of ConcreteComponent" << std::endl;
  29:      }
  30:  };
  31:   
  32:  // Decorator 装饰类,抽象基类, 维护一个指向 Component 对象的指针
  33:  // Decorator 与 Component 是聚合关系
  34:  // 必须定义一个与 Component 接口一致的接口
  35:  // 如果仅需要添加一个职责时,没有必要定义抽象的 Decorator 类,这时可以
  36:  // 把 Decorator 向 Component 转发请求的职责合并到 ConcreteDecorator 中。
  37:  class Decorator : public Component
  38:  {
  39:  public:
  40:      Decorator(Component* pComponent) 
  41:          : m_pComponent(pComponent){    }
  42:      virtual ~Decorator()
  43:      {
  44:          if(m_pComponent)
  45:              delete m_pComponent;
  46:          m_pComponent = NULL;
  47:      }
  48:   
  49:      //
  50:      virtual void Operation()
  51:      {
  52:          if(m_pComponent != NULL)
  53:              m_pComponent->Operation();
  54:      }
  55:   
  56:  protected:
  57:      Component* m_pComponent;
  58:  };
  59:   
  60:  // 派生自 Decorator, 这里代表为 ConcreateComponent 动态添加职责的类
  61:  // Decorator 的子类定义了特殊的装饰功能,即为 Component 添加了特定的功能
  62:  // Decorator 的子类,可以自由添加其它的一些操作及属性
  63:  class ConcreteDecoratorA : public Decorator
  64:  {
  65:  public:
  66:      ConcreteDecoratorA(Component* pComponent, int elem) 
  67:          : Decorator(pComponent), _privateElement(elem)
  68:      {        }
  69:      virtual ~ConcreteDecoratorA(){    }
  70:   
  71:      virtual void Operation()
  72:      {
  73:          m_pComponent->Operation();
  74:          AddedBehavior();
  75:          PrintSelfState();
  76:      }
  77:  private:
  78:      void AddedBehavior()
  79:      {
  80:          std::cout << "AddedBehavior of ConcreteDecoratorA" << std::endl;
  81:      }
  82:   
  83:      // ConcreateDecoratorA 自己添加的操作
  84:      void PrintSelfState() const
  85:      {
  86:          std::cout << "ConcreteDecoratorA::_privateElement= " 
  87:                  << _privateElement << std::endl;
  88:      }
  89:  private:
  90:      int _privateElement;
  91:  };
  92:   
  93:  // ConcreateDecoratorB, Decorator 的子类,可以自由添加其它的一些操作及属性
  94:  class ConcreteDecoratorB : public Decorator
  95:  {
  96:  public:
  97:      ConcreteDecoratorB(Component* pComponent, const std::string strB) 
  98:          : Decorator(pComponent), _strB(strB)
  99:      {        }
 100:      virtual ~ConcreteDecoratorB(){    }
 101:   
 102:      virtual void Operation()
 103:      {
 104:          m_pComponent->Operation();
 105:          AddedBehavior();
 106:          PrintSelfState();
 107:      }
 108:  private:
 109:      void AddedBehavior()
 110:      {
 111:          std::cout << "AddedBehavior of ConcreteDecoratorB" << std::endl;
 112:      }
 113:   
 114:      //ConcreateDecoratorB 自己添加的操作及属性
 115:      void PrintSelfState() const
 116:      {
 117:          std::cout << "ConcreteDecoratorB:_strB= " << _strB << std::endl;
 118:      }
 119:  private:
 120:      std::string _strB;
 121:  };
 122:   
 123:  // ConcreateDecoratorC, Decorator 的子类,可以自由添加其它的一些操作及属性
 124:  class ConcreteDecoratorC : public Decorator
 125:  {
 126:  public:
 127:      ConcreteDecoratorC(Component* pComponent) : Decorator(pComponent){    }
 128:      virtual ~ConcreteDecoratorC(){    }
 129:   
 130:      virtual void Operation()
 131:      {
 132:          m_pComponent->Operation();
 133:          AddedBehavior();
 134:      }
 135:  private:
 136:      void AddedBehavior()
 137:      {
 138:          std::cout << "AddedBehavior of ConcreteDecoratorC" << std::endl;
 139:      }
 140:  };
 141:  //Factory for test
 142:  class Factory
 143:  {
 144:  public:
 145:      //test
 146:      //如下工厂方法返回的为:
 147:      //return (new ConcreteDecoratorC ( new ConcreteDecoratorA (new ConcreteComponent, 3)));
 148:      //可以看出,我们使用 ConcreteDecoratorA 装饰了 ConcreteComponent
 149:      //然后又用 ConcreteDecoratorC 装饰了 ConcreteDecoratorA
 150:      Component* getComponent1()
 151:      {
 152:          Component* myComponent;
 153:          myComponent = new ConcreteDecoratorA(new ConcreteComponent, 3);
 154:          myComponent = new ConcreteDecoratorC(myComponent);
 155:   
 156:          return myComponent;
 157:      }
 158:   
 159:      //test
 160:      //如下工厂方法返回的为:
 161:      //return (new ConcreteDecoratorC ( new ConcreteDecoratorB 
 162:      //        ( new ConcreteDecoratorA (new ConcreteComponent, 3), "test")));
 163:      //可以看出,我们使用 ConcreteDecoratorA 装饰了 ConcreteComponent
 164:      //然后又用 ConcreteDecoratorB 装饰了 ConcreteDecoratorA
 165:      //最后又用 ConcreteDecoratorC 装饰了 ConcreteDecoratorB
 166:      Component* getComponent2()
 167:      {
 168:          Component* myComponent;
 169:          myComponent = new ConcreteDecoratorA(new ConcreteComponent, 3);
 170:          myComponent = new ConcreteDecoratorB(myComponent, "test");
 171:          myComponent = new ConcreteDecoratorC(myComponent);
 172:   
 173:          return myComponent;
 174:      }
 175:  };

//test

   1:  //
   2:  #include "Decorator.h"
   3:  #include <stdlib.h>
   4:   
   5:  int main()
   6:  {
   7:      Factory* myFactory = new Factory;
   8:      Component* myComponent1 = myFactory->getComponent1();
   9:      myComponent1->Operation();
  10:   
  11:      std::cout << std::endl;
  12:   
  13:      Component* myComponent2 = myFactory->getComponent2();
  14:      myComponent2->Operation();
  15:   
  16:      delete myComponent2;
  17:      delete myComponent1;
  18:      delete myFactory;
  19:   
  20:      return EXIT_SUCCESS;
  21:  }
  22:   

Decorator 模式帮助我们将问题分为两部分:
1):如何实现提供新功能的对象.
2):如何为每种特殊情况组织对象.
这样能够将 Decorator 对象的实现与决定如何使用 Decorator 的对象分离开来从而提高了内聚性, 因为每个 Decoroator 对象只用关心自己添加的功能---无需关心自己如何被添加到对象链中.

Decorator 模式要求对象链的实例化与使用它的 Client 对象完全分离开.最典型的实现是通过使用工厂对象,根据某些配置信息实例化对象链。我们上面的简单示例就使用了工厂方法来创建简单的对象链。***

Java 中的 I/O 流就广泛使用了 Decorator 模式。如 ByteArrayInputStream,FileInputStream,FilterInputStream,InputStream,ObjectInputStream,SequenceInputStream 和 StringBufferInputStream 这些类都直接派生自 java.io.InputStream 这些类都扮演装饰对象的角色。所有的装饰都(直接或间接地)派生自 FilterInputStream 类。

发布了54 篇原创文章 · 获赞 3 · 访问量 12万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章