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

        装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许在不改变原有对象结构和功能的基础上,动态地给对象添加额外的职责或功能。

装饰器模式的主要特点包括:

  • 1. 保持了被装饰对象的核心职责不变。
  • 2. 能够透明地为对象添加新的行为和功能。
  • 3. 可以通过多个装饰器的组合来实现复杂的功能扩展。

        在实现装饰器模式时,通常会有一个抽象构件(Component)类定义了对象的基本操作接口,具体构件(ConcreteComponent)类实现了这些基本操作,装饰器(Decorator)类继承自抽象构件类并包含一个指向抽象构件对象的引用,具体装饰器(ConcreteDecorator)类在装饰器类的基础上实现了额外的功能。

装饰器模式的优点包括:

  • 1. 提供了比继承更灵活的扩展对象功能的方式。
  • 2. 可以有效地避免类数量的爆炸式增长。

缺点是:会增加代码的复杂性,在理解和维护上可能会有一定难度。

以下是一个使用 C++ 实现装饰器模式的示例代码:

#include <iostream>

// 抽象组件类
class Component {
public:
    virtual void operation() = 0;
};

// 具体组件类
class ConcreteComponent : public Component {
public:
    void operation() override {
        std::cout << "执行具体组件的操作" << std::endl;
    }
};

// 装饰器抽象类,继承自组件类
class Decorator : public Component {
protected:
    Component* component;

public:
    Decorator(Component* comp) : component(comp) {}

    void operation() override {
        if (component) {
            component->operation();
        }
    }
};

// 具体装饰器 A
class ConcreteDecoratorA : public Decorator {
public:
    ConcreteDecoratorA(Component* comp) : Decorator(comp) {}

    void operation() override {
        std::cout << "在操作前添加额外功能 A" << std::endl;
        Decorator::operation();
        std::cout << "在操作后添加额外功能 A" << std::endl;
    }
};

// 具体装饰器 B
class ConcreteDecoratorB : public Decorator {
public:
    ConcreteDecoratorB(Component* comp) : Decorator(comp) {}

    void operation() override {
        std::cout << "在操作前添加额外功能 B" << std::endl;
        Decorator::operation();
        std::cout << "在操作后添加额外功能 B" << std::endl;
    }
};

int main() {
    Component* component = new ConcreteComponent();
    Component* decoratorA = new ConcreteDecoratorA(component);
    Component* decoratorB = new ConcreteDecoratorB(decoratorA);

    decoratorB->operation();

    delete decoratorB;
    delete decoratorA;
    delete component;

    return 0;
}

        在上述代码中,我们首先定义了一个抽象组件 Component ,然后有一个具体组件 ConcreteComponent 实现了其操作。

        Decorator 是装饰器的抽象类,持有一个组件的指针,并在其 operation 方法中调用所持有组件的操作。

        ConcreteDecoratorA 和 ConcreteDecoratorB 是具体的装饰器类,它们重写了 operation 方法,在调用被装饰组件的操作前后添加了额外的功能。

        在 main 函数中,我们通过层层装饰并调用操作来展示装饰器模式的效果。最后记得释放动态分配的内存。

相关推荐

  1. 设计模式装饰模式 -- C++】

    2024-07-16 21:20:06       50 阅读
  2. C++设计模式装饰模式

    2024-07-16 21:20:06       17 阅读
  3. 装饰设计模式

    2024-07-16 21:20:06       45 阅读
  4. 设计模式装饰模式

    2024-07-16 21:20:06       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 21:20:06       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 21:20:06       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 21:20:06       57 阅读
  4. Python语言-面向对象

    2024-07-16 21:20:06       68 阅读

热门阅读

  1. 哪些点权衡素材优秀与否

    2024-07-16 21:20:06       17 阅读
  2. 前端反显后端图片、上传预览图片

    2024-07-16 21:20:06       18 阅读
  3. 使用AIOHTTP模块:提高网络请求效率

    2024-07-16 21:20:06       21 阅读
  4. Redis--过期删除策略和数据淘汰策略

    2024-07-16 21:20:06       24 阅读
  5. actual combat 35 —— es

    2024-07-16 21:20:06       21 阅读
  6. 在 Gradle 项目中,排查依赖冲突可以的详细步骤

    2024-07-16 21:20:06       16 阅读
  7. LeetCode题练习与总结:最大间距--164

    2024-07-16 21:20:06       21 阅读
  8. ThinkPHP6事件系统使用指南

    2024-07-16 21:20:06       22 阅读
  9. postman安装介绍

    2024-07-16 21:20:06       19 阅读
  10. echarts忽略Null值:使用echarts的connectNulls

    2024-07-16 21:20:06       21 阅读
  11. 知识蒸馏和知识图谱相结合的大模型微调方案

    2024-07-16 21:20:06       21 阅读
  12. uni-app开发时自定义导航栏

    2024-07-16 21:20:06       22 阅读