码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++设计模式-装饰器(Decorator)


    目录

    C++设计模式-装饰器(Decorator)

    一、意图

    二、适用性

    三、结构

    四、参与者

    五、代码


    C++设计模式-装饰器(Decorator)

    一、意图

    动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。

    二、适用性

    • 在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
    • 处理那些可以撤消的职责。
    • 当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。

    三、结构

     

    四、参与者

    • Component

            定义一个对象接口,可以给这些对象动态地添加职责。

    • ConcreteDecorator

           定义一个对象,可以给这个对象添加一些职责。

    • Decorator

            维持一个指向Component对象的指针,并定义一个与Component接口一致的接口。

    • ConcreteComponent

            向组件添加职责。

    五、代码

    1. #include
    2. using namespace std;
    3. class Component {
    4. public:
    5. virtual void Operation() = 0;
    6. };
    7. class ConcreteComponent : public Component{
    8. public:
    9. virtual void Operation() {
    10. cout << "ConcreteComponent" << endl;
    11. }
    12. };
    13. class Decorator : public Component {
    14. public:
    15. Decorator(Component* tempComponent) :component(tempComponent) {}
    16. virtual void Operation() {
    17. component->Operation();
    18. }
    19. private:
    20. Component* component;
    21. };
    22. class ConcreteDecoratorA : public Decorator {
    23. public:
    24. ConcreteDecoratorA(Component* tempComponent) :Decorator(tempComponent) {}
    25. virtual void Operation() {
    26. Decorator::Operation();
    27. cout << "ConcreteDecoratorA" << endl;
    28. }
    29. };
    30. class ConcreteDecoratorB : public Decorator {
    31. public:
    32. ConcreteDecoratorB(Component* tempComponent) :Decorator(tempComponent) {}
    33. virtual void Operation() {
    34. Decorator::Operation();
    35. cout << "ConcreteDecoratorB" << endl;
    36. }
    37. };
    38. int main() {
    39. Component* component = new ConcreteComponent;
    40. component->Operation();
    41. Component* componentA = new ConcreteDecoratorA(component);
    42. componentA->Operation();
    43. Component* componentB = new ConcreteDecoratorB(component);
    44. componentB->Operation();
    45. return 0;
    46. }

  • 相关阅读:
    java计算机毕业设计科技项目在线评审系统源码+mysql数据库+系统+lw文档+部署
    机器学习 | Python决策树算法
    一体化运维:挖矿病毒可能正在蚕食你的IT资源
    C语言自定义类型-结构体
    开发vue3 UI组件库,并且发布到NPM
    C# 迭代器
    LeetCode每日一题——1619. 删除某些元素后的数组均值
    [github配置] 远程访问仓库以及问题解决
    vmware入门之运行RHEL虚拟机
    Redis笔记基础篇:6分钟看完Redis的八种数据类型
  • 原文地址:https://blog.csdn.net/qq_40660998/article/details/133628183
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号