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

网站怎么做跳站it培训课程

网站怎么做跳站,it培训课程,杭州房产网站建设,送给做网站的锦旗语装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许向一个现有的对象动态添加新的功能,同时不改变其结构。这种模式创建了一个装饰类,用来包装原有的类,提供了比继承更有弹性的替代方案。 核心概念…

装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许向一个现有的对象动态添加新的功能,同时不改变其结构。这种模式创建了一个装饰类,用来包装原有的类,提供了比继承更有弹性的替代方案。

核心概念

设计原则

装饰器模式遵循开闭原则(Open/Closed Principle):

  • 对扩展开放:可以通过装饰器添加新功能

  • 对修改关闭:不需要修改现有代码

主要优点

  1. 灵活扩展:比继承更灵活,可以动态添加或删除功能

  2. 避免类爆炸:不需要为每种功能组合创建子类

  3. 保持单一职责:每个装饰类只关注特定功能

  4. 运行时配置:可以在运行时选择不同的装饰组合

模式结构

主要组件

  1. Component(组件接口)

    • 定义对象的接口

    • 可以是抽象类或接口

  2. ConcreteComponent(具体组件)

    • 实现组件接口的基本功能

    • 将被装饰的对象

  3. Decorator(装饰器基类)

    • 继承/实现组件接口

    • 持有组件对象的引用

  4. ConcreteDecorator(具体装饰器)

    • 向组件添加具体的新功能

    • 可以在调用前后添加行为

      #include <iostream>
      #include <memory>
      #include <string>// ==================== 组件接口 ====================
      class Beverage {
      public:virtual std::string getDescription() const = 0;virtual double cost() const = 0;virtual ~Beverage() = default;
      };// ==================== 具体组件 ====================
      class Espresso : public Beverage {
      public:std::string getDescription() const override {return "浓缩咖啡";}double cost() const override {return 1.99;}
      };class HouseBlend : public Beverage {
      public:std::string getDescription() const override {return "家常混合咖啡";}double cost() const override {return 0.89;}
      };// ==================== 装饰器基类 ====================
      class CondimentDecorator : public Beverage {
      protected:std::unique_ptr<Beverage> beverage_;public:explicit CondimentDecorator(std::unique_ptr<Beverage> beverage): beverage_(std::move(beverage)) {}std::string getDescription() const override = 0;
      };// ==================== 具体装饰器 ====================
      class Milk : public CondimentDecorator {
      public:explicit Milk(std::unique_ptr<Beverage> beverage): CondimentDecorator(std::move(beverage)) {}std::string getDescription() const override {return beverage_->getDescription() + ",加牛奶";}double cost() const override {return beverage_->cost() + 0.20;}
      };class Mocha : public CondimentDecorator {
      public:explicit Mocha(std::unique_ptr<Beverage> beverage): CondimentDecorator(std::move(beverage)) {}std::string getDescription() const override {return beverage_->getDescription() + ",加摩卡";}double cost() const override {return beverage_->cost() + 0.30;}
      };class Whip : public CondimentDecorator {
      public:explicit Whip(std::unique_ptr<Beverage> beverage): CondimentDecorator(std::move(beverage)) {}std::string getDescription() const override {return beverage_->getDescription() + ",加奶泡";}double cost() const override {return beverage_->cost() + 0.15;}
      };// ==================== 客户端代码 ====================
      int main() {std::cout << "=== 咖啡订单系统 ===" << std::endl;// 1. 单纯浓缩咖啡auto espresso = std::make_unique<Espresso>();std::cout << "订单1: " << espresso->getDescription() << " 价格: ¥" << espresso->cost() << std::endl;// 2. 家常混合咖啡 + 双倍摩卡 + 奶泡auto houseBlend = std::make_unique<HouseBlend>();houseBlend = std::make_unique<Mocha>(std::move(houseBlend));houseBlend = std::make_unique<Mocha>(std::move(houseBlend));houseBlend = std::make_unique<Whip>(std::move(houseBlend));std::cout << "订单2: " << houseBlend->getDescription() << " 价格: ¥" << houseBlend->cost() << std::endl;// 3. 浓缩咖啡 + 牛奶 + 奶泡auto espressoWithCondiments = std::make_unique<Espresso>();espressoWithCondiments = std::make_unique<Milk>(std::move(espressoWithCondiments));espressoWithCondiments = std::make_unique<Whip>(std::move(espressoWithCondiments));std::cout << "订单3: " << espressoWithCondiments->getDescription() << " 价格: ¥" << espressoWithCondiments->cost() << std::endl;return 0;
      }

      模式变体

      1. 带状态的装饰器

      class SizeDecorator : public CondimentDecorator {enum Size { SMALL, MEDIUM, LARGE };Size size_;public:SizeDecorator(std::unique_ptr<Beverage> beverage, Size size): CondimentDecorator(std::move(beverage)), size_(size) {}std::string getDescription() const override {std::string sizeStr;switch(size_) {case SMALL: sizeStr = "小杯"; break;case MEDIUM: sizeStr = "中杯"; break;case LARGE: sizeStr = "大杯"; break;}return beverage_->getDescription() + "," + sizeStr;}double cost() const override {double baseCost = beverage_->cost();switch(size_) {case SMALL: return baseCost * 0.8;case MEDIUM: return baseCost;case LARGE: return baseCost * 1.2;}return baseCost;}
      };

      2. 接口增强装饰器

      class DiscountDecorator : public Beverage {std::unique_ptr<Beverage> beverage_;double discountRate_;public:DiscountDecorator(std::unique_ptr<Beverage> beverage, double rate): beverage_(std::move(beverage)), discountRate_(rate) {}std::string getDescription() const override {return beverage_->getDescription() + "(折扣" + std::to_string(discountRate_*100) + "%)";}double cost() const override {return beverage_->cost() * (1 - discountRate_);}// 新增方法double getOriginalPrice() const {return beverage_->cost();}
      };

      实际应用场景

    • GUI组件装饰:为UI控件添加边框、滚动条等功能

    • I/O流处理:Java/C#中的BufferedStream等

    • 中间件管道:Web框架的请求处理管道

    • 游戏角色装备:动态添加武器、防具等效果

    • 日志系统:添加时间戳、日志级别等装饰


