论文完成情况怎么填写:读《大话设计模式》---装饰模式(decorator) - jia

来源:百度文库 编辑:九乡新闻网 时间:2024/05/04 14:46:34
装饰模式动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更灵活。 一般的实现代码
1.class Component
2.{
3.public:
4.      //纯虚类   5.       virtual void Operation() = 0;
6.};
7.8.class ConcreteComponent : public  Component
9.{
10.public:
11.      virtual void Operation()
12.      {
13.              cout << "具体对象的操作" << endl;
14.      }
15.};
16.17.class Decorator : Component
18.{
19.protected:
20.       Component * component;
21.public:
22.       void SetComponent(Component * component)
23.       {
24.               this->compontent = component;
25.       }
26.       //重写 Operation(),实际执行的是Component的Operation()   27.       virtual void Operation()
28.       {
29.                component->Operation();
30.       }
31.}
32.33.class ConcreteDecoratorA : Decorator
34.{
35.private:
36.         //本类的独有功能,区别于ConcreteDecoratorB   37.         string addedState;
38.public:
39.         virtual void Operation()
40.         {
41.                 //首先运行原Component的Opretion (),再执行本类的功能,如addedState,相当于对原Component进行了装饰。   42.                 Component::Operation();
43.                 addedState = "New State";
44.                 cout << "具体装饰对象A的操作";
45.          }
46.}
47.48.class ConcreteDecoratorB : Decorator
49.{
50.public:
51.         virtual void Operation()
52.         {
53.                 //首先运行原Component的Opretion (),再执行本类的功能,如addedBehavior(),相当于对原Component进行了装饰。   54.                 Component::Operation();
55.                 AddedBehavior();
56.                 cout << "具体装饰对象B的操作";
57.          }
58.private:
59.          void AddedBehavior(){ }
60.}
61.int main()
62.{
63.          ConcreteComponent  *c = new ConcreteComponent ;
64.          ConcreteDecoratorA *d1 = new ConcreteComponent ;
65.          ConcreteDecoratorB *d2 = new ConcreteComponent ;
66.          //装饰的方法是:首先用ConcreteComponent实例化对象c,然后用ConcreteDecoratorA的实例化对象d1来包装c,   67.          //再用ConcreteDecoratorB的实例化对象d2来包装d1,最后执行d2的Operation()   68.          d1->SetComponent(c);
69.          d2->SetComponent(d1);
70.          d2->Operation();
71.          return 0;
72.}
73.装饰模式是利用SetComponent来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何添加到对象中去。 一个具体的实现代码:1.#include 2.#include 3.using namespace std;
4.5.class Person
6.{
7.public:
8. Person(){};
9. Person(string name)
10. {
11.  this->name = name;
12. }
13. virtual void Show()
14. {
15.  cout << "装扮的" << name << endl;
16. }
17.18.private:
19. string name;
20.};
21.22.class Finery : public Person
23.{
24.protected:
25. Person * component;
26. //打扮 27.public:
28. void Decorate(Person * component)
29. {
30.  this->component = component;
31. }
32. virtual void Show()
33. {
34.  component->Show();
35. }
36.};
37.38.//具体服装类(ConcreteDecorator) 39.class TShirts : public Finery
40.{
41.public:
42. virtual void Show()
43. {
44.  cout << "大T恤 ";
45.  Finery::Show();
46. }
47.};
48.49.class BigTrouser : public Finery
50.{
51.public:
52. virtual void Show()
53. {
54.  cout << "垮裤 ";
55.  Finery::Show();
56. }
57.};
58.59.class Sneakers : public Finery
60.{
61.public:
62. virtual void Show()
63. {
64.  cout << "破球鞋 ";
65.  Finery::Show();
66. }
67.};
68.69.class BusinessSuit : public Finery
70.{
71.public:
72. virtual void Show()
73. {
74.  cout << "西装 ";
75.  Finery::Show();
76. }
77.};
78.79.class necktie : public Finery
80.{
81.public:
82. virtual void Show()
83. {
84.  cout << "领带 ";
85.  Finery::Show();
86. }
87.};
88.89.class Shoeleather : public Finery
90.{
91.public:
92. virtual void Show()
93. {
94.  cout << "皮鞋 ";
95.  Finery::Show();
96. }
97.};
98.99.int main(int argc, char* argv[])
100.{
101. Person * xc = new Person("小菜");
102. cout << "第一种装扮: " << endl;
103.104.    TShirts    *ts = new TShirts();
105.    BigTrouser *bt = new BigTrouser();
106.    Sneakers   *sk = new Sneakers();
107.108. ts->Decorate(xc);
109. bt->Decorate(ts);
110. sk->Decorate(bt);
111. sk->Show();
112.113. return 0;
114.}
115.116.本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jia_xiaoxin/archive/2008/11/04/3213742.aspx