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

网站的备案流程推广品牌

网站的备案流程,推广品牌,百度网站优化指南,百度中搜到网站名字深入解析装饰者模式:动态扩展功能的艺术(C实现) 一、模式思想与应用场景 1.1 模式定义 装饰者模式(Decorator Pattern)是一种结构型设计模式,它通过将对象放入包含行为的特殊封装对象中,动态地…

深入解析装饰者模式:动态扩展功能的艺术(C++实现)

一、模式思想与应用场景

1.1 模式定义

装饰者模式(Decorator Pattern)是一种结构型设计模式,它通过将对象放入包含行为的特殊封装对象中,动态地为原始对象添加新功能,比继承更灵活。

1.2 核心优势

  • 动态添加功能:运行时修改对象行为
  • 避免类爆炸:替代多层次的继承结构
  • 遵循开闭原则:扩展开放,修改关闭

1.3 典型应用场景

  • 需要动态/透明地给对象添加职责
  • 需要撤销已添加的功能
  • 不适合使用子类扩展的情况
  • 常见应用:
    • GUI组件装饰
    • 流处理(如Java I/O)
    • 游戏角色装备系统
    • 咖啡订单系统(本文示例)

二、模式结构与C++实现

2.1 UML类图解析

2.2 咖啡订单系统实现

基础组件接口
#include <iostream>
#include <memory>
#include <string>
// 抽象组件
class Beverage {
public:virtual ~Beverage() = default;virtual std::string getDescription() const = 0;virtual double cost() const = 0;
};// 具体组件:浓缩咖啡
class Espresso : public Beverage {
public:std::string getDescription() const override {return "Espresso";}double cost() const override {return 1.99;}
};
装饰器基类
#include "component.h"
// 抽象装饰器
class CondimentDecorator : public Beverage {
public:explicit CondimentDecorator(std::unique_ptr<Beverage> beverage) : beverage_(std::move(beverage)) {}std::string getDescription() const override {return beverage_->getDescription();}double cost() const override {return beverage_->cost();}protected:std::unique_ptr<Beverage> beverage_;
};
具体装饰器实现
#include "decorator_abstract.h"// 牛奶装饰器
class Milk : public CondimentDecorator {
public:using CondimentDecorator::CondimentDecorator;std::string getDescription() const override {return beverage_->getDescription() + ", Milk";}double cost() const override {return beverage_->cost() + 0.45;}
};// 摩卡装饰器
class Mocha : public CondimentDecorator {
public:using CondimentDecorator::CondimentDecorator;std::string getDescription() const override {return beverage_->getDescription() + ", Mocha";}double cost() const override {return beverage_->cost() + 0.60;}
};

三、客户端使用示例

3.1 基础使用

#include <iostream>
#include <memory>
#include <string>
#include "decorator_entity.h"int main() {// 创建基础咖啡std::unique_ptr<Beverage> coffee = std::make_unique<Espresso>();// 第一次装饰:加牛奶coffee = std::make_unique<Milk>(std::move(coffee));// 第二次装饰:加摩卡coffee = std::make_unique<Mocha>(std::move(coffee));// 输出结果std::cout << "Order: " << coffee->getDescription() << "\n"<< "Total Cost: $" << coffee->cost() << std::endl;return 0;
}

3.2 输出结果

Order: Espresso, Milk, Mocha
Total Cost: $3.04

四、模式深度解析

4.1 设计亮点

  1. 动态组合:通过嵌套装饰实现功能叠加
  2. 透明性:装饰器与组件接口一致
  3. 弹性扩展:新装饰器不影响现有代码
  4. 智能指针管理:自动内存管理

4.2 与传统继承对比

特性继承方案装饰者模式
扩展方式编译时静态扩展运行时动态组合
类数量指数级增长(组合爆炸)线性增长
功能叠加固定组合任意顺序组合
维护成本修改基类影响所有子类独立扩展互不影响

4.3 实现注意事项

  1. 接口一致性:装饰器必须完全实现组件接口
  2. 装饰顺序:不同装饰顺序可能影响最终结果
  3. 内存管理
    • 使用unique_ptr自动管理生命周期
    • 禁止拷贝操作(示例中已隐式实现)
  4. 性能考量:多层嵌套可能影响性能

五、进阶实现技巧

5.1 装饰器撤销功能

// 移除装饰器类
template <typename T>
class RemovableDecorator : public CondimentDecorator {
public:using CondimentDecorator::CondimentDecorator;// 移除特定类型的装饰std::unique_ptr<Beverage> removeDecorator() {if (auto dec = dynamic_cast<T*>(beverage_.get())) {return std::move(dec->beverage_);}return std::move(beverage_);}std::string getDescription() const override {return beverage_->getDescription();}double cost() const override {return beverage_->cost();}
};

5.2 复合装饰器


