Decorator - 装饰模式

1. 概述ios

 

       若你从事过面向对象开发,实现给一个类或对象增长行为,使用继承机制,这是全部面向对象语言的一个基本特性。若是已经存在的一个类缺乏某些方法,或者需要给方法添加更多的功能(魅力),你也许会仅仅继承这个类来产生一个新类—这创建在额外的代码上。ui

      经过继承一个现有类可使得子类在拥有自身方法的同时还拥有父类的方法。可是这种方法是静态的,用户不能控制增长行为的方式和时机。若是  你但愿改变一个已经初始化的对象的行为,你怎么办?或者,你但愿继承许多类的行为,改怎么办?前一个,只能在于运行时完成,后者显然时可能的,可是可能会致使产生大量的不一样的类—可怕的事情。this

 

2. 问题spa

你如何组织你的代码使其能够容易的添加基本的或者一些不多用到的 特性,而不是直接不额外的代码写在你的类的内部?.net

3. 解决方案3d

 

        装饰器模式: 动态地给一个对象添加一些额外的职责或者行为。就增长功能来讲, Decorator模式相比生成子类更为灵活。code

 

       装饰器模式提供了改变子类的灵活方案。装饰器模式在没必要改变原类文件和使用继承的状况下,动态的扩展一个对象的功能。它是经过建立一个包装对象,也就是装饰来包裹真实的对象。对象

 

       当用于一组子类时,装饰器模式更加有用。若是你拥有一族子类(从一个父类派生而来),你须要在与子类独立使用状况下添加额外的特性,你可使用装饰器模式,以免代码重复和具体子类数量的增长。blog

4. 适用性继承

如下状况使用Decorator模式

1)• 在不影响其余对象的状况下,以动态、透明的方式给单个对象添加职责。

2)• 处理那些能够撤消的职责。

3)• 当不能采用生成子类的方法进行扩充时。一种状况是,可能有大量独立的扩展,

为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增加。

另外一种状况多是由于类定义被隐藏,或类定义不能用于生成子类。

5. 结构

UML图以下



Component是定义一个对象接口,能够给这些对象动态地添加职责。

ConcreteComponent是定义了一个具体的对象,也能够给这个对象添加一些职责。
Decorator,装饰抽象类,继承了Component,从外类来扩展Component类的功能,但对于Component来讲,是无需知道Decorator的存在的。
至于ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能。

要善于变通,若是只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类能够是ConcreteComponent的一个子类。
一样道理,若是只有一个ConcreteDecorator类,那么就没有必要创建一个单独的Decorator类,而能够把Decorator和ConcreteDecorator的责任合并成一个类。

新加入的东西仅仅是为了知足一些只在某种特定状况下才会执行的特殊行为 的须要。而装饰模式却提供了一个很是好的解决方案,它把每一个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,所以,当须要执行特殊行为 时,客户代码就能够在运行时根据须要有选择地、按顺序地使用装饰功能包装对象了。

代码以下

Decorator.h

复制代码
#ifndef _DECORATOR_H_
#define _DECORATOR_H_ //Component抽象类,定义该类对象的接口 class Component { public: virtual ~Component(); virtual void Operation()=0; protected: Component(); }; //ConcreteDecorator:具体的Component对象,能够给该对象动态 添加职责 class ConcreteComponent:public Component { public: ConcreteComponent(); ~ConcreteComponent(); virtual void Operation(); }; //Decorator:装饰抽象类,继承自Component class Decorator:public Component { public: Decorator(Component* com); void SetComponent(Component* com); virtual ~Decorator(); virtual void Operation(); protected: Component* _com; }; //ConcreteDecorator就是具体的装饰对象之一,起到给Component添加职责的功能 class ConcreteDecoratorA:public Decorator { public: ConcreteDecoratorA(Component* com); ~ConcreteDecoratorA(); virtual void Operation(); void AddBehavorA(); }; //ConcreteDecorator就是具体的装饰对象之二,起到给Component添加职责的功能 class ConcreteDecoratorB:public Decorator { public: ConcreteDecoratorB(Component* com); ~ConcreteDecoratorB(); virtual void Operation(); void AddBehavorB(); }; //ConcreteDecorator就是具体的装饰对象之三,起到给Component添加职责的功能 class ConcreteDecoratorC:public Decorator { public: ConcreteDecoratorC(Component* com); ~ConcreteDecoratorC(); virtual void Operation(); void AddBehavorC(); }; //ConcreteDecorator就是具体的装饰对象之四,起到给Component添加职责的功能 class ConcreteDecoratorD:public Decorator { public: ConcreteDecoratorD(Component* com); ~ConcreteDecoratorD(); virtual void Operation(); void AddBehavorD(); }; //只添加一种装饰,则不用抽象出装饰基类 class DecoratorOnlyOne:public Component { public: DecoratorOnlyOne(Component* com); ~DecoratorOnlyOne(); virtual void Operation(); void AddBehavor(); private: Component* _com; }; //若是只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类能够是ConcreteComponent的一个子类。 //#endif
复制代码

