首页 > 后端开发 > C++ > 正文

c++如何实现一个装饰器模式_c++结构型设计模式Decorator【实例】

裘德小鎮的故事
发布: 2025-12-13 12:03:24
原创
688人浏览过
C++可通过接口抽象、类组合与智能指针委托实现装饰器模式:定义统一Component接口,Concrete Component实现基础功能,Decorator基类持有并转发Component指针,Concrete Decorators添加具体职责,运行时动态组合。

c++如何实现一个装饰器模式_c++结构型设计模式decorator【实例】

在 C++ 中没有 Python 那样原生的 @decorator 语法,但可以通过类组合 + 接口抽象 + 指针/智能指针委托,完整实现装饰器模式(Decorator Pattern)——它属于结构型设计模式,核心是「动态地为对象添加职责,而不改变其原有结构」。

定义统一接口(Component)

所有被装饰对象和装饰器都要实现同一接口,保证可替换性:

class Coffee {
public:
    virtual ~Coffee() = default;
    virtual std::string getDescription() const = 0;
    virtual double getCost() const = 0;
};
登录后复制

实现具体组件(Concrete Component)

基础对象,比如一杯普通咖啡:

class SimpleCoffee : public Coffee {
public:
    std::string getDescription() const override {
        return "Simple coffee";
    }
    double getCost() const override {
        return 2.0;
    }
};
登录后复制

定义装饰器基类(Decorator)

关键:持有 Component 智能指针,并转发调用;自身也继承 Component,支持层层嵌套:

立即学习C++免费学习笔记(深入)”;

class CoffeeDecorator : public Coffee {
protected:
    std::unique_ptr<Coffee> coffee_;

public:
    explicit CoffeeDecorator(std::unique_ptr<Coffee> coffee)
        : coffee_(std::move(coffee)) {}

    std::string getDescription() const override {
        return coffee_->getDescription();
    }

    double getCost() const override {
        return coffee_->getCost();
    }
};
登录后复制

实现具体装饰器(Concrete Decorators)

每个装饰器只负责一项增强逻辑,例如加牛奶、加糖、加焦糖:

Health AI健康云开放平台
Health AI健康云开放平台

专注于健康医疗垂直领域的AI技术开放平台

Health AI健康云开放平台 113
查看详情 Health AI健康云开放平台
  • MilkDecorator:在描述后追加 " + milk",成本 +0.5
  • SugarDecorator:描述追加 " + sugar",成本 +0.2
  • CaramelDecorator:描述追加 " + caramel",成本 +0.8

以 MilkDecorator 为例:

class MilkDecorator : public CoffeeDecorator {
public:
    explicit MilkDecorator(std::unique_ptr<Coffee> coffee)
        : CoffeeDecorator(std::move(coffee)) {}

    std::string getDescription() const override {
        return CoffeeDecorator::getDescription() + " + milk";
    }

    double getCost() const override {
        return CoffeeDecorator::getCost() + 0.5;
    }
};
登录后复制

组合使用(动态装配)

运行时自由组合,无需修改原有类:

auto coffee = std::make_unique<SimpleCoffee>();
coffee = std::make_unique<MilkDecorator>(std::move(coffee));
coffee = std::make_unique<SugarDecorator>(std::move(coffee));
coffee = std::make_unique<CaramelDecorator>(std::move(coffee));

std::cout << coffee->getDescription() << "\n"; // Simple coffee + milk + sugar + caramel
std::cout << "Cost: $" << coffee->getCost() << "\n"; // 3.5
登录后复制

注意:用 std::unique_ptr 管理所有权,避免内存泄漏;也可用 std::shared_ptr 支持共享引用。

基本上就这些。装饰器模式在 C++ 中靠「接口抽象 + 组合委托 + 多态调用」落地,重点是让装饰器和被装饰者类型一致,才能无缝嵌套。不复杂但容易忽略的是:必须统一接口、装饰器必须持有并转发原始对象、构造时用 move 语义提升效率。

以上就是c++++如何实现一个装饰器模式_c++结构型设计模式Decorator【实例】的详细内容,更多请关注php中文网其它相关文章!

c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号