进步始于交流
收获源于分享

C++设计模式-装饰模式

介绍

  • Component抽象构件
    Component是一个接口或者是抽象类, 就是定义我们最核心的对象, 也就是最原始的对象, 如上面的成绩单。
  • ConcreteComponent 具体构件
    ConcreteComponent是最核心、 最原始、 最基本的接口或抽象类的实现, 你要装饰的就是它。
  • Decorator装饰角色
    在它的属性里必然有一个
    private变量指向Component抽象构件。若只有一个装饰类,则可以没有抽象装饰角色,直接实现具体的装饰角色即可
  • ConcreteDecorator具体装饰角色
    要把最核心的、 最原始的、 最基本的东西装饰成其他东西。

原始方法和装饰方法的执行顺序在具体的装饰类是固定的, 可以通过方法重载实现多种执行顺序。

优点: 装饰类和被装饰类可以独立发展, 而不会相互耦合; 装饰模式是继承关系的一个替代方案; 装饰模式可以动态地扩展一个实现类的功能

缺点: 多层的装饰是比较复杂的, 尽量减少装饰类的数量, 以便降低系统的复杂度

应用:

  • 需要扩展一个类的功能, 或给一个类增加附加功能。
  • 需要动态地给一个对象增加功能, 这些功能可以再动态地撤销。
  • 需要为一批的兄弟类进行改装或加装功能, 当然是首选装饰模式。

范例

抽象构件

#ifndef COMPONENT_H
#define COMPONENT_H
class Component {
//抽象的构件
public:
    virtual void operate() = 0;
};
#endif // COMPONENT_H

具体构件

#ifndef CONCRETECOMPONENT_H
#define CONCRETECOMPONENT_H
#include "component.h"
#include <QDebug>
class ConcreteComponent : public Component {
public:
    void operate() override {
        qDebug()<<"ConcreteComponent do Something";
    }
};
#endif // CONCRETECOMPONENT_H

抽象装饰角色

#ifndef DECORATOR_H
#define DECORATOR_H
#include "component.h"
class Decorator : public Component {
//通过构造函数传递被修饰者
public:
    Decorator(Component *component){
        component_ = component;
    }
    //委托给被修饰者执行
    void operate() override {
        component_->operate();
    }
private:
    Component *component_ = nullptr;
};
#endif // DECORATOR_H

具体装饰角色

#ifndef CONCRETEDECORATOR_H
#define CONCRETEDECORATOR_H
#include <QDebug>
#include "decorator.h"
class ConcreteDecorator1 : public Decorator {
public:
    ConcreteDecorator1(Component *component)
        : Decorator(component) {
    }
    //重写父类的Operation方法
    void operate() override {
        method1();
        Decorator::operate();
    }
private:
    //定义自己的修饰方法
    void method1(){
        qDebug()<<"method1 of Decorator1";
    }
};
class ConcreteDecorator2 : public Decorator {
public:
    ConcreteDecorator2(Component *component)
        : Decorator(component) {

    }
    //重写父类的Operation方法
    void operate() override {
        method1();
        Decorator::operate();
    }
private:
    //定义自己的修饰方法
    void method1(){
        qDebug()<<"method1 of Decorator2";
    }
};
#endif // CONCRETEDECORATOR_H

main

#include "concretecomponent.h"
#include "concretedecorator.h"
int main(int argc, char *argv[]) {
    Component *component = new ConcreteComponent();
    //第一次修饰
    component = new ConcreteDecorator1(component);
    //第二次修饰
    component = new ConcreteDecorator2(component);
    //修饰后运行
    component->operate();
}

例子又忘了delete。。。每个例子到最后main都忘了delete

结果

method1 of Decorator2
method1 of Decorator1
ConcreteComponent do Something

源码GitHub:CppDesignPattern
相关链接:C++设计模式

赞(0) 打赏
未经允许不得转载:Coologic » C++设计模式-装饰模式

