装饰者模式:动态将职责附加到对象上,若要扩展功能,装饰者提供了比继承更具弹性的代替方案。
UML图
设计原则:
1. 多用组合,少用继承。
利用继承设计子类的行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为。然而,如果能够利用组合的做法扩展对象的行为,就可以在运行时动态地进行扩展。
2. 类应设计的对扩展开放,对修改关闭。
要点:
1. 装饰者和被装饰对象有相同的超类型。
2. 可以用一个或多个装饰者包装一个对象。
3. 装饰者可以在所委托被装饰者的行为之前或之后,加上自己的行为,以达到特定的目的。
4. 对象可以在任何时候被装饰,所以可以在运行时动态的,不限量的用你喜欢的装饰者来装饰对象。
5. 装饰模式中使用继承的关键是想达到装饰者和被装饰对象的类型匹配,而不是获得其行为。
6. 装饰者一般对组件的客户是透明的,除非客户程序依赖于组件的具体类型。在实际项目中可以根据需要为装饰者添加新的行为,做到“半透明”装饰者。
7. 适配器模式的用意是改变对象的接口而不一定改变对象的性能,而装饰模式的用意是保持接口并增加对象的职责。
源代码:
Decorator.h
- #include <string>
- #include <iostream>
- #include <memory>
- using namespace std;
- //抽象类Tank
- class Tank
- {
- public:
- virtual void shot()=0;
- virtual void run()=0;
- public:
- virtual ~Tank()
- {
- cout<<"in the destructor of Tank"<<endl;
- }
- };
- //具体类 T50
- class T50:public Tank
- {
- public:
- void shot()
- {
- cout<<"Tank T50 shot()"<<endl;
- }
- void run()
- {
- cout<<"Tank T50 run()"<<endl;
- }
- public:
- virtual ~T50()
- {
- cout<<"In the destructor of T50"<<endl;
- }
- };
- //具体类T75
- class T75:public Tank
- {
- public:
- void shot()
- {
- cout<<"Tank T75 shot()"<<endl;
- }
- void run()
- {
- cout<<"Tank T75 run()"<<endl;
- }
- public:
- virtual ~T75()
- {
- cout<<"In the destructor of T75"<<endl;
- }
- };
- //具体类 T90
- class T90:public Tank
- {
- public:
- void shot()
- {
- cout<<"Tank T90 shot()"<<endl;
- }
- void run()
- {
- cout<<"Tank T90 run()"<<endl;
- }
- public:
- virtual ~T90()
- {
- cout<<"In the destructor of T90"<<endl;
- }
- };
- //抽象类,Decorator
- class Decorator:public Tank
- {
- protected:
- auto_ptr<Tank> tank;
- public:
- Decorator(auto_ptr<Tank> tank):tank(tank) {} //具体的坦克的装饰类
- virtual ~Decorator()
- {
- cout<<"In the destructor of Decorator"<<endl;
- }
- public:
- void shot()
- {
- tank->shot();
- }
- void run()
- {
- tank->run();
- }
- };
- class InfraredDecorator: public Decorator
- {
- private:
- string infrared;//这就是所谓的addAtrribute
- public:
- InfraredDecorator(auto_ptr<Tank> tank):Decorator(tank) {}
- virtual ~InfraredDecorator()
- {
- cout<<"in the destructor of InfraredDecorator"<<endl;
- }
- public:
- void set_Infrared(const string &infrared)
- {
- this->infrared=infrared;
- }
- string get_infrared() const
- {
- return infrared;
- }
- void run()
- {
- tank->run();
- set_Infrared("+Infrared");
- cout<<get_infrared()<<endl;
- }
- void shot()
- {
- tank->shot();
- }
- };
- class AmphibianDecorator:public Decorator
- {
- private:
- string amphibian;
- public:
- AmphibianDecorator(auto_ptr<Tank> tank):Decorator(tank) {}
- ~AmphibianDecorator()
- {
- cout<<"in the destructor of AmphibianDecorator"<<endl;
- }
- public:
- void set_amphibian(const string &hibian)
- {
- this->amphibian=amphibian;
- }
- string get_amphibian() const
- {
- return amphibian;
- }
- public:
- void run()
- {
- tank->run();
- set_amphibian("+amphibian");
- cout<<get_amphibian()<<endl;
- }
- void shot()
- {
- tank->shot();
- }
- };
- class GPSDecorator:public Decorator
- {
- private:
- string gps;
- public:
- GPSDecorator(auto_ptr<Tank> tank):Decorator(tank) {}
- ~GPSDecorator()
- {
- cout<<"in the destructor of GPSDecorator"<<endl;
- }
- public:
- void set_gps(const string &gps)
- {
- this->gps=gps;
- }
- string get_gps() const
- {
- return gps;
- }
- public:
- void run()
- {
- tank->run();
- set_gps("+gps");
- cout<<get_gps()<<endl;
- }
- void shot()
- {
- tank->shot();
- }
- };
Decorator.cpp
- #include "Decorator.h"
- int main(int argc, char **argv)
- {
- //给T50增加红外功能
- auto_ptr<Tank> tank1(new T50);
- auto_ptr<Tank> pid1(new InfraredDecorator(tank1));
- pid1->shot();
- cout<<endl;
- pid1->run();
- cout<<endl;
- cout<<endl<<"---------------"<<endl;
- //给t75增加红外、两栖功能
- auto_ptr<Tank> tank2(new T75);
- auto_ptr<Tank> pid2(new InfraredDecorator(tank2));
- auto_ptr<Tank> pad2(new AmphibianDecorator(pid2));
- pad2->shot();
- cout<<endl;
- pad2->run();
- cout<<endl;
- cout<<endl<<"--------------"<<endl;
- //给T75增加红外,两栖,定位功能
- auto_ptr<Tank> tank3(new T75);
- auto_ptr<Tank> pid3(new InfraredDecorator(tank3));
- auto_ptr<Tank> pad3(new AmphibianDecorator(pid3));
- auto_ptr<Tank> pgd3(new GPSDecorator(pad3));
- pgd3->shot();
- cout<<endl;
- pgd3->run();
- cout<<endl;
- return 0;
- }
例2:
Test_Decorator.cpp
- #include <iostream>
- #include <string>
- using namespace std;
- class Person
- {
- private:
- string name;
- public:
- Person()
- {
- }
- Person(string name)
- {
- this->name=name;
- }
- virtual void show()
- {
- cout<<" Decorator name "<<name<<endl;
- }
- };
- class Decorator:public Person
- {
- protected:
- Person* component;
- public:
- void Deco(Person *component)
- {
- this->component=component;
- }
- void show()
- {
- component->show();
- }
- };
- class toufa:public Decorator
- {
- public:
- void show()
- {
- cout<<"头发"<<endl;
- component->show();
- }
- };
- class shoot:public Decorator
- {
- public:
- void show()
- {
- cout<<"鞋子"<<endl;
- component->show();
- }
- };
- int main()
- {
- Person fuli("fuli");
- shoot xie;
- toufa tou;
- xie.Deco(&fuli);
- tou.Deco(&xie);
- tou.show();
- return 0;
- }