Decorator.cpp

复制代码
#include "Decorator.h" #include <iostream> using namespace std; Component::Component() {} Component::~Component() { cout << "~Component" << endl; } ConcreteComponent::ConcreteComponent() {} ConcreteComponent::~ConcreteComponent() { cout << "~ConcreteComponent" << endl; } void ConcreteComponent::Operation() { cout << "原职责:ConcreteComponent::Operation" << endl; } Decorator::Decorator(Component* com) { this->_com = com; } void Decorator::SetComponent(Component* com) { this->_com = com; } Decorator::~Decorator() { cout << "~Decorator" << endl; delete this->_com; this->_com = NULL; } void Decorator::Operation() {} ConcreteDecoratorA::ConcreteDecoratorA(Component* com):Decorator(com) {} ConcreteDecoratorA::~ConcreteDecoratorA() { cout << "~ConcreteDecoratorA" << endl; } void ConcreteDecoratorA::Operation() { this->_com->Operation(); //附加职责A this->AddBehavorA(); } void ConcreteDecoratorA::AddBehavorA() { cout << "附加职责A:ConcreteDecoratorA::AddBehavorA" << endl; } ConcreteDecoratorB::ConcreteDecoratorB(Component* com):Decorator(com) {} ConcreteDecoratorB::~ConcreteDecoratorB() { cout << "~ConcreteDecoratorB" << endl; } void ConcreteDecoratorB::Operation() { this->_com->Operation(); //附加职责B this->AddBehavorB(); } void ConcreteDecoratorB::AddBehavorB() { cout << "附加职责B:ConcreteDecoratorB::AddBehavorB" << endl; } ConcreteDecoratorC::ConcreteDecoratorC(Component* com):Decorator(com) {} ConcreteDecoratorC::~ConcreteDecoratorC() { cout << "~ConcreteDecoratorC" << endl; } void ConcreteDecoratorC::Operation() { this->_com->Operation(); //附加职责C this->AddBehavorC(); } void ConcreteDecoratorC::AddBehavorC() { cout << "附加职责C:ConcreteDecoratorC::AddBehavorC" << endl; } ConcreteDecoratorD::ConcreteDecoratorD(Component* com):Decorator(com) {} ConcreteDecoratorD::~ConcreteDecoratorD() { cout << "~ConcreteDecoratorD" << endl; } void ConcreteDecoratorD::Operation() { this->_com->Operation(); //附加职责D this->AddBehavorD(); } void ConcreteDecoratorD::AddBehavorD() { cout << "附加职责D:ConcreteDecoratorD::AddBehavorD" << endl; } //**************只添加一种修饰****************** DecoratorOnlyOne::DecoratorOnlyOne(Component* com):_com(com) { } DecoratorOnlyOne::~DecoratorOnlyOne() { cout << "~DecoratorOnlyOne" << endl; delete this->_com; this->_com = NULL; } void DecoratorOnlyOne::Operation() { this->_com->Operation(); this->AddBehavor(); } void DecoratorOnlyOne::AddBehavor() { cout << "附加惟一职责:DecoratorOnlyOne::AddBehavor" << endl; }
复制代码

main.cpp

复制代码
#include "Decorator.h" #include <iostream> using namespace std; int main() { Component* pCom = new ConcreteComponent(); //要装饰的对象 Decorator* pDec = NULL; pDec = new ConcreteDecoratorA(pCom); //给装饰对象附加职责A pDec = new ConcreteDecoratorB(pDec); //给装饰对象附加职责B pDec = new ConcreteDecoratorC(pDec); //给装饰对象附加职责C pDec = new ConcreteDecoratorD(pDec); //给装饰对象附加职责D pDec->Operation(); cout << "-------------------------------" << endl; //只添加一种修饰 Component* pCom1 = new ConcreteComponent(); DecoratorOnlyOne* pDec1 = new DecoratorOnlyOne(pCom1); pDec1->Operation(); cout << "-------------------------------" << endl; delete pDec; cout << "-------------------------------" << endl; delete pDec1; return 0; }
复制代码

总结

通常,当系统须要新功能的时候,是向旧的类中添加新的代码。这些新加的代码一般装饰了原有类的核心职责或主要行为,但这种作法的问题在于,它们在主类中加入了新的字段,新的方法和新的逻辑,从而增长了主类的复杂度,而且新加入的东西仅仅是为了知足一些只在某种特定状况下才会执行的特殊行为的须要。而装饰模式却提供了一个很是好的解决方案,它把每一个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,所以,当须要执行特殊行为时,客户代码就能够在运行时根据须要有选择地、按顺序地使用装饰功能包装对象了。

那么装饰模式的优势有:1 把类中的装饰功能从类中搬移去除,这样能够简化原有的类,这样作更大的好处就是有效地把类的核心职责和装饰功能区分开了。并且能够去除相关类中重复的装饰逻辑。

相关文章
相关标签/搜索