装饰模式
动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更灵活。
一般的实现代码
- class Component
- {
- public:
- //纯虚类
- virtual void Operation() = 0;
- };
- class ConcreteComponent : public Component
- {
- public:
- virtual void Operation()
- {
- cout << "具体对象的操作" << endl;
- }
- };
- class Decorator : Component
- {
- protected:
- Component * component;
- public:
- void SetComponent(Component * component)
- {
- this->compontent = component;
- }
- //重写 Operation(),实际执行的是Component的Operation()
- virtual void Operation()
- {
- component->Operation();
- }
- }
- class ConcreteDecoratorA : Decorator
- {
- private:
- //本类的独有功能,区别于ConcreteDecoratorB
- string addedState;
- public:
- virtual void Operation()
- {
- //首先运行原Component的Opretion (),再执行本类的功能,如addedState,相当于对原Component进行了装饰。
- Component::Operation();
- addedState = "New State";
- cout << "具体装饰对象A的操作";
- }
- }
- class ConcreteDecoratorB : Decorator
- {
- public:
- virtual void Operation()
- {
- //首先运行原Component的Opretion (),再执行本类的功能,如addedBehavior(),相当于对原Component进行了装饰。
- Component::Operation();
- AddedBehavior();
- cout << "具体装饰对象B的操作";
- }
- private:
- void AddedBehavior(){ }
- }
- int main()
- {
- ConcreteComponent *c = new ConcreteComponent ;
- ConcreteDecoratorA *d1 = new ConcreteComponent ;
- ConcreteDecoratorB *d2 = new ConcreteComponent ;
- //装饰的方法是:首先用ConcreteComponent实例化对象c,然后用ConcreteDecoratorA的实例化对象d1来包装c,
- //再用ConcreteDecoratorB的实例化对象d2来包装d1,最后执行d2的Operation()
- d1->SetComponent(c);
- d2->SetComponent(d1);
- d2->Operation();
- return 0;
- }
装饰模式是利用SetComponent来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,
每个装饰对象只关心自己的功能,不需要关心如何添加到对象中去。
一个具体的实现代码:
- #include <iostream>
- #include <string>
- using namespace std;
- class Person
- {
- public:
- Person(){};
- Person(string name)
- {
- this->name = name;
- }
- virtual void Show()
- {
- cout << "装扮的" << name << endl;
- }
- private:
- string name;
- };
- class Finery : public Person
- {
- protected:
- Person * component;
- //打扮
- public:
- void Decorate(Person * component)
- {
- this->component = component;
- }
- virtual void Show()
- {
- component->Show();
- }
- };
- //具体服装类(ConcreteDecorator)
- class TShirts : public Finery
- {
- public:
- virtual void Show()
- {
- cout << "大T恤 ";
- Finery::Show();
- }
- };
- class BigTrouser : public Finery
- {
- public:
- virtual void Show()
- {
- cout << "垮裤 ";
- Finery::Show();
- }
- };
- class Sneakers : public Finery
- {
- public:
- virtual void Show()
- {
- cout << "破球鞋 ";
- Finery::Show();
- }
- };
- class BusinessSuit : public Finery
- {
- public:
- virtual void Show()
- {
- cout << "西装 ";
- Finery::Show();
- }
- };
- class necktie : public Finery
- {
- public:
- virtual void Show()
- {
- cout << "领带 ";
- Finery::Show();
- }
- };
- class Shoeleather : public Finery
- {
- public:
- virtual void Show()
- {
- cout << "皮鞋 ";
- Finery::Show();
- }
- };
- int main(int argc, char* argv[])
- {
- Person * xc = new Person("小菜");
- cout << "第一种装扮: " << endl;
- TShirts *ts = new TShirts();
- BigTrouser *bt = new BigTrouser();
- Sneakers *sk = new Sneakers();
- ts->Decorate(xc);
- bt->Decorate(ts);
- sk->Decorate(bt);
- sk->Show();
- return 0;
- }