第六章 穿什么有这么重要---装饰模式(读书笔记)

来源:互联网 发布:mac pro是主机吗 编辑:程序博客网 时间:2024/06/08 10:01

1.装饰模式(Decorator):动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更加灵活。

2.Component是定义一个对象接口,可以给这些对象动态地添加职责。ConcreteComponent是定义了一个具体的对象,也可以给这个对象添加一些职责。Decorator:装饰抽象类,继承了Component从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的.至于ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能。

3.装饰模式是利用SetComponent来对对象进行包装的。这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。

4.装饰模式是为己有功能动态地添加更多功能的一种方式。当系统需要新功能的时候,是向旧的类中添加新的代码。这些新加的代码通常装饰了原有类的核心职责或主要行为。

5.装饰模式可以让你全副武装到牙齿,也可以让你只挂一丝到内裤。

6.装饰模式的优点是:把类中的装饰功能从类中搬移去除,这样可以简化原有的类。最大的好处是有效地把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。

7.保证装饰类之间彼此独立,这样他们就可以以任意的顺序进行组合了。

小菜的第一款装扮


代码:

#pragma once#include <string>#include <iostream>using std::string;using std::cout;using std::endl;class Person{public:Person(string name){m_Name=name;};void WearTShirts(void)      {          cout << "大T恤" << endl;      }; void WearBigTrouser(void)      {          cout << "垮裤" << endl;      };        void WearSneakers(void)      {          cout << "破球鞋" << endl;      };        void WearSuit(void)      {          cout << "西装" << endl;      };        void WearTie(void)      {          cout << "领带" << endl;      };        void WearLeatherShoes(void)      {          cout << "皮鞋" << endl;      };        void Show(void)      {          cout << m_Name << "装扮的"<< endl;      };  private:string m_Name;};void main(){Person* person=new Person("小菜");person->Show();person->WearBigTrouser();person->WearTShirts();      person->WearSneakers();cout<<endl; person->Show();      person->WearSuit();      person->WearLeatherShoes();      person->WearTie();  }

运行结果:



使用继承和抽象



代码实现:

#pragma once#include <string>#include <iostream>using std::string;using std::cout;using std::endl;class Person{public:Person(string name){m_Name=name;};      void Show(void)      {          cout << m_Name << "装扮的"<< endl;      };  private:string m_Name;};//服饰抽象类  class Finery  {  public:      virtual void Show(void) = 0;  };  class TShirts :public Finery  {  public:      void Show(void)      {          cout << "大T恤" << endl;      };  };    class BigTrouser :public Finery  {  public:      void Show(void)      {          cout << "垮裤" << endl;      };  };    class Sneakers :public Finery  {  public:      void Show(void)      {          cout << "破球鞋" << endl;      };  };    class Suit :public Finery  {  public:      void Show(void)      {          cout << "西装" << endl;      };  };      class Tie :public Finery  {  public:      void Show(void)      {          cout << "领带" << endl;      };  };    class LeatherShoe :public Finery  {  public:      void Show(void)      {          cout << "皮鞋" << endl;      };  };  void main(){Person* person=new Person("小菜");Finery* dtx=new TShirts();Finery* kk = new BigTrouser();      Finery* pqx = new Sneakers(); person->Show();dtx->Show();kk->Show();pqx->Show();cout<<endl;}

实验结果:


装饰设计模式的UML



小菜的装饰模式装扮


代码实现:

#pragma once#include <string>#include <iostream>using std::string;using std::cout;using std::endl;class Person{public:Person(string name){m_Name=name;};      void Show(void)      {          cout << "装扮的"<< m_Name << endl;      }; Person(){};private:string m_Name;};//服饰类  class Finery : public Person{  public:  void Decorator(Person* comp){this->m_pComponent=comp;};void Show(void){if (m_pComponent!=NULL){m_pComponent->Show();}}protected:Person* m_pComponent;};  class TShirts :public Finery  {  public:      void Show(void)      {          cout << "大T恤" << endl;  Finery::Show();    };  };    class BigTrouser :public Finery  {  public:      void Show(void)      {          cout << "垮裤" << endl;  Finery::Show();    };  };    class Sneakers :public Finery  {  public:      void Show(void)      {          cout << "破球鞋" << endl;  Finery::Show();    };  };    class Suit :public Finery  {  public:      void Show(void)      {          cout << "西装" << endl;  Finery::Show();    };  };      class Tie :public Finery  {  public:      void Show(void)      {          cout << "领带" << endl;  Finery::Show();    };  };    class LeatherShoe :public Finery  {  public:      void Show(void)      {          cout << "皮鞋" << endl;  Finery::Show();    };  };  void main(){Person* person=new Person("小菜");Sneakers* pqx = new Sneakers();      BigTrouser* kk = new BigTrouser();      TShirts* dtx = new TShirts(); pqx->Decorator(person);      kk->Decorator(pqx);      dtx->Decorator(kk);      dtx->Show();  }



0 0
原创粉丝点击