作用:

装饰器模式有助于将其他行为或责任动态附加到对象,装饰器为扩展功能提供了子类化的灵活替代方案。这也被称为包装。如果您的应用程序进行某种过滤,那么装饰器可能是考虑这项工作的良好模式。

#include <iostream>
#include <string>
using namespace std;class Car
{
protected:string _str;
public:Car(){_str = "Unknown Car";}virtual string getDescription(){return _str;}virtual double getCost() = 0;virtual ~Car(){cout<<"~Car \n";}
};class OptionsDecorator:public Car
{
public:virtual string getDescription() = 0;virtual ~OptionsDecorator(){cout<<"~OptionsDecorator"<<endl;}
};class CarModel1:public Car
{
public:CarModel1(){_str = "CarModel1";}virtual double getCost(){return 31000.23;}~CarModel1(){cout<<"CarModel1\n";}
};class Navigation:public OptionsDecorator
{Car *_b;
public:Navigation(Car *b){_b = b;}string getDescription(){return _b->getDescription() + ",Navigation";}double getCost(){return _b->getCost() + 300;}~Navigation(){cout<<"~Navigation"<<endl;delete _b;}
};class PremiumSoundSystem: public OptionsDecorator
{Car *_b;public:PremiumSoundSystem(Car *b){_b = b;}string getDescription(){return _b->getDescription() + ", PremiumSoundSystem";}double getCost(){return 0.30 + _b->getCost();}~PremiumSoundSystem(){cout << "~PremiumSoundSystem()\n";delete _b;}
};class ManualTransmission: public OptionsDecorator
{Car *_b;public:ManualTransmission(Car *b){_b = b;}string getDescription(){return _b->getDescription()+ ", ManualTransmission";}double getCost(){return 0.30 + _b->getCost();}~ManualTransmission(){cout << "~ManualTransmission()\n";delete _b;}
};int main()
{//Create our Car that we want to buyCar *b = new CarModel1();cout << "Base model of " << b->getDescription() << " costs $" << b->getCost() << "\n";//Who wants base model let's add some more featuresb = new Navigation(b);cout << b->getDescription() << " will cost you $" << b->getCost() << "\n";b = new PremiumSoundSystem(b);b = new ManualTransmission(b);cout << b->getDescription() << " will cost you $" << b->getCost() << "\n";// WARNING! Here we leak the CarModel1, Navigation and PremiumSoundSystem objects!// Either we delete them explicitly or rewrite the Decorators to take// ownership and delete their Cars when destroyed.delete b;return 0;
}

Decorate 模式相关推荐

  1. 设计模式练习:Decorate模式

    关于这个装饰模式,其实还有很多地方还要学习,还要继续去模仿一些情况,转换为As可以适用的应用程序.因为要掌握到23种常见的模式,并不是一两天的事情,还需要在实践当中更加运用. 今天练习一个设计模式,至 ...

  2. Decorate和Proxy模式

    Decorate模式 Decorate模式的几个常见的应用场景: throttle(函数节流) debounce(函数防抖) AOP 装饰者模式基本的套路就是在不改变原有的函数提供的功能的情况下,再次 ...

  3. 23种设计模式分析(4):结构型模式

    1.1.9 Composite复合模式 Composite(复合.合成.组合)模式是一种结构型模式,定义:将对象组合成树形结构以表示"部分-整体"的层次结构,它使得客户对单个对象和 ...

  4. C++23种设计模式(四)--Decorate(装饰模式)

    1.装饰模式概述 1.1.意图 动态的给一个对象添加一些额外的职责.就增加功能来说,Decorate模式相比生成子类更为灵活. 1.2.适用性 在不影响其他对象的情况下,以动态,透明的方式给单个对象添 ...

  5. 装饰者模式—设计模式

    设计模式中的装饰者模式 装饰(Decorate)模式又称为包装(Wrapper)模式.可以动态的为一个对象增加新的功能.装饰模式是一种用于代替继承的技术,无须通过继承增加子类就能扩展对象的新功能.使用 ...

  6. 第 3 次读 Effective Java,这 58 个技巧最值!

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 来源:Dong GuoChao <Effective ...

  7. Java接地气日常编码技巧

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 本文来源:http://r6d.cn/9KG9 Effec ...

  8. 读完《Effective Java》后,总结了 50 条开发技巧

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | Dong GuoChao 来源 | https ...

  9. Java中不可或缺的59个小技巧,贼好用!

    来源:https://blog.dogchao.cn/?p=70 <Effective JavaJava>名著,必读.如果能严格遵从本文的原则,以编写API的质量来苛求自己的代码,会大大提 ...

  10. 读完 Effective Java,我整理了 59 条技巧!(含pdf)

    点击⬆️方"逆锋起笔",公众号回复 编程资源领取大佬们推荐的学习资料 上一篇:CTO 写低级 Bug,致公司 70 GB 数据泄露! 作者:Dong GuoChao 链接:http ...

最新文章

  1. 安卓怎么用抖音做锁屏_香港超级推荐全网霸屏快速上排怎么做
  2. 微信扫描二维码登入实现,网页端
  3. Redis缓存使用技巧和设计方案
  4. sql over的作用及用法
  5. 学习Matlab强大的符号计算(解方程)
  6. 机器学习之数据预处理——特征编码(标签编码,独热编码)
  7. 前端学习(2658):vue3 computed
  8. 哈希表,哈希算法(C语言)
  9. Hadoop核心之MapReduce架构设计
  10. 智能手表也不安全?可能泄露用户密码
  11. 使用Angular4手把手构建符合企业实际的项目管理系统----【npm】工具的使用
  12. 网页报错404:The origin server did not find 的原因(持续更新带图并总结了其他博客的内容)
  13. 运维(19) 制作U盘启动盘安装CentOS7
  14. 如何免费在本地播放flv格式的视频
  15. 计算机专业进银行和国家电网,银行春招与国家电网那个好?
  16. 创建easyui的右键菜单(onRowContextMenu)
  17. 一文读懂背照式CMOS图像传感器
  18. Goland DEBUG报错(could not launch process: decoding dwarf section info at offset 0x0: too short)
  19. 《Python自然语言处理(第二版)-Steven Bird等》学习笔记:第02章 获得文本语料和词汇资源
  20. linux系统c语言重命名文件,C语言文件操作函数

热门文章

  1. 微信小程序中自定义select下拉选项框
  2. 【线性代数】6-6:相似矩阵(Similar Matrices)
  3. 微信扫描下载apk解决方案
  4. MeionDZ:锁相环实现倍频功能
  5. android如何增加北斗定位,安卓手机怎么设置北斗导航图文教程
  6. 使用 Sprinkles 构建您自己的类型安全版本的 Tailwind CSS
  7. MDCC 2014移动开发人员大会參会实录
  8. 2021 | VMware ESX虚拟机
  9. diskpart命令详解
  10. 公告模块phpcms