装饰模式

      装饰模式,是在不改变原类文件和使用继承的情况下,动态的扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。是为已有功能动态加入很多其它功能的一种方式。当系统须要新功能的时候,把每一个要装饰的功能放在单独的类中,并让这个类包装它所装饰的对象,因此,当须要执行特殊行为时,客户代码就能够在执行时依据须要有选择的、按顺序地使用装饰功能包装对象。

装饰模式适用的场景:
  1. 须要动态的给一个对象加入功能,这些功能还能够动态的撤销。
  2. 须要添加一些基本功能的排列组合而产生的很大量的功能。或者因排列的顺序不同产生不同的效果的时候。
装饰模式的长处:
  1. Deorator模式与继承关系的目的都是要扩展对象的功能,可是Decorator能够提供比继承很多其它的灵活性。
  2. 通过使用不同的详细装饰类以及这些装饰类的排列组合,设计师能够创造床非常多不同行为的组合。
  3. 更重要的是,装饰模式把类中的装饰功能从类中搬移去除,这样能够简化原有的类,有效的把类的核心职责和装饰功能区分开了,并且能够去除相关类中反复的装饰逻辑。
装饰模式的缺点:
  1. 这样的比继承更加灵活机动的特性,也同一时候意味着更加多的复杂性。
  2. 装饰模式会导致设计中出现非常多的小类,假设过度使用,会使程序变得非常复杂。


C++代码实现:

Decorator.h
#include "stdafx.h"
#include <iostream>
#include <memory>
using namespace std;

class Component
{
public:
        virtual void Operation() = 0;
};


class ConcreteComponent :public Component
{
        virtual void Operation()
       {
              cout << "详细对象的操作!" << endl;
       }
};

class Decorator :public Component
{
private:
        shared_ptr<Component > _pComponent;
protected:
        shared_ptr<Component > GetComponent()
       {
               return _pComponent;
       }
       
public:
        void SetComponent( shared_ptr<Component > pComponent )
       {
              _pComponent = pComponent;
       }


        virtual void Operation()
       {
               if (_pComponent != NULL)
              {
                     _pComponent->Operation();
              }
       }
};

class ConcreteDecorator1 :public Decorator
{
public:
        virtual void Operation()
       {
               //首先执行原Component的方法,再执行本类的方法
              GetComponent()->Operation();
              cout << "详细装饰对象ConcreteDecorator1" << endl;
       }

};

class ConcreteDecorator2 :public Decorator
{
public:
        virtual void Operation()
       {
              GetComponent()->Operation();
              cout << "详细装饰对象ConcreteDecorator2" << endl;
       }

};
// DecorateMethod.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Decorator.h"


int _tmain (int argc , _TCHAR * argv [])
{

        //装饰的方法是,首先建造ConcreteComponent对象,
        //然后建造ConcreteDecorator1来装饰ConcreteComponent
        //借助智能指针来实现.
        //最后运行的是最后的装饰类的Operation
        shared_ptr<ConcreteComponent > pConcreteComponent(new ConcreteComponent);

        shared_ptr<ConcreteDecorator1 > pConcreteDecorator1(new ConcreteDecorator1);
       pConcreteDecorator1->SetComponent(pConcreteComponent);
       pConcreteDecorator1->Operation();
       std::cout << std::endl;

        shared_ptr<ConcreteDecorator2 > pConcreteDecorator2(new ConcreteDecorator2);
       pConcreteDecorator2->SetComponent(pConcreteDecorator1);

       pConcreteDecorator2->Operation();
       getchar();
        return 0;
}



原文地址:https://www.cnblogs.com/gcczhongduan/p/4073910.html