装饰设计模式_......._百度空间

一般的实现代码

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,{zh1}执行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 << "{dy}种装扮: " << endl;

TShirts    *ts = new TShirts();
BigTrouser *bt = new BigTrouser();
Sneakers   *sk = new Sneakers();

ts->Decorate(xc);
bt->Decorate(ts);
sk->Decorate(bt);
sk->Show();
    cin.get();
return 0;
}



郑重声明:资讯 【装饰设计模式_......._百度空间】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——