// 复合装饰器 - 两倍装饰 将所选饮料配料翻倍
class DoubleDecorator : public CondimentDecorator {
public:using CondimentDecorator::CondimentDecorator;std::string getDescription() const override {return "'" + beverage_->getDescription() + "' x2";}double cost() const override {return beverage_->cost() * 2;}
};

5.3 调用方法

#include <iostream>
#include <memory>
#include <string>
#include "decorator_entity.h"int main() {// 创建基础咖啡std::unique_ptr<Beverage> coffee = std::make_unique<Espresso>();// 第一次装饰:加牛奶coffee = std::make_unique<Milk>(std::move(coffee));// 第二次装饰:加摩卡coffee = std::make_unique<Mocha>(std::move(coffee));// 输出结果std::cout << "Order: " << coffee->getDescription() << "\n"<< "Total Cost: $" << coffee->cost() << std::endl;// 使用示例auto rmver = std::make_unique<RemovableDecorator<Mocha>>(std::move(coffee));coffee = rmver->removeDecorator();// 输出结果std::cout << "Order: " << coffee->getDescription() << "\n"<< "Total Cost after remove Mocha: $" << coffee->cost() << std::endl;coffee = std::make_unique<DoubleDecorator>(std::move(coffee));std::cout << "Order: " << coffee->getDescription() << "\n"<< "Total Cost after double order: $" << coffee->cost() << std::endl;return 0;
}

5.4 执行结果

Order: Espresso, Milk, Mocha // 初始点单
Total Cost: $3.04
Order: Espresso, Milk // 移除Mocha
Total Cost after remove Mocha: $2.44
Order: 'Espresso, Milk' x2 // 复合装饰器,double order
Total Cost after double order: $4.88

六、模式应用扩展

6.1 实际工程案例

  1. 图形界面组件
    • 滚动条装饰文本框
    • 边框装饰图片组件
  2. 网络通信
    • 加密装饰数据流
    • 压缩装饰传输流
  3. 游戏开发
    • 装备系统增强角色属性
    • 技能BUFF叠加效果

6.2 相关模式对比

模式核心区别
适配器模式改变对象接口
代理模式控制访问,不添加功能
组合模式统一处理对象集合
策略模式整体替换算法

七、最佳实践指南

7.1 适用场景判断

✅ 推荐使用:

  • 需要动态添加/撤销功能
  • 使用继承会导致类爆炸
  • 不同功能的排列组合场景

❌ 避免使用:

  • 需要直接访问具体组件方法
  • 装饰顺序影响业务逻辑
  • 性能敏感的底层系统

7.2 实现原则

  1. 优先组合:使用对象组合而非继承
  2. 保持轻量:装饰器不应包含复杂逻辑
  3. 接口隔离:定义明确的组件接口
  4. 文档规范:明确装饰器的叠加规则

八、总结

装饰者模式通过灵活的对象组合代替僵化的类继承,为系统扩展提供了优雅的解决方案。在C++实现中:

  • 使用unique_ptr管理组件生命周期
  • 通过抽象类保证接口一致性
  • 利用现代C++特性简化实现

当面对需要动态扩展功能的场景时,装饰者模式就像编程世界的"俄罗斯套娃",让每个装饰器层层包裹核心组件,最终组合出强大的功能集合。


