结构型模式——装饰模式

装饰设计模式适用于以下情况:

• 在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责;

下面是一个装饰模式的示例程序:

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

// Step 1: The Component Interface
class Coffee {
public:
    virtual ~Coffee() = default;
    virtual std::string getDescription() const = 0;
    virtual double getCost() const = 0;
};

// Step 2: Concrete Component (The plain object we want to decorate)
class SimpleCoffee : public Coffee {
public:
    std::string getDescription() const override {
        return "Simple Coffee";
    }
    
    double getCost() const override {
        return 2.00; // Base price $2.00
    }
};

// Step 3: The Base Decorator
// It maintains a pointer to a wrapped Coffee object and matches its interface
class CoffeeDecorator : public Coffee {
protected:
    std::unique_ptr<Coffee> wrappedCoffee_;

public:
    CoffeeDecorator(std::unique_ptr<Coffee> coffee) : wrappedCoffee_(std::move(coffee)) {}
    
    std::string getDescription() const override {
        return wrappedCoffee_->getDescription();
    }
    
    double getCost() const override {
        return wrappedCoffee_->getCost();
    }
};

// Step 4: Concrete Decorator A (Adds Milk)
class MilkDecorator : public CoffeeDecorator {
public:
    MilkDecorator(std::unique_ptr<Coffee> coffee) : CoffeeDecorator(std::move(coffee)) {}

    std::string getDescription() const override {
        // Delegate to the wrapped object, then add milk description
        return CoffeeDecorator::getDescription() + ", Milk";
    }

    double getCost() const override {
        // Delegate to the wrapped object, then add milk cost
        return CoffeeDecorator::getCost() + 0.50;
    }
};

// Step 5: Concrete Decorator B (Adds Sugar)
class SugarDecorator : public CoffeeDecorator {
public:
    SugarDecorator(std::unique_ptr<Coffee> coffee) : CoffeeDecorator(std::move(coffee)) {}

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

    double getCost() const override {
        return CoffeeDecorator::getCost() + 0.25;
    }
};

// Client Code
int main() {
    std::cout << "--- Ordering Custom Coffees ---\n";

    // 1. Order a completely plain coffee
    std::unique_ptr<Coffee> myCoffee = std::make_unique<SimpleCoffee>();
    std::cout << myCoffee->getDescription() << " costs: $" << myCoffee->getCost() << "\n";

    // 2. Wrap it with Milk at runtime
    myCoffee = std::make_unique<MilkDecorator>(std::move(myCoffee));
    std::cout << myCoffee->getDescription() << " costs: $" << myCoffee->getCost() << "\n";

    // 3. Wrap it with Sugar as well
    myCoffee = std::make_unique<SugarDecorator>(std::move(myCoffee));
    std::cout << myCoffee->getDescription() << " costs: $" << myCoffee->getCost() << "\n";

    // 4. Create a pre-configured double-milk coffee from scratch
    std::unique_ptr<Coffee> premiumCoffee = std::make_unique<SugarDecorator>(
        std::make_unique<MilkDecorator>(
            std::make_unique<MilkDecorator>(
                std::make_unique<SimpleCoffee>()
            )
        )
    );
    std::cout << "\nPremium Order:\n" << premiumCoffee->getDescription() << " costs: $" << premiumCoffee->getCost() << "\n";

    return 0;
}

程序运行结果如下:

shell 复制代码
$ g++ -o main main.cpp
$ ./main
--- Ordering Custom Coffees ---
Simple Coffee costs: $2
Simple Coffee, Milk costs: $2.5
Simple Coffee, Milk, Sugar costs: $2.75

Premium Order:
Simple Coffee, Milk, Milk, Sugar costs: $3.25
相关推荐
千谦阙听6 小时前
C++类和对象(中):默认成员函数、构造与析构、拷贝构造、运算符重载
开发语言·c++·学习
weixin_307779136 小时前
一维无粘 Burgers 方程的激波形成问题:MacCormack 格式求解
c++·算法·matlab
HugoStudio_SWAN7 小时前
洛谷 B4500 / B4449 / B3843 凯撒密码、密码强度与密码合规——加密与安全的三道门
c++·学习·程序人生·算法·安全
雾里看山7 小时前
源码目录、构建目录与 out-of-source build
c++·软件构建·cmake
渡我白衣9 小时前
并查集:基础认识与模拟实现
android·java·javascript·数据结构·c++·算法·并查集
如意猴10 小时前
【C++】001--C++入门(1)
开发语言·c++
hetao173383710 小时前
2026-09-01~09-04 hetao1733837 的刷题记录
c++·算法
HugoStudio_SWAN12 小时前
洛谷 P1420 / P1179 / B4262 最长连号、数字统计与词频统计——统计的三种面孔
c++·学习·程序人生·算法
raindayinrain13 小时前
c++并发
c++
j7~13 小时前
【C++】智能指针的使用及其原理--详解
c++·c++11·内存泄漏·智能指针·raii·boost智能指针