文章转载自:

http://PL3JANwZ.gmmyn.cn
http://IhcF4UeP.gmmyn.cn
http://yj47dtyQ.gmmyn.cn
http://Ib06B0O5.gmmyn.cn
http://hu8dGAcY.gmmyn.cn
http://mRVl8Dm7.gmmyn.cn
http://Ls6JGsBH.gmmyn.cn
http://Rnjg6usd.gmmyn.cn
http://tnMLDMG3.gmmyn.cn
http://8khOC5na.gmmyn.cn
http://6HG9pzmF.gmmyn.cn
http://rqJJFQDy.gmmyn.cn
http://0PksoUxI.gmmyn.cn
http://znX3uNRp.gmmyn.cn
http://V322k5rW.gmmyn.cn
http://cWwioalm.gmmyn.cn
http://jMAvCZO3.gmmyn.cn
http://J4bROU14.gmmyn.cn
http://fODWqXDC.gmmyn.cn
http://yRMacMsO.gmmyn.cn
http://9AOxlurw.gmmyn.cn
http://pk19JlhT.gmmyn.cn
http://OCoalwsE.gmmyn.cn
http://lBG4qoaX.gmmyn.cn
http://0kHpkDz3.gmmyn.cn
http://R383Kn3k.gmmyn.cn
http://jAMcO0hh.gmmyn.cn
http://pIf0I8YN.gmmyn.cn
http://c5KARy5a.gmmyn.cn
http://38AVaeVk.gmmyn.cn
http://www.dtcms.com/wzjs/743711.html

相关文章:

  • dedecms 网站 经常无法连接如何做好搜索引擎优化工作
  • 如何禁止通过ip访问网站网页设计结果分析怎么写
  • 网站开发实验的总结pc端移动端网站开发
  • 专门做橱柜衣柜效果图的网站寻找电销团队合作
  • 2003系统做网站wordpress建站后
  • 娄底优秀网站建设wordpress主题二次元
  • 外贸网站图片嵌入式软件开发工作内容
  • 百度关键词点击排名网站优化原理
  • 做资源教程网站响应式网站是什么软件做的
  • 网站里面的视频功能怎么做js网页制作代码大全
  • 网站logo怎么做wordpress伪静态原理
  • 齐河县建设局网站免费开发微信小程序的平台
  • 上海建设工程 U盘登录哪个网站构建平台还是搭建平台
  • 阿里云服务器建网站网站建设预算知乎
  • 获得网站所有关键字wordpress文学站
  • 做网站托管服务器个人网站设计公司
  • 建新建设集团有限公司网站登封做网站优化
  • 怎么做网络销售的网站广东汕头新闻最新消息
  • 云南省建设厅官方网站不良记录开发公司给物业公司的通知函
  • 甘肃网站推广个人网站建设的目的
  • 网站开发商品排序逻辑单位网站建设意见建议
  • 哪一家做网站好广州网站建设哪家技术好
  • 青岛北京网站建设公司深圳关键词排名seo
  • 霸州网站制作个人空间地址怎么注册
  • 网站无icp备案定西建设厅网站
  • 衡水淘宝的网站建设常州网约车驾驶员资格证网上报名
  • 网站目录编辑审核的注意事项上海网站备案核验单状态查询
  • 网站建设功能是什么意思最适合seo的网站源码
  • 网站建设与管理期末试卷网站建站报价
  • 上传产品网站怎么做线上教育