Qt C++设计模式->装饰器模式

装饰器模式(Decorator Pattern)是一种结构型设计模式,允许你在不改变对象接口的情况下,动态地为对象添加额外的功能。它通过将对象包装在装饰器类中来实现这一点,从而提供了比继承更灵活的扩展方式。

装饰器模式的应用场景

装饰器模式非常适合用于需要动态添加功能的场景,例如在GUI框架中,可以为窗口、按钮等组件添加不同的行为或样式。在Qt中,装饰器模式常用于图形界面中,为控件添加边框、背景色或其他视觉效果。

装饰器模式示例代码

cpp 复制代码
#include <QDebug>
#include <QString>

// 抽象组件类
class Coffee {
public:
    virtual QString getDescription() const = 0; // 获取描述
    virtual double cost() const = 0;             // 获取价格
    virtual ~Coffee() = default;
};

// 具体组件类:基本咖啡
class SimpleCoffee : public Coffee {
public:
    QString getDescription() const override {
        return "Simple Coffee";
    }

    double cost() const override {
        return 2.0; // 基本咖啡的价格
    }
};

// 抽象装饰器类
class CoffeeDecorator : public Coffee {
protected:
    Coffee* coffee; // 被装饰的咖啡

public:
    CoffeeDecorator(Coffee* coffee) : coffee(coffee) {}
};

// 具体装饰器类:牛奶装饰
class MilkDecorator : public CoffeeDecorator {
public:
    MilkDecorator(Coffee* coffee) : CoffeeDecorator(coffee) {}

    QString getDescription() const override {
        return coffee->getDescription() + ", Milk"; // 添加牛奶描述
    }

    double cost() const override {
        return coffee->cost() + 0.5; // 牛奶附加费用
    }
};

// 具体装饰器类:糖装饰
class SugarDecorator : public CoffeeDecorator {
public:
    SugarDecorator(Coffee* coffee) : CoffeeDecorator(coffee) {}

    QString getDescription() const override {
        return coffee->getDescription() + ", Sugar"; // 添加糖描述
    }

    double cost() const override {
        return coffee->cost() + 0.2; // 糖附加费用
    }
};

// 使用示例
int main() {
    Coffee* coffee = new SimpleCoffee(); // 创建基本咖啡

    // 添加牛奶装饰
    Coffee* milkCoffee = new MilkDecorator(coffee);
    qDebug() << milkCoffee->getDescription() << "cost:" << milkCoffee->cost();

    // 添加糖装饰
    Coffee* sugarMilkCoffee = new SugarDecorator(milkCoffee);
    qDebug() << sugarMilkCoffee->getDescription() << "cost:" << sugarMilkCoffee->cost();

    // 清理内存
    delete sugarMilkCoffee;
    delete milkCoffee; // 自动清理basic coffee
    delete coffee;

    return 0;
}

代码解析

  • Coffee类:抽象组件类,定义了基本咖啡的接口,提供描述和价格的方法。

  • SimpleCoffee类:具体组件类,表示基本咖啡,实现了描述和价格方法。

  • CoffeeDecorator类:抽象装饰器类,持有一个Coffee对象,提供基本的装饰功能。

  • MilkDecorator和SugarDecorator类:具体装饰器类,分别为咖啡添加牛奶和糖的功能,重写了描述和价格方法。

装饰器模式的优点

  • 灵活性:可以动态地添加或删除功能,而无需修改原有代码。

  • 透明性:客户代码只需要处理组件接口,而不需要了解装饰的实现。

  • 组合性:可以通过多个装饰器组合来实现复杂的功能。

装饰器模式的缺点

  • 复杂性:使用装饰器可能导致系统中出现大量的小类,增加系统复杂性。

  • 调试困难:由于多个装饰器的组合,调试时可能比较复杂。

适合使用装饰器模式的情况

  • 需要在运行时动态地给对象添加功能时。

  • 需要避免使用大量的子类来实现功能的组合时。

通过装饰器模式,Qt应用程序可以灵活地扩展UI组件的功能,提供更加丰富的用户体验。

相关推荐
倒头就睡的小比特1 天前
算法竞赛C++常用的STL
c++·算法
weilx12341 天前
C++笔记-文件IO-<fcntl.h>
c++
Smileyqp沛沛1 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车1 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光1 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
吞下星星的少年·-·1 天前
C++ 萌新语法入门篇
c++·算法比赛
霍霍的袁1 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
another heaven1 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
程序猿编码1 天前
告别改源码适配模型:纯 C++ 可配置 LLM 推理引擎,全格式全结构兼容
c++·大模型·llm·推理引擎
此生决int1 天前
深入理解C++系列(20)——C++11(下)
开发语言·c++