【设计模式】Head First 设计模式——装饰者模式 C++实现

news/2025/3/19 17:48:23/

设计模式最大的作用就是在变化和稳定中间寻找隔离点,然后分离它们,从而管理变化。将变化像小兔子一样关到笼子里,让它在笼子里随便跳,而不至于跳出来把你整个房间给污染掉。

设计思想

动态地将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有弹性的替代方案。

装饰类继承自超类,继承是为了有正确的类型,而非继承超类的行为。

业务场景

假定你需要给蜜雪冰城设计一套售卖系统。他们家所有的饮料假设只有描述和价格两种属性,你可能又会想到用继承去解决:先抽象一个奶茶父类,然后每种奶茶都继承该父类并实现各自的display和cost方法。

你很快就会发现这么做有很多麻烦:首先,这家奶茶店所有的奶茶种类加一起数量可能有几十种数百种,这也就意味着你的子类会有很多很多;其次,这么设计似乎并不符合实际的业务场景,人们点奶茶的时候并不是一成不变的,有的不要珍珠,有的双倍糖等等,加的调料和份数也不一样,那么最后如何描述这杯奶茶以及计算其价格就成了问题。

代码案例

#include<iostream>
#i#include<iostream>
#include<vector>
#include<algorithm>
#include <string>
#include <memory>//饮料抽象类
class Beverage {
public:virtual ~Beverage() {};virtual std::string getDescription() = 0;virtual double cost() = 0;
protected:std::string description;};
//调料装饰者类
class Condimentecorator :public Beverage {
public:virtual ~Condimentecorator() {};virtual std::string getDescription() = 0;
};//espresso 饮料类
class Espresso :public Beverage {
public:Espresso() :Beverage(){description = "Espresso";}std::string getDescription() { return description; }double cost() {return 1.99;}
};//houseblend 饮料类
class HouseBlend :public Beverage {
public:HouseBlend() :Beverage(){description = "HouseBlend";}std::string getDescription() { return description; }double cost() {return 0.89;}
};//DarkRoast 饮料类
class DarkRoast :public Beverage {
public:DarkRoast() :Beverage(){description = "DarkRoast";}std::string getDescription() { return description; }double cost() {return 0.99;}
};//Decat 饮料类
class Decat :public Beverage {
public:Decat() :Beverage(){description = "Decat";}std::string getDescription() { return description; }double cost(){return 1.05;}
};//Mocha调料装饰者
class Mocha :public Condimentecorator {public:Mocha(std::shared_ptr<Beverage> be) :Condimentecorator(), beverage(be) {}std::string getDescription() {return (beverage->getDescription() + " Mocha");}double cost(){return 0.2 + beverage->cost();}public:std::shared_ptr<Beverage> beverage;
};//Soy调料装饰者
class Soy :public Condimentecorator {
public:Soy(std::shared_ptr<Beverage> be):Condimentecorator(),beverage(be){}std::string getDescription(){return (beverage->getDescription() + " Soy");}double cost(){return 0.15 + beverage->cost();}
public:std::shared_ptr<Beverage> beverage;
};//Whip调料装饰者
class Whip :public Condimentecorator {
public:Whip(std::shared_ptr<Beverage> be) :Condimentecorator(), beverage(be) {}std::string getDescription(){return (beverage->getDescription() + " Whip");}double cost(){return 0.10 + beverage->cost();}
public:std::shared_ptr<Beverage> beverage;
};int main()
{std::shared_ptr<Beverage> beverage = std::make_shared<Espresso>();std::cout << beverage->getDescription()<<" costs: "<<beverage->cost() << std::endl;std::shared_ptr<Beverage> beverage2 = std::make_shared<DarkRoast>();std::cout << beverage2->getDescription() << " costs: " << beverage2->cost() << std::endl;beverage2 = std::make_shared<Mocha>(beverage2);std::cout << beverage2->getDescription() << " costs: " << beverage2->cost() << std::endl;beverage2 = std::make_shared<Mocha>(beverage2);std::cout << beverage2->getDescription() << " costs: " << beverage2->cost() << std::endl;beverage2 = std::make_shared<Whip>(beverage2);std::cout << beverage2->getDescription() << " costs: " << beverage2->cost() << std::endl;auto it = std::make_shared<Whip>(beverage2);std::cout << it->cost() << " " << it->getDescription() << std::endl;std::cout << it->beverage->cost() << " " << it->beverage->getDescription() << std::endl;return 0;
}

