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

手机网站打开微信登录九江做网站大概多少钱

手机网站打开微信登录,九江做网站大概多少钱,青州网站网站建设,做收益的网站多少钱深入解析装饰者模式#xff1a;动态扩展功能的艺术#xff08;C实现#xff09; 一、模式思想与应用场景 1.1 模式定义 装饰者模式#xff08;Decorator Pattern#xff09;是一种结构型设计模式#xff0c;它通过将对象放入包含行为的特殊封装对象中#xff0c;动态地…深入解析装饰者模式动态扩展功能的艺术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_ptrBeverage beverage) : beverage_(std::move(beverage)) {}std::string getDescription() const override {return beverage_-getDescription();}double cost() const override {return beverage_-cost();}protected:std::unique_ptrBeverage 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.hint main() {// 创建基础咖啡std::unique_ptrBeverage coffee std::make_uniqueEspresso();// 第一次装饰加牛奶coffee std::make_uniqueMilk(std::move(coffee));// 第二次装饰加摩卡coffee std::make_uniqueMocha(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 设计亮点 动态组合通过嵌套装饰实现功能叠加透明性装饰器与组件接口一致弹性扩展新装饰器不影响现有代码智能指针管理自动内存管理 4.2 与传统继承对比 特性继承方案装饰者模式扩展方式编译时静态扩展运行时动态组合类数量指数级增长组合爆炸线性增长功能叠加固定组合任意顺序组合维护成本修改基类影响所有子类独立扩展互不影响 4.3 实现注意事项 接口一致性装饰器必须完全实现组件接口装饰顺序不同装饰顺序可能影响最终结果内存管理 使用unique_ptr自动管理生命周期禁止拷贝操作示例中已隐式实现 性能考量多层嵌套可能影响性能 五、进阶实现技巧 5.1 装饰器撤销功能 // 移除装饰器类 template typename T class RemovableDecorator : public CondimentDecorator { public:using CondimentDecorator::CondimentDecorator;// 移除特定类型的装饰std::unique_ptrBeverage removeDecorator() {if (auto dec dynamic_castT*(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.hint main() {// 创建基础咖啡std::unique_ptrBeverage coffee std::make_uniqueEspresso();// 第一次装饰加牛奶coffee std::make_uniqueMilk(std::move(coffee));// 第二次装饰加摩卡coffee std::make_uniqueMocha(std::move(coffee));// 输出结果std::cout Order: coffee-getDescription() \n Total Cost: $ coffee-cost() std::endl;// 使用示例auto rmver std::make_uniqueRemovableDecoratorMocha(std::move(coffee));coffee rmver-removeDecorator();// 输出结果std::cout Order: coffee-getDescription() \n Total Cost after remove Mocha: $ coffee-cost() std::endl;coffee std::make_uniqueDoubleDecorator(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 实际工程案例 图形界面组件 滚动条装饰文本框边框装饰图片组件 网络通信 加密装饰数据流压缩装饰传输流 游戏开发 装备系统增强角色属性技能BUFF叠加效果 6.2 相关模式对比 模式核心区别适配器模式改变对象接口代理模式控制访问不添加功能组合模式统一处理对象集合策略模式整体替换算法 七、最佳实践指南 7.1 适用场景判断 ✅ 推荐使用 需要动态添加/撤销功能使用继承会导致类爆炸不同功能的排列组合场景 ❌ 避免使用 需要直接访问具体组件方法装饰顺序影响业务逻辑性能敏感的底层系统 7.2 实现原则 优先组合使用对象组合而非继承保持轻量装饰器不应包含复杂逻辑接口隔离定义明确的组件接口文档规范明确装饰器的叠加规则 八、总结 装饰者模式通过灵活的对象组合代替僵化的类继承为系统扩展提供了优雅的解决方案。在C实现中 使用unique_ptr管理组件生命周期通过抽象类保证接口一致性利用现代C特性简化实现 当面对需要动态扩展功能的场景时装饰者模式就像编程世界的俄罗斯套娃让每个装饰器层层包裹核心组件最终组合出强大的功能集合。
http://www.hkea.cn/news/14396188/

相关文章:

  • 做地方短租网站历史建筑信息平台
  • 默认网站 域名 网站绑定网站开发必须要用js
  • 自动优化网站软件没有了广州西樵网站制作
  • 个人网站需要哪些内容网站建设与管理(第2版)
  • 男女做爰网站基础型网站
  • 网站实现搜索功能wordpress用户名怎么设置密码
  • 网站上面怎么做链接施工企业信用管理制度和机制
  • 怎样建小型网站大宗交易平台有哪些
  • 大丰专业做网站建设银行网站最近打不开吗
  • 网站建设公司的pest分析做二手物资哪个网站好
  • 网站验证码调用网站备案帐户有什么用
  • 冲压加工瑞安有做网站吗自己免费制作app
  • 手机网站开发教程网站建设公司有哪些主要内容组成
  • 河南省建设厅网站中州杯中国网络排名前十名
  • 不用dw怎么做网站小公司网站模版
  • 郑州外贸网站建设公司沈阳市网站设计公司大全
  • php做的网站有哪些章丘网站建设
  • 网站建设有什么意义wordpress火车头采集
  • 做网站的相关规定想自己建一个公司网站怎么做
  • 电商网站建设试题东营网站建设怎么建设
  • 天津市建设行业联合会网站小兔自助建站系统
  • 凡科网站手机投票怎么做个人网站怎么做微信支付
  • l兰州网站建设网站开发公司市场
  • 建设部标准定额司网站口碑好门户网站开发
  • 苏州企业建设网站服务网站建设基本要求
  • 软件下载类型网站怎么做网站怎么seo关键词排名优化推广
  • 网站地图建设设计公司企业官网
  • 站酷海洛设计网站官网免费网站推广网站短视频
  • 申请一个免费的网站空间百度推广优化
  • thinkphp网站开发实例教程宁波公司建设网站