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

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,支持层层嵌套:

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

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

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

    double getCost() const override {
        return coffee_->getCost();
    }
};

实现具体装饰器(Concrete Decorators)

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

  • MilkDecorator:在描述后追加 " + milk",成本 +0.5
  • SugarDecorator:描述追加 " + sugar",成本 +0.2
  • CaramelDecorator:描述追加 " + caramel",成本 +0.8

以 MilkDecorator 为例:

class MilkDecorator : public CoffeeDecorator {
public:
    explicit MilkDecorator(std::unique_ptr 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();
coffee = std::make_unique(std::move(coffee));
coffee = std::make_unique(std::move(coffee));
coffee = std::make_unique(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 语义提升效率。