当前位置: 首页 > wzjs >正文

淘宝客网站html广州网站建设正

淘宝客网站html,广州网站建设正,网站空间推荐,麻阳住房和城乡建设局网站1.装饰器模式(Decorator Pattern)的定义 装饰器模式是一种结构型设计模式,其核心思想是: 动态地给对象添加额外功能,而不改变其原有结构通过**包装(wrapping)**原始对象来提供增强功能遵循开闭…

1.装饰器模式(Decorator Pattern)的定义

装饰器模式是一种结构型设计模式,其核心思想是:

  1. 动态地给对象添加额外功能,而不改变其原有结构
  2. 通过**包装(wrapping)**原始对象来提供增强功能
  3. 遵循开闭原则(对扩展开放,对修改关闭)

UML核心组件

  • Component:定义原始对象的接口
  • ConcreteComponent:原始对象的具体实现
  • Decorator:持有Component引用并实现相同接口
  • ConcreteDecorator:具体的装饰器实现

2.背景

我在搭建python测试框架时,无论是unittest还是pytest,均提供了装饰器模式,对function加入装饰器声明包装,使得function成为待使用的case

3.Python装饰器(Decorator)的实现

Python中的装饰器语法(@decorator)是装饰器模式的一种语法糖实现,但二者并不完全等同:

def decorator(func):def wrapper(*args, **kwargs):print("Before function")  # 添加新功能result = func(*args, **kwargs)  # 调用原函数print("After function")   # 添加新功能return resultreturn wrapper@decorator
def original_function():print("Original function")

4.测试框架中的"装饰器模式"应用

1) pytest的实现方式
@pytest.mark.slow
def test_function():pass
  • 本质pytest.mark.slow是一个函数装饰器,它给测试函数添加了元数据
  • 装饰器模式体现
    • 原始组件:测试函数
    • 装饰器:pytest.mark系统
    • 新增功能:添加标记(mark)信息到测试函数
2) unittest的实现方式
@unittest.skip("reason")
def test_method(self):pass
  • 本质unittest.skip是一个类装饰器(实际是描述符协议实现)
  • 装饰器模式体现
    • 原始组件:测试方法
    • 装饰器:unittest.skip等装饰器
    • 新增功能:改变测试方法的执行行为(如跳过测试)

5.为什么说这是装饰器模式?

虽然测试框架中的装饰器使用看起来像是简单的语法装饰器,但它们实际上符合装饰器模式的核心思想:

  1. 不修改原测试函数/方法:保持原始测试逻辑不变
  2. 动态添加功能
    • pytest:添加标记、参数化、fixture依赖等
    • unittest:添加跳过、预期失败等行为
  3. 多层包装能力
    @pytest.mark.slow
    @pytest.mark.parametrize("input", [1,2,3])
    def test_func(input): pass
    

6.上位机中的装饰器模式

下面展示一个在QT上位机应用中使用装饰器模式的完整示例。这个例子模拟了一个数据可视化系统,可以动态地给数据处理器添加不同的功能(如日志记录、数据验证、加密等)。

场景描述

数据处理器可以动态添加以下功能:

  1. 日志记录功能
  2. 数据验证功能
  3. 数据加密功能

实现代码

#include <QCoreApplication>
#include <QDebug>
#include <QString>
#include <memory>// 抽象组件接口
class DataProcessor {
public:virtual ~DataProcessor() = default;virtual QString process(const QString& data) = 0;
};// 具体组件 - 核心数据处理功能
class CoreDataProcessor : public DataProcessor {
public:QString process(const QString& data) override {qDebug() << "Core processing data:" << data;// 模拟核心处理逻辑return data.toUpper();}
};// 抽象装饰器
class DataProcessorDecorator : public DataProcessor {
protected:std::unique_ptr<DataProcessor> processor;
public:DataProcessorDecorator(std::unique_ptr<DataProcessor> processor): processor(std::move(processor)) {}
};// 具体装饰器 - 日志记录
class LoggingDecorator : public DataProcessorDecorator {
public:using DataProcessorDecorator::DataProcessorDecorator;QString process(const QString& data) override {qDebug() << "Logging: Before processing -" << data;QString result = processor->process(data);qDebug() << "Logging: After processing -" << result;return result;}
};// 具体装饰器 - 数据验证
class ValidationDecorator : public DataProcessorDecorator {
public:using DataProcessorDecorator::DataProcessorDecorator;QString process(const QString& data) override {if(data.isEmpty()) {qWarning() << "Validation failed: Empty data";return "";}return processor->process(data);}
};// 具体装饰器 - 数据加密
class EncryptionDecorator : public DataProcessorDecorator {QString encrypt(const QString& data) {// 简单加密示例 - 实际应用中替换为真正的加密算法QString result;for(QChar ch : data) {result.append(QChar(ch.unicode() + 1));}return result;}QString decrypt(const QString& data) {// 简单解密QString result;for(QChar ch : data) {result.append(QChar(ch.unicode() - 1));}return result;}public:using DataProcessorDecorator::DataProcessorDecorator;QString process(const QString& data) override {QString encrypted = encrypt(data);qDebug() << "Encrypted data:" << encrypted;QString processed = processor->process(encrypted);return decrypt(processed);}
};// QT上位机应用示例
int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);// 创建基础处理器std::unique_ptr<DataProcessor> processor = std::make_unique<CoreDataProcessor>();// 动态添加功能bool enableLogging = true;bool enableValidation = true;bool enableEncryption = true;if(enableLogging) {processor = std::make_unique<LoggingDecorator>(std::move(processor));}if(enableValidation) {processor = std::make_unique<ValidationDecorator>(std::move(processor));}if(enableEncryption) {processor = std::make_unique<EncryptionDecorator>(std::move(processor));}// 模拟上位机数据处理QString testData = "Hello QT Decorator Pattern";qDebug() << "Original data:" << testData;QString result = processor->process(testData);qDebug() << "Final result:" << result;// 测试空数据验证qDebug() << "\nTesting empty data validation:";processor->process("");return a.exec();
}
输出示例
Original data: "Hello QT Decorator Pattern"
Encrypted data: "Ifmmp!RU!Efdpsojps!Qbssfsu"
Logging: Before processing - "Ifmmp!RU!Efdpsojps!Qbssfsu"
Core processing data: "Ifmmp!RU!Efdpsojps!Qbssfsu"
Logging: After processing - "IFMMP!RU!EFDPSOJPS!QBSSFSU"
Final result: "HELLO QT DECORATOR PATTERN"Testing empty data validation:
Validation failed: Empty data
关键点解析
  1. 组件接口DataProcessor 定义了核心接口
  2. 具体组件CoreDataProcessor 实现基础功能
  3. 装饰器基类DataProcessorDecorator 持有被装饰对象的指针
  4. 具体装饰器
    • LoggingDecorator 添加日志功能
    • ValidationDecorator 添加数据验证
    • EncryptionDecorator 添加加密/解密功能
  5. 动态组合:在运行时根据需要组合各种功能
