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

在 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)
每个装饰器只负责一项增强逻辑,例如加牛奶、加糖、加焦糖:
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
还木有评论哦,快来抢沙发吧~