结构型模式——装饰模式

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

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

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

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
相关推荐
C++ 老炮儿的技术栈4 小时前
std::atomic 是什么
linux·c语言·网络·c++·visual studio
All for pursuit.4 小时前
【完结】LeetCodeHot100题 详细解题过程及代码汇总
数据结构·c++·算法·leetcode
重生之小比特5 小时前
【算法拾光】双指针|移动零 + 复写零
数据结构·c++·算法
多弗朗皮卡丘5 小时前
C++异常
开发语言·c++·异常
米糕闯编程5 小时前
鱼香ros2(十七)C++小海龟巡逻
开发语言·c++·机器人·ros2·服务·小海龟
博风6 小时前
9、设计模式应用举例:责任链模式:计算阶乘
设计模式·责任链模式
栈帧之外6 小时前
【C++】红黑树实现
开发语言·c++
程序员老陆6 小时前
C++中同一个回调函数被多个设备并发调用:并发问题不在“函数”,而在“状态”
开发语言·c++·程序设计
辛苦才能6 小时前
C++ unordered_map/set 深度解析:快在O(1),输在无序,与map/set怎么选
c++·哈希算法·散列表
电子IT笔记8 小时前
人走了,记录不能“空白”: MES×HR 离职人员下拉回显实战
设计模式