Decorator装饰模式
作用:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
UML图如下:
Component是定义一个对象接口,可以给这些对象动态地添加职责。
ConcreteComponent是定义了一个具体的对象,也可以给这个对象添加一些职责。
Decorator,装饰抽象类,继承了Component,从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的。
至于ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能。
要善于变通,如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。
同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。
新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为 的需要。而装饰模式却提供了一个非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为 时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。
代码如下:
Decorator.h
1 #ifndef _DECORATOR_H_ 2 #define _DECORATOR_H_ 3 4 //Component抽象类,定义该类对象的接口 5 class Component 6 { 7 public: 8 virtual ~Component(); 9 virtual void Operation()=0; 10 protected: 11 Component(); 12 }; 13 14 //ConcreteDecorator:具体的Component对象,可以给该对象动态 添加职责 15 class ConcreteComponent:public Component 16 { 17 public: 18 ConcreteComponent(); 19 ~ConcreteComponent(); 20 virtual void Operation(); 21 }; 22 23 //Decorator:装饰抽象类,继承自Component 24 class Decorator:public Component 25 { 26 public: 27 Decorator(Component* com); 28 void SetComponent(Component* com); 29 virtual ~Decorator(); 30 virtual void Operation(); 31 protected: 32 Component* _com; 33 }; 34 35 //ConcreteDecorator就是具体的装饰对象之一,起到给Component添加职责的功能 36 class ConcreteDecoratorA:public Decorator 37 { 38 public: 39 ConcreteDecoratorA(Component* com); 40 ~ConcreteDecoratorA(); 41 virtual void Operation(); 42 void AddBehavorA(); 43 }; 44 45 //ConcreteDecorator就是具体的装饰对象之二,起到给Component添加职责的功能 46 class ConcreteDecoratorB:public Decorator 47 { 48 public: 49 ConcreteDecoratorB(Component* com); 50 ~ConcreteDecoratorB(); 51 virtual void Operation(); 52 void AddBehavorB(); 53 }; 54 55 //ConcreteDecorator就是具体的装饰对象之三,起到给Component添加职责的功能 56 class ConcreteDecoratorC:public Decorator 57 { 58 public: 59 ConcreteDecoratorC(Component* com); 60 ~ConcreteDecoratorC(); 61 virtual void Operation(); 62 void AddBehavorC(); 63 }; 64 65 //ConcreteDecorator就是具体的装饰对象之四,起到给Component添加职责的功能 66 class ConcreteDecoratorD:public Decorator 67 { 68 public: 69 ConcreteDecoratorD(Component* com); 70 ~ConcreteDecoratorD(); 71 virtual void Operation(); 72 void AddBehavorD(); 73 }; 74 75 //只添加一种装饰,则不用抽象出装饰基类 76 class DecoratorOnlyOne:public Component 77 { 78 public: 79 DecoratorOnlyOne(Component* com); 80 ~DecoratorOnlyOne(); 81 virtual void Operation(); 82 void AddBehavor(); 83 private: 84 Component* _com; 85 }; 86 87 //如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。 88 //略 89 #endif
Decorator.cpp
1 #include "Decorator.h" 2 #include <iostream> 3 4 using namespace std; 5 6 Component::Component() 7 {} 8 9 Component::~Component() 10 { 11 cout << "~Component" << endl; 12 } 13 14 ConcreteComponent::ConcreteComponent() 15 {} 16 17 ConcreteComponent::~ConcreteComponent() 18 { 19 cout << "~ConcreteComponent" << endl; 20 } 21 22 void ConcreteComponent::Operation() 23 { 24 cout << "原职责:ConcreteComponent::Operation" << endl; 25 } 26 27 Decorator::Decorator(Component* com) 28 { 29 this->_com = com; 30 } 31 32 void Decorator::SetComponent(Component* com) 33 { 34 this->_com = com; 35 } 36 37 Decorator::~Decorator() 38 { 39 cout << "~Decorator" << endl; 40 delete this->_com; 41 this->_com = NULL; 42 } 43 44 void Decorator::Operation() 45 {} 46 47 ConcreteDecoratorA::ConcreteDecoratorA(Component* com):Decorator(com) 48 {} 49 50 ConcreteDecoratorA::~ConcreteDecoratorA() 51 { 52 cout << "~ConcreteDecoratorA" << endl; 53 } 54 55 void ConcreteDecoratorA::Operation() 56 { 57 this->_com->Operation(); 58 //附加职责A 59 this->AddBehavorA(); 60 } 61 62 void ConcreteDecoratorA::AddBehavorA() 63 { 64 cout << "附加职责A:ConcreteDecoratorA::AddBehavorA" << endl; 65 } 66 67 ConcreteDecoratorB::ConcreteDecoratorB(Component* com):Decorator(com) 68 {} 69 70 ConcreteDecoratorB::~ConcreteDecoratorB() 71 { 72 cout << "~ConcreteDecoratorB" << endl; 73 } 74 75 void ConcreteDecoratorB::Operation() 76 { 77 this->_com->Operation(); 78 //附加职责B 79 this->AddBehavorB(); 80 } 81 82 void ConcreteDecoratorB::AddBehavorB() 83 { 84 cout << "附加职责B:ConcreteDecoratorB::AddBehavorB" << endl; 85 } 86 87 ConcreteDecoratorC::ConcreteDecoratorC(Component* com):Decorator(com) 88 {} 89 90 ConcreteDecoratorC::~ConcreteDecoratorC() 91 { 92 cout << "~ConcreteDecoratorC" << endl; 93 } 94 95 void ConcreteDecoratorC::Operation() 96 { 97 this->_com->Operation(); 98 //附加职责C 99 this->AddBehavorC(); 100 } 101 102 void ConcreteDecoratorC::AddBehavorC() 103 { 104 cout << "附加职责C:ConcreteDecoratorC::AddBehavorC" << endl; 105 } 106 107 ConcreteDecoratorD::ConcreteDecoratorD(Component* com):Decorator(com) 108 {} 109 110 ConcreteDecoratorD::~ConcreteDecoratorD() 111 { 112 cout << "~ConcreteDecoratorD" << endl; 113 } 114 115 void ConcreteDecoratorD::Operation() 116 { 117 this->_com->Operation(); 118 //附加职责D 119 this->AddBehavorD(); 120 } 121 122 void ConcreteDecoratorD::AddBehavorD() 123 { 124 cout << "附加职责D:ConcreteDecoratorD::AddBehavorD" << endl; 125 } 126 127 //**************只添加一种修饰****************** 128 DecoratorOnlyOne::DecoratorOnlyOne(Component* com):_com(com) 129 { 130 } 131 132 DecoratorOnlyOne::~DecoratorOnlyOne() 133 { 134 cout << "~DecoratorOnlyOne" << endl; 135 delete this->_com; 136 this->_com = NULL; 137 } 138 139 void DecoratorOnlyOne::Operation() 140 { 141 this->_com->Operation(); 142 this->AddBehavor(); 143 } 144 145 void DecoratorOnlyOne::AddBehavor() 146 { 147 cout << "附加唯一职责:DecoratorOnlyOne::AddBehavor" << endl; 148 }
main.cpp
1 #include "Decorator.h" 2 #include <iostream> 3 4 using namespace std; 5 int main() 6 { 7 Component* pCom = new ConcreteComponent(); //要装饰的对象 8 Decorator* pDec = NULL; 9 pDec = new ConcreteDecoratorA(pCom); //给装饰对象附加职责A 10 pDec = new ConcreteDecoratorB(pDec); //给装饰对象附加职责B 11 pDec = new ConcreteDecoratorC(pDec); //给装饰对象附加职责C 12 pDec = new ConcreteDecoratorD(pDec); //给装饰对象附加职责D 13 pDec->Operation(); 14 15 cout << "-------------------------------" << endl; 16 17 //只添加一种修饰 18 Component* pCom1 = new ConcreteComponent(); 19 DecoratorOnlyOne* pDec1 = new DecoratorOnlyOne(pCom1); 20 pDec1->Operation(); 21 22 cout << "-------------------------------" << endl; 23 24 delete pDec; 25 cout << "-------------------------------" << endl; 26 27 delete pDec1; 28 29 return 0; 30 }
运行结果:
Decorator模式除了采用组合的方式取得了比采用继承方式更好的效果,Decorator模式还给设计带来一种“即用即付”的方式来添加职责。在OO设计和分析经常有这样一种情况:为了多态,通过父类指针指向其具体子类,但是这就带来另外一个问题,当具体子类要添加新的职责,就必须向其父类添加一个这个职责的抽象接口,否则是通过父类指针是调用不到这个方法了。这样处于高层的父类就承载了太多的特征(方法),并且继承自这个父类的所有子类都不可避免继承了父类的这些接口,但是可能这并不是这个具体子类所需要的。而在Decorator模式提供了一种较好的解决方法,当需要添加一个操作的时候就可以通过Decorator模式来解决,你可以一步步添加新的职责。