C++可通过接口抽象、类组合与智能指针委托实现装饰器模式:定义统一Component接口,Concrete Component实现基础功能,Decorator基类持有并转发Component指针,Concrete Decorators添加具体职责,运行时动态组合。

在 C++ 中没有 Python 那样原生的 @decorator 语法,但可以通过类组合 + 接口抽象 + 指针/智能指针委托,完整实现装饰器模式(Decorator Pattern)——它属于结构型设计模式,核心是「动态地为对象添加职责,而不改变其原有结构」。
所有被装饰对象和装饰器都要实现同一接口,保证可替换性:
class Coffee {
public:
virtual ~Coffee() = default;
virtual std::string getDescription() const = 0;
virtual double getCost() const = 0;
};基础对象,比如一杯普通咖啡:
class SimpleCoffee : public Coffee {
public:
std::string getDescription() const override {
return "Simple coffee";
}
double getCost() const override {
return 2.0;
}
};关键:持有 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();
}
};每个装饰器只负责一项增强逻辑,例如加牛奶、加糖、加焦糖:
以 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++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号