评论 226

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #0
  2. #0
  3. #0
  4. #0
  5. #0
  6. #0
  7. #0
  8. #0
  9. #0
  10. #0
  11. #0
  12. #0
  13. #0
  14. #0
  15. #0
  16. #0
  17. #0
  18. #0
  19. #0
  20. #0
  21. #0
  22. #0
  23. #0
  24. #0
  25. #0
  26. #0
  27. #0
  28. #0
  29. #0
  30. #0
  31. #0
  32. #0
  33. #0
  34. #0
  35. #0
  36. #0
  37. #0
  38. #0
  39. #0
  40. #0
  41. #0
  42. #0
  43. #0
  44. #0
  45. #0
  46. #0
  47. #0
  48. #0
  49. #0
  50. #0
  51. #0
  52. #0
  53. #0
  54. #0
  55. #0
  56. #0
  57. #0
  58. #0
  59. #0
  60. #0
  61. #0
  62. #0
  63. #0
  64. #0
  65. #0
  66. #0
  67. #0
  68. #0
  69. #0
  70. #0
  71. #0
  72. #0
  73. #0
  74. #0

    php://input.

    17个月前 (10-17)回复
  75. #0

    print 28763*4196403;

    17个月前 (10-17)回复
  76. #0

    ‘;print 28763*4196403;#

    17个月前 (10-17)回复
  77. #0

    “;print 28763*4196403;#

    17个月前 (10-17)回复
  78. #0

    ;print 28763*4196403;

    17个月前 (10-17)回复
  79. #0

    | type %SystemRoot%\win.ini |

    17个月前 (10-17)回复
  80. #0

    && type %SystemRoot%\win.ini &&

    17个月前 (10-17)回复
  81. #0

    type %SystemRoot%\win.ini

    17个月前 (10-17)回复
  82. #0

    | type %SystemDrive%\boot.ini |

    17个月前 (10-17)回复
  83. #0

    && type %SystemDrive%\boot.ini &&

    17个月前 (10-17)回复
  84. #0

    type %SystemDrive%\boot.ini

    17个月前 (10-17)回复
  85. #0

    | /bin/cat /etc/master.passwd |

    17个月前 (10-17)回复
  86. #0

    && /bin/cat /etc/master.passwd &&

    17个月前 (10-17)回复
  87. #0

    /bin/cat /etc/master.passwd

    17个月前 (10-17)回复
  88. #0

    | /bin/cat /etc/security/passwd |

    17个月前 (10-17)回复
  89. #0

    && /bin/cat /etc/security/passwd &&

    17个月前 (10-17)回复
  90. #0

    /bin/cat /etc/security/passwd

    17个月前 (10-17)回复
  91. #0

    | /bin/cat /etc/passwd |

    17个月前 (10-17)回复
  92. #0

    && /bin/cat /etc/passwd &&

    17个月前 (10-17)回复
  93. #0

    /bin/cat /etc/passwd

    17个月前 (10-17)回复
  94. #0

    1<!–

    17个月前 (10-17)回复
  95. #0

    1]]]]]]]]]

    17个月前 (10-17)回复
  96. #0
  97. #0

    X-CRLF-Safe-77d60f8f391250429a8ddcb19fe53a3a: no

    17个月前 (10-17)回复
  98. #0
  99. #0
  100. #0
  101. #0
  102. #0

    1hTtP://webscan.guanxingtai.net/rfi.md5.txt

    17个月前 (10-17)回复
  103. #0

    webscan.guanxingtai.net/rfi.md5.txt

    17个月前 (10-17)回复
  104. #0
  105. #0
  106. #0
  107. #0
  108. #0
  109. #0
  110. #0
  111. #0
  112. #0
  113. #0
  114. #0
  115. #0
  116. #0

    1%28%29%22%26%251%27-%3B%3Cxss_77d60f8f391250429a8ddcb19fe53a3a%2F%3E%27

    17个月前 (10-17)回复
  117. #0
  118. #0

    1%3Cxss_77d60f8f391250429a8ddcb19fe53a3a%2F%3E

    17个月前 (10-17)回复
  119. #0
  120. #0
  121. #0
  122. #0
  123. #0
  124. #0

    1

    php://input9个月前 (08-28)回复
  125. #0

    1

    "9个月前 (08-28)回复
  126. #0

    1

    '9个月前 (08-28)回复
  127. #0
  128. #0
  129. #0
  130. #0
  131. #0
  132. #0
  133. #0
  134. #0
  135. #0
  136. #0
  137. #0
  138. #0
  139. #0
  140. #0
  141. #0
  142. #0
  143. #0

    1

    1()"9个月前 (08-28)回复
  144. #0
  145. #0

    1

    php://input.10个月前 (08-06)回复
  146. #0

    1

    print 28763*4196403;10个月前 (08-06)回复
  147. #0

    1

  148. #0

    1

  149. #0

    1

    ;print 28763*4196403;10个月前 (08-06)回复
  150. #0
  151. #0
  152. #0
  153. #0
  154. #0
  155. #0
  156. #0
  157. #0
  158. #0
  159. #0
  160. #0
  161. #0
  162. #0
  163. #0
  164. #0
  165. #0
  166. #0
  167. #0
  168. #0
  169. #0
  170. #0
  171. #0
  172. #0
  173. #0
  174. #0
  175. #0
  176. #0
  177. #0
  178. #0
  179. #0
  180. #0
  181. #0
  182. #0
  183. #0
  184. #0
  185. #0
  186. #0
  187. #0
  188. #0
  189. #0
  190. #0
  191. #0
  192. #0
  193. #0
  194. #0

    1

  195. #0
  196. #0
  197. #0
  198. #0
  199. #0
  200. #0
  201. #0

    1

  202. #0

    1

  203. #0
  204. #0

    1

    /bin/cat /etc/passwd10个月前 (08-06)回复
  205. #0

    1

    1"'`--10个月前 (08-06)回复
  206. #0

    1

    1]]]]]]]]]10个月前 (08-06)回复
  207. #0

    1

    1'"10个月前 (08-06)回复
  208. #0
  209. #0
  210. #0
  211. #0
  212. #0
  213. #0
  214. #0
  215. #0
  216. #0
  217. #0
  218. #0
  219. #0
  220. #0
  221. #0
  222. #0
  223. #0
  224. #0
  225. #0

    1

    1()"&%1'-;'10个月前 (08-06)回复
  226. #0

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