http://www.ppmy.cn/news/1070389.html

相关文章

软考(一)进制的表示(二进制,八进制,十进制,十六进制)

进制的转换 一、进制的表示 二进制&#xff08;B&#xff09;&#xff1a; 0 , 1 , 10 , 11 , 100 , 101 , 110 , 111 , 1000 0,1,10,11,100,101,110,111,1000 0,1,10,11,100,101,110,111,1000 对应的十进制是&#xff1a; 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 0,1,2,3,4,5,6…

前端基础---HTML笔记汇总一

HTML定义 HTML超文本标记语言——HyperText Markup Language。 超文本是什么&#xff1f; 链接标记是什么&#xff1f; 标记也叫标签&#xff0c;带尖括号的文本 标签分类 单标签:只有开始标签&#xff0c;没有结束标签(<br>换行 <hr>水平线 <img> 图像标…

学习哈哈哈哈

# 零、学习计划 * 数据库相关 * 索引 * [我以为我对数据库索引很了解&#xff0c;直到我遇到了阿里面试官 - 知乎 (zhihu.com)](https://zhuanlan.zhihu.com/p/107487215) * [给我一分钟&#xff0c;让你彻底明白MySQL聚簇索引和非聚簇索引 - 知乎 (zhihu.com)](ht…

震动分析国标GB/T 19873.3-2019/ISO 13373-3:2015笔记

1.国家标准 1.1震动测量 现行国家标准是&#xff1a;GB/T 19873.2-2009 机器状态监测与诊断 振动状态监测 第2部分&#xff1a;振动数据处理、分析与描述 它的起草人&#xff1a; 郑州机械研究所。西安热工研究院有限公司。东南大学。 主要起草人 韩国明 、张学延 、傅行…

mysql连接错误 errorCode 0, state 08S01

mysql连接错误 errorCode 0, state 08S01 一开始遇到这个问题的时候&#xff0c;在网上查的都是在连接数据库处添加useSSLfalse spring.datasource.urljdbc:mysql://localhost:3306/xxx?allowPublicKeyRetrievaltrue&useUnicodetrue&characterEncodingUTF-8&use…

装备一台ubuntu

配置远程连接&#xff1a; ubuntu的root用户无法远程登入问题&#xff1a; openssh安装命令&#xff1a; sudo apt-get install openssh-server 安装完成通过以下命令查看SSH是否启动 ps -e | grep ssh 如果只有ssh-agent表示还没启动&#xff0c;需要&#xff1a; /etc/i…

太阳能景观凉亭

丰富的太阳辐射能是重要的能源&#xff0c;是取之不尽、用之不竭的、无污染、廉价、人类能够自由利用的能源。太阳能每秒钟到达地面的能量高达80万千瓦&#xff0c;假如把地球表面0.1%的太阳能转为电能&#xff0c;转变率5%&#xff0c;每年发电量可达5.61012千瓦小时&#xff…

ST SR5E1 22KW OBC 3KW DC DC Combo System 二合一车载充电器解决方案

ST SR5E1 22KW OBC & 3KW DC DC Combo System 二合一车载充电器解决方案 电动车内一般有两个不同电压等级的电池&#xff0c;高压电池用于驱动电机&#xff0c;低压电瓶用于车内电子设备供电&#xff0c;两个电池之间需要一个DCDC变换器来实现功率的流动&#xff0c;根据主…