装饰器模式

在原来的基础上,增加一个功能。

比如有个手机类,你可以写个抽象的装饰器类,该类中加入个手机类的成员,因为后期要用到这个手机类,且无法被更改,则可用protected:成员。

假如要写个手机贴膜的装饰器,则可继承写好的装饰器,然后对相应的虚函数功能进行重写。

代码示例:

 1 #define  _CRT_SECURE_NO_WARNINGS 
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 //抽象的手机类
 7 class Phone
 8 {
 9 public:
10     virtual void show() = 0;
11 };
12 
13 class iPhone :public Phone
14 {
15 public:
16     virtual void show() {
17         cout << "秀出了iphone" << endl;
18     }
19 };
20 
21 class Mi :public Phone
22 {
23     virtual void show() {
24         cout << "秀出了小米手机" << endl;
25     }
26 };
27 
28 //写一个抽象的装饰器
29 class Decorator :public Phone
30 {
31 public:
32     Decorator(Phone *phone) {
33         this->phone = phone;
34     }
35 
36     virtual void show() = 0;
37 protected:
38     Phone * phone; //拥有一个 所有手机的父类指针
39 };
40 
41 //具体的手机贴膜装饰器
42 class MoDecorator :public Decorator
43 {
44 public:
45     MoDecorator(Phone *phone) : Decorator(phone) {}
46     virtual void show() {
47         this->phone->show();  //保持原有的show
48 
49         this->mo(); //额外添加一个 mo的方法
50     }
51 
52     //膜装饰器,可以修饰添加的方法
53     void mo() {
54         cout << "手机有了贴膜" << endl;
55     }
56 };
57 
58 //皮套的装饰器类
59 class TaoDecorator :public Decorator
60 {
61 public:
62     TaoDecorator(Phone *phone) : Decorator(phone) {}
63     virtual void show() {
64         this->phone->show();
65         tao();
66     }
67 
68     void tao() {
69         cout << "手机有了皮套" << endl;
70     }
71 };
72 
73 
74 
75 int main(void)
76 {
77     Phone *phone = new iPhone;
78     phone->show();
79     cout << " -----  " << endl;
80     Phone * moPhone = new MoDecorator(phone);
81     moPhone->show();
82     cout << " -----  " << endl;
83     Phone * taoPhone = new TaoDecorator(phone);
84     taoPhone->show(); 
85 
86     cout << " ------  " << endl;
87     Phone *moTaoPhone = new TaoDecorator(moPhone);
88     moTaoPhone->show(); //moPhone.show() + tao() ==  phone.show() + mo() + tao();
89 
90     
91     return 0;
92 }

 

举个英雄联盟的例子:

代码:

 1 #define  _CRT_SECURE_NO_WARNINGS 
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 class Hero
 7 {
 8 public:
 9     virtual void status() = 0;
10 };
11 
12 class Akali :public Hero
13 {
14 public:
15     virtual void status()  {
16         cout << "HP: 1000" << endl;
17         cout << "AP: 500" << endl;
18         cout << "AD: 50" << endl;
19     }
20 };
21 
22 //英雄的装饰器
23 class Decorator :public Hero
24 {
25 public:
26     Decorator(Hero *hero){
27         this->hero = hero;
28     }
29     virtual void status() = 0;
30 
31 protected:
32     Hero *hero;
33 };
34 
35 //日炎斗篷的装饰器
36 class RYDecorator :public Decorator
37 {
38 public:
39     RYDecorator(Hero *hero) : Decorator(hero) {}
40     virtual void status()  {
41         this->hero->status(); //先调用被装饰的 英雄的基本状态
42 
43         cout << "HP + 100000" << endl;
44     }
45 
46 };
47 
48 //深渊权杖的装备装饰器
49 class SYQZDecorator :public Decorator
50 {
51 public:
52     SYQZDecorator(Hero *hero) : Decorator(hero) {}
53     virtual void status() {
54         this->hero->status();
55 
56         cout << "AP +50000 " << endl;
57     }
58 };
59 
60 int main(void)
61 {
62     Hero *akali = new Akali;
63     cout << "akali的初始状态" << endl;
64     akali->status();
65 
66     cout << "通过日然斗篷创建新的akali" << endl;
67     Hero *ryAkali = new RYDecorator(akali);
68     ryAkali->status();
69 
70     cout << "再通过 深渊权杖的装备装饰器 修饰日炎akali" << endl;
71     Hero *syAkali = new SYQZDecorator(ryAkali);
72     syAkali->status();
73     
74     return 0;
75 }

 

posted @ 2020-03-24 09:07  撑雨伞的小男孩  阅读(134)  评论(0编辑  收藏  举报