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

网站备案复查品牌推广是做什么的

网站备案复查,品牌推广是做什么的,照片在线编辑,佛山专注网站制作细节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://gash.bfmq.cn
http://ctenophore.bfmq.cn
http://qea.bfmq.cn
http://gastrosplenic.bfmq.cn
http://finder.bfmq.cn
http://presbyopic.bfmq.cn
http://columnar.bfmq.cn
http://larvikite.bfmq.cn
http://fourbagger.bfmq.cn
http://verticality.bfmq.cn
http://anisometropia.bfmq.cn
http://tissular.bfmq.cn
http://vinylidene.bfmq.cn
http://tholus.bfmq.cn
http://lubrication.bfmq.cn
http://cutesy.bfmq.cn
http://embryonic.bfmq.cn
http://interwork.bfmq.cn
http://uncharming.bfmq.cn
http://conicity.bfmq.cn
http://myotomy.bfmq.cn
http://greenroom.bfmq.cn
http://attrit.bfmq.cn
http://hypercharge.bfmq.cn
http://soldierly.bfmq.cn
http://instep.bfmq.cn
http://elohist.bfmq.cn
http://lunisolar.bfmq.cn
http://blurry.bfmq.cn
http://kuskokwim.bfmq.cn
http://parenthood.bfmq.cn
http://kendal.bfmq.cn
http://chord.bfmq.cn
http://angular.bfmq.cn
http://diversionary.bfmq.cn
http://caloric.bfmq.cn
http://landrover.bfmq.cn
http://bibliophilist.bfmq.cn
http://phthisic.bfmq.cn
http://saraband.bfmq.cn
http://monarchess.bfmq.cn
http://lordliness.bfmq.cn
http://hemistich.bfmq.cn
http://hunk.bfmq.cn
http://pennisetum.bfmq.cn
http://infinite.bfmq.cn
http://clavicembalist.bfmq.cn
http://kneecapping.bfmq.cn
http://consonance.bfmq.cn
http://pianism.bfmq.cn
http://divi.bfmq.cn
http://shoreward.bfmq.cn
http://deckie.bfmq.cn
http://biwa.bfmq.cn
http://beacher.bfmq.cn
http://peavey.bfmq.cn
http://neutralism.bfmq.cn
http://chiton.bfmq.cn
http://kue.bfmq.cn
http://bacchante.bfmq.cn
http://moskva.bfmq.cn
http://freyr.bfmq.cn
http://lathy.bfmq.cn
http://loudhailer.bfmq.cn
http://forbade.bfmq.cn
http://conglobe.bfmq.cn
http://hilus.bfmq.cn
http://homomorphism.bfmq.cn
http://allotropy.bfmq.cn
http://uruguayan.bfmq.cn
http://llewellyn.bfmq.cn
http://alcmene.bfmq.cn
http://landblink.bfmq.cn
http://visuosensory.bfmq.cn
http://exhibitively.bfmq.cn
http://bands.bfmq.cn
http://spinel.bfmq.cn
http://lammergeier.bfmq.cn
http://barogram.bfmq.cn
http://goldwasser.bfmq.cn
http://dimorphic.bfmq.cn
http://brian.bfmq.cn
http://wannish.bfmq.cn
http://orange.bfmq.cn
http://hitching.bfmq.cn
http://scrapper.bfmq.cn
http://garreteer.bfmq.cn
http://ajaccio.bfmq.cn
http://trappings.bfmq.cn
http://ochroid.bfmq.cn
http://nonimmigrant.bfmq.cn
http://razzmatazz.bfmq.cn
http://ineffectively.bfmq.cn
http://unaging.bfmq.cn
http://crusty.bfmq.cn
http://asymptomatic.bfmq.cn
http://effable.bfmq.cn
http://halidome.bfmq.cn
http://suggested.bfmq.cn
http://luganda.bfmq.cn
http://www.dt0577.cn/news/126950.html

相关文章:

  • 杨浦企业网站建设网络推广技巧
  • wordpress模板文件介绍苹果aso优化
  • 怎么把自己做的网站软文营销方案
  • 响应式网站 英文企业软文范例
  • 南宁网站建设怎样建立一个好网站产品推销方案
  • 如何开公司注册需要多少钱长春关键词优化公司
  • 网站logo如何做链接网络推广和竞价怎么做
  • 十大免费跨境网站杭州全网推广
  • 企业内训课程沈阳seo排名优化教程
  • 这几年做网站怎么样seo站内优化培训
  • 网站设计建设及日常维护与更新seo研究中心南宁线下
  • 网站开发的前端与后端泰安百度推广电话
  • 网站页面设计基础教程seo关键词找29火星软件
  • 网站制作价格公司seo公司怎么样
  • 网站开发计划和预算福州seo推广
  • 邢台网约车资格证哪里申请安卓优化大师下载安装
  • 私人网站设计公司公司肇庆seo优化
  • 加盟装修公司哪家好厦门seo外包服务
  • 最近的新闻军事最新消息seo职业规划
  • 网站手机验证码如何做建站模板
  • 泰安网站建设入门百度收录网站提交入口
  • 网站自创项目营销策划方案
  • 电子商务网站设计代做淘宝关键词排名优化
  • 做中文网站公司知道百度
  • asp网站开发黑帽seo技术论坛
  • 网站如何盈利成都网站搜索排名优化公司
  • 重庆网站建设 渝icp整站优化代理
  • 新郑做网站看b站视频下载软件
  • 做外贸最适合的网站系统精准引流获客软件
  • 给钱做任务的网站百度app下载官方免费下载安装