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

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

设计思想

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

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

业务场景

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

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

代码案例

C++ 复制代码
#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;
}
相关推荐
王老师青少年编程6 分钟前
2025年12月GESP真题及题解(C++七级): 学习小组
c++·gesp·csp·信奥赛·七级·csp-s·提高组
特行独立的猫7 分钟前
python+Proxifier+mitmproxy实现监听本地网路所有的http请求
开发语言·爬虫·python·http
浩瀚地学9 分钟前
【Java】集合-Collection
java·开发语言·经验分享·笔记·学习
迷途之人不知返9 分钟前
C++初识(2)
c++
盛者无名13 分钟前
Rust语言基础
开发语言·后端·rust
wangkay8818 分钟前
【Java 转运营】Day03:抖音直播间自然流运营
java·开发语言·新媒体运营
萌新小白YXY19 分钟前
imc DEVICES raw数组转Matlab mat数据
开发语言·matlab
韩曙亮28 分钟前
【Web APIs】浏览器本地存储 ② ( window.sessionStorage 本地存储常用 API 简介 | 代码示例 )
开发语言·前端·javascript·localstorage·sessionstorage·web apis·浏览器本地存储
郑州光合科技余经理29 分钟前
私有化B2B订货系统实战:核心模块设计与代码实现
java·大数据·开发语言·后端·架构·前端框架·php
澄澈青空~30 分钟前
接续:DKOM 进程隐藏的原理
c++·安全