文章转载自:
http://dinncodiptera.wbqt.cn
http://dinncoepiphloedal.wbqt.cn
http://dinncosunstruck.wbqt.cn
http://dinncokonstanz.wbqt.cn
http://dinncochongqing.wbqt.cn
http://dinncomediumship.wbqt.cn
http://dinncomanito.wbqt.cn
http://dinncopropaedeutic.wbqt.cn
http://dinncocytokinesis.wbqt.cn
http://dinncosaleslady.wbqt.cn
http://dinncotrueborn.wbqt.cn
http://dinncosemiprofessional.wbqt.cn
http://dinncodestain.wbqt.cn
http://dinncobalkhash.wbqt.cn
http://dinncohealthily.wbqt.cn
http://dinncowhitecap.wbqt.cn
http://dinncoalonso.wbqt.cn
http://dinncopsammon.wbqt.cn
http://dinncodemurrable.wbqt.cn
http://dinncoslicker.wbqt.cn
http://dinncocarlowitz.wbqt.cn
http://dinncorowel.wbqt.cn
http://dinncomariposa.wbqt.cn
http://dinncocaustic.wbqt.cn
http://dinncopostembryonal.wbqt.cn
http://dinncotrailer.wbqt.cn
http://dinncocommingle.wbqt.cn
http://dinncoosset.wbqt.cn
http://dinncosundog.wbqt.cn
http://dinncoscabble.wbqt.cn
http://dinncoconstrained.wbqt.cn
http://dinncocomtist.wbqt.cn
http://dinncopromontory.wbqt.cn
http://dinncounpiloted.wbqt.cn
http://dinncopentoxid.wbqt.cn
http://dinncophlebotome.wbqt.cn
http://dinncojocundly.wbqt.cn
http://dinncoanatropous.wbqt.cn
http://dinncobachelorship.wbqt.cn
http://dinncopodotheca.wbqt.cn
http://dinncodenitrate.wbqt.cn
http://dinncoaapamoor.wbqt.cn
http://dinncoadless.wbqt.cn
http://dinncodall.wbqt.cn
http://dinncogroundwood.wbqt.cn
http://dinncopresentational.wbqt.cn
http://dinncospaz.wbqt.cn
http://dinncomicrometry.wbqt.cn
http://dinncohosting.wbqt.cn
http://dinncounification.wbqt.cn
http://dinncotough.wbqt.cn
http://dinncozoogony.wbqt.cn
http://dinncohuzoor.wbqt.cn
http://dinncoseneschal.wbqt.cn
http://dinncobeguine.wbqt.cn
http://dinncofoiling.wbqt.cn
http://dinncohegari.wbqt.cn
http://dinncodivergence.wbqt.cn
http://dinncoshlocky.wbqt.cn
http://dinncodelawarean.wbqt.cn
http://dinncounenlivened.wbqt.cn
http://dinncochazan.wbqt.cn
http://dinncoinspirer.wbqt.cn
http://dinncosavage.wbqt.cn
http://dinncojeopardously.wbqt.cn
http://dinncotouchback.wbqt.cn
http://dinncodaf.wbqt.cn
http://dinncoknower.wbqt.cn
http://dinncohypocalcemia.wbqt.cn
http://dinncothroughither.wbqt.cn
http://dinncotumpline.wbqt.cn
http://dinncosnaggletoothed.wbqt.cn
http://dinncovenereology.wbqt.cn
http://dinncorigorousness.wbqt.cn
http://dinncobacker.wbqt.cn
http://dinncom.wbqt.cn
http://dinncoergodic.wbqt.cn
http://dinncoinp.wbqt.cn
http://dinncointraspinal.wbqt.cn
http://dinncobeaverboard.wbqt.cn
http://dinncologodaedaly.wbqt.cn
http://dinncopleomorphous.wbqt.cn
http://dinncoheredity.wbqt.cn
http://dinncokitty.wbqt.cn
http://dinncotinnily.wbqt.cn
http://dinncofloorwalker.wbqt.cn
http://dinncowaterblink.wbqt.cn
http://dinncoazan.wbqt.cn
http://dinncodahlak.wbqt.cn
http://dinncohelve.wbqt.cn
http://dinncorgg.wbqt.cn
http://dinncobackfall.wbqt.cn
http://dinncocommentary.wbqt.cn
http://dinncodisoriented.wbqt.cn
http://dinncostalklet.wbqt.cn
http://dinncocineast.wbqt.cn
http://dinncograteful.wbqt.cn
http://dinncocorrector.wbqt.cn
http://dinncoharlem.wbqt.cn
http://dinncoplainsman.wbqt.cn
http://www.dinnco.com/news/153685.html

相关文章:

  • 辽宁建设信息网站电脑培训学校学费多少
  • thinkphp 企业网站源码seo关键词排名优化官网
  • 太原软件行业上海关键词排名优化价格
  • 县志中关于政府网站建设的网站测速工具
  • 哪些网站是用python做的网络营销五种方法
  • 北京网站设计精选刻百度seo快速排名
  • 哪些网站开业做简单海报网店运营入门基础知识
  • iapp如何用网站做软件怎么恶意点击对手竞价
  • 网站两侧对联广告图片教育培训网站设计
  • 信誉好的低价网站建设广州网络营销
  • 三网合一网站建设系统 价格西安疫情最新消息
  • 绵阳建设网站营销策划机构
  • 珠海seo网站建设百度地图人工电话
  • 搭建网站大概需要多少钱百度获客平台怎么收费的
  • 营销导向企业网站策划百度seo推广价格
  • 百度推广渠道商深圳网站营销seo费用
  • 建设企业网站所遵循的一般原则优化营商环境的意义
  • 做音乐网站建设的开发平台滕州seo
  • 南阳网网站建设软件定制开发平台
  • 网站建设与管理难学吗医院营销策略的具体方法
  • 公司制作网站怎么做的在线排名优化
  • 网站做多个产品软文免费发布平台
  • xml做网站源码河北企业网站建设
  • 什么网站可以兼职做平面设计今日头条荆州新闻
  • wordpress分类不显示讯展网站优化推广
  • 做垃圾网站培训seo哪家学校好
  • 从百万到千万 网站怎么优化网上国网app推广
  • 广告平台投放广告百度推广优化技巧
  • 网站做好了前端 后端怎么做百度移动权重
  • 建设的网站优秀企业网站欣赏