装饰器模式的C++实现示例

核心思想

装饰器设计模式是一种结构型设计模式,它允许动态地为对象添加额外的行为或职责,而无需修改其原始类。装饰器模式通过创建一个装饰器类来包装原始对象,并在保持原始对象接口一致性的前提下,扩展其功能。

装饰器模式的核心在于:

​动态扩展功能:在不改变原始对象的情况下,通过装饰器为其添加新的行为。

​组合优于继承:装饰器模式通过组合的方式扩展功能,避免了继承带来的类层次结构复杂化。

使用场景

​动态添加功能:当需要为对象动态地添加功能,并且这些功能可以组合使用时。

​避免子类膨胀:当使用继承会导致子类数量爆炸时,装饰器模式提供了一种更灵活的替代方案。

​扩展第三方库:当无法修改第三方库的代码,但需要为其添加额外功能时。

优点

​灵活性:可以动态地为对象添加或移除功能。

​遵循开闭原则:无需修改原始类,即可扩展其功能。

​避免继承的缺点:通过组合实现功能扩展,避免了继承带来的类层次结构复杂化。

缺点

​复杂性:装饰器模式会引入大量小类,增加了代码的复杂性。

​调试困难:由于装饰器是层层包装的,调试时可能难以追踪到具体的行为。

示例代码

cpp 复制代码
#include <iostream>
#include <memory>
#include <string>

// 抽象组件:定义文本接口
class Text {
public:
    virtual ~Text() = default;
    virtual std::string render() const = 0;
};

// 具体组件:实现基本的文本
class PlainText : public Text {
private:
    std::string content;

public:
    explicit PlainText(const std::string& content) : content(content) {}

    std::string render() const override {
        return content;
    }
};

// 抽象装饰器:继承自 Text,并包含一个 Text 对象的指针
class TextDecorator : public Text {
protected:
    std::shared_ptr<Text> text;

public:
    explicit TextDecorator(std::shared_ptr<Text> text) : text(std::move(text)) {}

    std::string render() const override {
        return text->render();
    }
};

// 具体装饰器:为文本添加加粗格式
class BoldText : public TextDecorator {
public:
    explicit BoldText(std::shared_ptr<Text> text) : TextDecorator(std::move(text)) {}

    std::string render() const override {
        return "<b>" + text->render() + "</b>";
    }
};

// 具体装饰器:为文本添加斜体格式
class ItalicText : public TextDecorator {
public:
    explicit ItalicText(std::shared_ptr<Text> text) : TextDecorator(std::move(text)) {}

    std::string render() const override {
        return "<i>" + text->render() + "</i>";
    }
};

// 具体装饰器:为文本添加下划线格式
class UnderlineText : public TextDecorator {
public:
    explicit UnderlineText(std::shared_ptr<Text> text) : TextDecorator(std::move(text)) {}

    std::string render() const override {
        return "<u>" + text->render() + "</u>";
    }
};

int main() {
    // 创建原始文本
    std::shared_ptr<Text> text = std::make_shared<PlainText>("Hello, World!");
	std::cout << text->render() << std::endl;
    // 动态添加装饰器
    text = std::make_shared<BoldText>(text);
    std::cout << text->render() << std::endl;

    text = std::make_shared<ItalicText>(text);
    std::cout << text->render() << std::endl;

    text = std::make_shared<UnderlineText>(text);
    std::cout << text->render() << std::endl;

    return 0;
}

输出结果

bash 复制代码
Hello, World!
<b>Hello, World!</b>
<i><b>Hello, World!</b></i>
<u><i><b>Hello, World!</b></i></u>

代码解析

Text 类

抽象组件,定义了文本的接口 render()。

PlainText 类

具体组件,实现了基本的文本渲染。

TextDecorator 类

抽象装饰器,继承自 Text,并包含一个 Text 对象的指针。

提供了默认的 render() 实现,直接调用被装饰对象的 render() 方法。

BoldText、ItalicText、UnderlineText 类

具体装饰器,继承自 TextDecorator,重写了 render() 方法,为文本添加额外的格式。

main 函数

创建了一个原始文本对象 PlainText。

通过装饰器动态地为文本添加加粗、斜体和下划线格式。

最终渲染文本并输出结果。

装饰器模式通过动态地为对象添加额外的行为,提供了一种灵活的功能扩展方式。它避免了继承带来的类层次结构复杂化,同时遵循了开闭原则。在 C++ 中,可以通过组合和继承机制轻松实现装饰器模式。

相关推荐
CoovallyAIHub3 分钟前
SBP-YOLO:面向嵌入式悬架的轻量实时模型,实现减速带与坑洼高精度检测
深度学习·算法·计算机视觉
UnnamedOrange25 分钟前
ROS1 配置代码覆盖率
c++·cmake
沐怡旸27 分钟前
【底层机制】std::unordered_map 扩容机制
c++·面试
沐怡旸28 分钟前
【底层机制】auto 关键字的底层实现机制
c++·面试
CoovallyAIHub35 分钟前
医药、零件、饮料瓶盖……SuperSimpleNet让质检“即插即用”
深度学习·算法·计算机视觉
华溢澄37 分钟前
macOS下基于Qt/C++的OpenGL开发环境的搭建
c++·qt·macos·opengl
dragoooon3438 分钟前
[优选算法专题二滑动窗口——串联所有单词的子串]
数据结构·c++·学习·算法·leetcode·学习方法
刃神太酷啦40 分钟前
C++ 异常处理机制:从基础到实践的全面解析----《Hello C++ Wrold!》(20)--(C/C++)
java·c语言·开发语言·c++·qt·算法·leetcode
CYRUS_STUDIO42 分钟前
OLLVM 移植 LLVM18 踩坑:一步步调试修复控制流平坦化
c语言·c++·llvm
将编程培养成爱好1 小时前
C++ 设计模式《外卖菜单展示》
c++·设计模式