上一篇
在C++面向对象编程中,设计模式C++是提升代码可维护性、扩展性和复用性的关键。今天我们将聚焦于一个非常实用且优雅的结构型设计模式——C++装饰器模式。无论你是编程新手还是有一定经验的开发者,这篇装饰器模式教程都将带你一步步理解其原理与实现。
装饰器模式(Decorator Pattern)允许你动态地给一个对象添加额外的职责,而无需修改其原始类。它通过创建一个包装对象(即“装饰器”)来包裹原始对象,并在其基础上增加新功能。这种方式比使用继承更加灵活,因为你可以在运行时决定要添加哪些功能。
我们以“咖啡店点单系统”为例:基础咖啡可以加牛奶、糖、奶油等配料,每种配料都会改变最终价格和描述。
// Beverage.h#include <string>class Beverage {public: virtual ~Beverage() = default; virtual std::string getDescription() const = 0; virtual double cost() const = 0;}; // Espresso.cpp#include "Beverage.h"class Espresso : public Beverage {public: std::string getDescription() const override { return "Espresso"; } double cost() const override { return 1.99; }}; // CondimentDecorator.h#include "Beverage.h"class CondimentDecorator : public Beverage {protected: Beverage* beverage;public: CondimentDecorator(Beverage* b) : beverage(b) {} virtual ~CondimentDecorator() { delete beverage; }}; // Milk.cpp#include "CondimentDecorator.h"class Milk : public CondimentDecorator {public: Milk(Beverage* b) : CondimentDecorator(b) {} std::string getDescription() const override { return beverage->getDescription() + ", Milk"; } double cost() const override { return 0.20 + beverage->cost(); }};// Sugar.cppclass Sugar : public CondimentDecorator {public: Sugar(Beverage* b) : CondimentDecorator(b) {} std::string getDescription() const override { return beverage->getDescription() + ", Sugar"; } double cost() const override { return 0.10 + beverage->cost(); }}; // main.cpp#include <iostream>#include "Espresso.cpp"#include "Milk.cpp"int main() { // 创建一杯浓缩咖啡 Beverage* espresso = new Espresso(); std::cout << espresso->getDescription() << " $" << espresso->cost() << std::endl; // 给咖啡加牛奶 Beverage* milkCoffee = new Milk(espresso); std::cout << milkCoffee->getDescription() << " $" << milkCoffee->cost() << std::endl; // 注意:根据上面的析构函数设计,delete milkCoffee 会自动 delete espresso delete milkCoffee; return 0;} std::unique_ptr)替代裸指针。通过本篇C++装饰器模式教程,你应该已经掌握了如何在C++中实现和应用装饰器模式。它不仅能让你的代码更灵活、更易扩展,还能有效避免因过度使用继承带来的复杂性。记住,设计模式C++不是银弹,但合理使用能极大提升软件质量。
希望这篇装饰器模式教程对你有帮助!如果你正在学习C++面向对象编程,不妨动手实现一遍上述代码,加深理解。
本文由主机测评网于2025-12-14发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/2025127669.html