装饰者模式-c++

----------------------------------------------------装饰者模式-------------------

00基础:抽象   封装 多态 继承

00原则:

封装变化

多用组合,少用继承

针对接口编程,不针对实现编程

为交互对象之间的,松耦合设计而努力

对扩展的开放,对修改的关闭。

00模式:

策略模式

观察者模式

装饰者模式


UML类图:


Beverage类实现:

#pragma once
//#include <string>
class Beverage
{
public:
	Beverage();
	~Beverage();

	virtual std::string getDescription() = 0;
	virtual double cast() = 0;
};

 drakRoast:(焦烤咖啡)

#pragma once
#include "Beverage.h"
class darkRoast :
	public Beverage
{
public:
	darkRoast();
	~darkRoast();
	std::string getDescription();
	double cast();
};

#include "stdafx.h"
#include "darkRoast.h"


darkRoast::darkRoast()
{
}


darkRoast::~darkRoast()
{
}

std::string darkRoast::getDescription()
{
	return "darkRoast";
}

double darkRoast::cast()
{
	return 29.9;
}

houseBlend类实现:(综合咖啡)

#pragma once
#include "Beverage.h"
class houseBlend :
	public Beverage
{
public:
	houseBlend();
	~houseBlend();
	std::string  getDescription();
	double cast();
};

std::string houseBlend::getDescription()
{
	return "houseBlend";
}

double houseBlend::cast()
{
	return 19.9;
}

condimentDecorator类实现(调料装饰:)

#pragma once
#include "Beverage.h"
class condimentDecorator :
	public Beverage
{
public:
	condimentDecorator();
	virtual ~condimentDecorator();

private:
	//Beverage * pBeverage;
};

milk实现:

#pragma once
#include "condimentDecorator.h"
class mike :
	public condimentDecorator
{
public:
	mike(Beverage * ptemp);
	~mike();
	std::string getDescription();
	double cast();
private:
	Beverage *pBeverage;
};

#include "stdafx.h"
#include "mike.h"


mike::mike(Beverage * ptemp)
{
	pBeverage = ptemp;
}


mike::~mike()
{
}

std::string mike::getDescription()
{
	//std::string temp = pBeverage->getDescription();
	//int i = temp.size();
	//char kong = " ";
	//if (temp[i] == kong)   //*(temp.rbegin()) == " ")
	//{
	//	return pBeverage->getDescription() + "mike ";
	//}
	//else
	//{
		return pBeverage->getDescription() + " mike ";
	//}
	
}

double mike::cast()
{
	return 1.5 + pBeverage->cast();
}

mocha类实现:(摩卡)

#include "stdafx.h"
#include "mocha.h"


mocha::mocha(Beverage *ptemp)
{
	pBeverage = ptemp;
}


mocha::~mocha()
{
}

std::string mocha::getDescription()
{
	return pBeverage->getDescription() + " mocha ";
}
double mocha::cast()
{
	return 3.5 + pBeverage->cast();
}

soy类实现:(黄油)

#pragma once
#include "condimentDecorator.h"
class soy :
	public condimentDecorator
{
public:
	soy(Beverage * ptemp);
	~soy();
	std::string getDescription();
	double cast();
private:
	Beverage * pBeverage;
};

#include "stdafx.h"
#include "soy.h"


soy::soy(Beverage * ptemp)
{
	pBeverage = ptemp;
}


soy::~soy()
{
}

std::string soy::getDescription()
{
	return pBeverage->getDescription() + " soy ";
}
double soy::cast()
{
	return 5.0 + pBeverage->cast();
}

主函数:DecoratorTest.cpp

#include "stdafx.h"
#include "Beverage.h"
#include "condimentDecorator.h"
#include "darkRoast.h"
#include "houseBlend.h"
#include "mike.h"
#include "mocha.h"
#include "soy.h"
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	Beverage *pBever = new houseBlend();
	condimentDecorator *pcondimentDecorator = new mike(pBever);
	condimentDecorator *pcondimentDecorator2 = new mocha(pcondimentDecorator);
	condimentDecorator *soy1 = new soy(pcondimentDecorator2);
	cout <<"description: " << soy1->getDescription() << "cast : " << soy1->cast() << endl;

	
	cin.get();
	return 0;
}

运行结果:综合咖啡 + 牛奶 + 摩卡 + 黄油 需要花费:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值