QT实际应用场景
  1. 通信协议栈:可以动态添加CRC校验、数据压缩、加密等层
  2. UI组件增强:为基本控件添加动画、阴影等效果
  3. 数据处理流水线:动态组合不同的数据处理算法
  4. 插件系统:通过装饰器模式动态扩展功能

这种模式在QT上位机开发中特别有用,因为它允许灵活地组合功能而不需要修改现有代码,符合开闭原则。


文章转载自:

http://jQUqVqo0.nfmLt.cn
http://BL7lvrJ8.nfmLt.cn
http://KIudVrhm.nfmLt.cn
http://gmytYxK2.nfmLt.cn
http://34i732dT.nfmLt.cn
http://tmmgTZ9W.nfmLt.cn
http://5sdMhJKo.nfmLt.cn
http://d7vX3L5e.nfmLt.cn
http://ssMCRpQk.nfmLt.cn
http://vBf7QORu.nfmLt.cn
http://GeWL0EDx.nfmLt.cn
http://sFOe9aUd.nfmLt.cn
http://zuryiRDq.nfmLt.cn
http://yPnD5ooq.nfmLt.cn
http://uoHDaWca.nfmLt.cn
http://fkeJQXVK.nfmLt.cn
http://TdUnO2Pb.nfmLt.cn
http://sp3OQOgW.nfmLt.cn
http://pU8r5h0m.nfmLt.cn
http://fv71vn5E.nfmLt.cn
http://UnrxYy8K.nfmLt.cn
http://2P46yBlC.nfmLt.cn
http://SUstQQTq.nfmLt.cn
http://88Zp5xMS.nfmLt.cn
http://iX5huPEl.nfmLt.cn
http://sQ9OICjf.nfmLt.cn
http://yv6Xwnss.nfmLt.cn
http://XrvxAtIg.nfmLt.cn
http://mWNOJ44F.nfmLt.cn
http://HwNKExwE.nfmLt.cn
http://www.dtcms.com/wzjs/645312.html

相关文章:

  • 外贸品牌网站建设电子商务平台建设预算
  • 婚纱摄影网站应该如何做优化泉州网站建设外包
  • 怎么创建免费网站微网站建设包含哪些内容
  • 有哪些做ae小动效的网站资源网站的建设
  • 可以直接进入网站的正能量群晖nas做网站
  • 公司网站首页大图怎么做新网站如何做排名
  • 在哪做网站专业营销型网站建设原则
  • 千图网解析网站怎么做阅读网站建设规划书
  • 400选号网站源码教育类网站策划书
  • 手机怎么创建自己的网站平台建筑材料价格信息网
  • 企业网站建设预算湖南人文科技学院在哪
  • 建网站系统能换吗网站开发项目个人总结
  • 西安免费做网站多少钱辽宁建设工程信息网签章
  • 网站怎么做才被收录快郑州市建筑材料信息价
  • 石家庄网站空间建网站卖东西
  • 个人音乐网站源码阿里云装wordpress慢
  • 定西网站建设公司什么是百度搜索推广
  • 怎样让自己做的网站被百度收录网站平台结构
  • 网站建设相关语言网站的超级链接怎么做
  • 郑州定制网站国内最新新闻热点事件
  • 湖南省城乡建设勘测院 网站ui设计师可以做到多少岁
  • 跟公司产品做网站布吉网站建设哪家服务周到
  • 房产信息网站西宁互联网公司
  • 如何建设一免费的网站高端品牌粉碎机
  • 哈尔滨悦创网络科技网站开发一个公司可以做两个网站不
  • 网站企业快速备案室内设计主要学什么课程
  • 网站预算表怎么做如何接推广的单子
  • 运城 网站 建设 招聘重庆市建设局网站
  • 网站规划建设与管理维护的论文光通信网站模板
  • 个人网站报价制作网站的步骤有哪些