装饰者模式 in C++

128 篇文章 0 订阅
8 篇文章 0 订阅
本文介绍了一种使用装饰者模式实现的咖啡订购系统,该系统通过动态地为基类添加职责来构建多种咖啡饮品。具体实现包括定义饮料基类及多种咖啡类型如Espresso、DarkRoast等,并通过装饰者类如Mocha、Soy和Whip为咖啡添加额外配料,从而实现了灵活的产品组合。
摘要由CSDN通过智能技术生成

一、工程目录

二、Code

1. Beverage.h

include <string>

using std::string;

class Beverage
{
public:
	virtual string getDescription()
	{
		return description;
	}

	virtual double cost() = 0;

	virtual ~Beverage()
	{
		description = "Unkonwn Beverage";
	}

protected:
	string description = "Unkonwn Beverage";
};

2. CondimentDecorator.h

#include "common/Beverage.h"

class CondimentDecorator : public Beverage
{
public:
	virtual string getDescription() = 0;
};

3. DarkRoast.h

#include "common/Beverage.h"

class DarkRoast : public Beverage
{
public:
	DarkRoast()
	{
		description = "Dark Roast Coffee";
	}

	double cost() override
	{
		return 1.45;
	}
};

4. Decat.h

#include "common/Beverage.h"

class Decat : public Beverage
{
public:
	Decat()
	{
		description = "Decat Coffee";
	}

	double cost() override
	{
		return 0.98;
	}
};

5. Espresso.h

#include "common/Beverage.h"

class Espresso : public Beverage
{
public:
	Espresso()
	{
		description = "Espresso Coffee";
	}

	double cost() override
	{
		return 1.99;
	}
};

6. HouseBlend.h

#include "common/Beverage.h"

class HouseBlend : public Beverage
{
public:
	HouseBlend()
	{
		description = "House Blend Coffee";
	}

	double cost() override
	{
		return 0.89;
	}
};

7. Mocha.h

#include "common/CondimentDecorator.h"

class Mocha : public CondimentDecorator
{
public:
	Mocha(Beverage* beverage)
	{
		this->beverage = beverage;
	}

	string getDescription() override
	{
		return beverage->getDescription() + ", Mocha";
	}

	double cost() override
	{
		return 0.2 + beverage->cost();
	}

private:
	Beverage* beverage;
};

8. Soy.h

#include "common/CondimentDecorator.h"

class Soy : public CondimentDecorator
{
public:
	Soy(Beverage* beverage)
	{
		this->beverage = beverage;
	}

	string getDescription() override
	{
		return beverage->getDescription() + ", Soy";
	}

	double cost() override
	{
		return 0.15 + beverage->cost();
	}

private:
	Beverage* beverage;
};

9. Whip.h

#include "common/CondimentDecorator.h"

class Whip : public CondimentDecorator
{
public:
	Whip(Beverage* beverage)
	{
		this->beverage = beverage;
	}

	string getDescription() override
	{
		return beverage->getDescription() + ", Whip";
	}

	double cost() override
	{
		return 0.33 + beverage->cost();
	}

private:
	Beverage* beverage;
};

三、test

1. main.cpp

#include <iostream>
#include "coffee/Espresso.h"
#include "coffee/DarkRoast.h"
#include "decorator/Soy.h"
#include "decorator/Mocha.h"
#include "decorator/Whip.h"

using namespace std;

int main(int argc, char **argv)
{
	Beverage* espresso = new Espresso();
	cout << espresso->getDescription() << " $" << espresso->cost() << endl;

	Beverage* darkRoast = new DarkRoast();
	Beverage* soy = new Soy(darkRoast);
	Beverage* mocha = new Mocha(soy);
	Beverage* whip = new Whip(mocha);
	cout << whip->getDescription() << " $" << whip->cost() << endl;

	if (espresso)   { delete espresso; 	espresso = nullptr; }
	if (darkRoast) 	{ delete darkRoast; darkRoast = nullptr;}
	if (soy) 		{ delete soy; 		soy = nullptr;		}
	if (mocha) 		{ delete mocha; 	mocha = nullptr;	}
	if (whip) 		{ delete whip; 		whip = nullptr;		}

	return 0;
}

2. result

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值