装饰者设计模式 C++实现

目录

一、设计原则

二、认识装饰者设计模式

三、以装饰者构造饮料的订单

四、装饰者模式框架

五、本文的类图

六、接口类

6.1饮料接口类

6.2调料接口类

七、写饮料的代码

7.1 浓缩咖啡饮料

7.2 深焙咖啡 

 7.3综合咖啡饮料

7.4咖啡因饮料 

八、写调料的代码

8.1摩卡调料

8.2 奶泡调料 

8.3 豆浆调料 

 8.4 牛奶调料

九、测试代码

十、项目完整代码

十二、参考资料


 

一、设计原则

  开放-关闭原则:类已更改对扩展开放,对修改关闭。

二、认识装饰者设计模式

装饰者设计模式 C++实现_第1张图片

三、以装饰者构造饮料的订单

装饰者设计模式 C++实现_第2张图片 

 

装饰者设计模式 C++实现_第3张图片 

 

四、装饰者模式框架

装饰者设计模式 C++实现_第4张图片 

五、本文的类图

装饰者设计模式 C++实现_第5张图片 

代码设计

价格表

--咖啡饮料价格--  
HouseBlend 综合 0.89
DarkRoast  深焙 0.99
Decat      低咖啡因 1.05
Espresso   浓缩  1.99

--调料价格-- 
Milk  牛奶 0.10
Mocha 摩卡 0.2
Soy   豆浆 0.15
Whip  奶泡 0.10

六、接口类

6.1饮料接口类

CBeverage.h

#pragma once

//饮料抽象类
class CBeverage
{
public:
	CBeverage(void);
	virtual ~CBeverage(void);
	void Init();
public:
	virtual double cost() = 0;
	virtual CString GetDescription() const;
protected:
	CString m_strDescription;
};

CBeverage.cpp 

#include "stdafx.h"
#include "Beverage.h"


CBeverage::CBeverage(void)
{
	Init();
}


CBeverage::~CBeverage(void)
{
}

void CBeverage::Init()
{
	m_strDescription = _T("Unknow Beverage");
}

CString CBeverage::GetDescription() const
{
	return m_strDescription;
}

 

6.2调料接口类

 CCondimentDecorator .h

#pragma once
#include "beverage.h"

//装饰者类
//调料抽象类
class CCondimentDecorator : public CBeverage
{
public:
	CCondimentDecorator(void);
	virtual ~CCondimentDecorator(void);
public:
	virtual CString GetDescription() const = 0;
};

CCondimentDecorator,cpp 

#include "stdafx.h"
#include "CondimentDecorator.h"


CCondimentDecorator::CCondimentDecorator(void)
{
}


CCondimentDecorator::~CCondimentDecorator(void)
{
}

七、写饮料的代码

7.1 浓缩咖啡饮料

CEspresso .h

#pragma once
#include "beverage.h"

//浓缩咖啡饮料
class CEspresso : public CBeverage
{
public:
	CEspresso(void);
	virtual ~CEspresso(void);
public:
	virtual double cost();
};

CEspresso .cpp

#include "stdafx.h"
#include "Espresso.h"


CEspresso::CEspresso(void)
{
	m_strDescription = _T("Espreso浓缩咖啡");
}


CEspresso::~CEspresso(void)
{
}

double CEspresso::cost()
{
	return 1.99;  //浓缩咖啡调料的价格
}

7.2 深焙咖啡 

CDarkRoast .h

#pragma once
#include "beverage.h"

//深焙咖啡
class CDarkRoast :
	public CBeverage
{
public:
	CDarkRoast(void);
	virtual ~CDarkRoast(void);
public:
	virtual double cost();
};

 CDarkRoast .cpp

#include "stdafx.h"
#include "DarkRoast.h"


CDarkRoast::CDarkRoast(void)
{
	m_strDescription = _T("DarkRoast深焙咖啡");
}


CDarkRoast::~CDarkRoast(void)
{
}

double CDarkRoast::cost()
{
	return 0.99;
}

 7.3综合咖啡饮料

CHouseBlend .h

#pragma once
#include "beverage.h"

//综合 咖啡饮料
class CHouseBlend :
	public CBeverage
{
public:
	CHouseBlend(void);
	virtual ~CHouseBlend(void);
	virtual double cost();
};

CHouseBlend .cpp

#include "stdafx.h"
#include "HouseBlend.h"


CHouseBlend::CHouseBlend(void)
{
	m_strDescription = _T("HouseBlend综合咖啡饮料");
}


CHouseBlend::~CHouseBlend(void)
{
}

double CHouseBlend::cost()
{
	return 0.89;  //综合 咖啡饮料价格 
}

7.4咖啡因饮料 

CDecat.h

#pragma once
#include "beverage.h"

//低咖啡因饮料
class CDecat :
	public CBeverage
{
public:
	CDecat(void);
	virtual ~CDecat(void);
public:
	virtual double cost();
};

CDecat.cpp

#include "stdafx.h"
#include "Decat.h"


CDecat::CDecat(void)
{
	m_strDescription = _T("Decat低咖啡因饮料");
}


CDecat::~CDecat(void)
{
}

double CDecat::cost()
{
	return 1.05;
}

八、写调料的代码

8.1摩卡调料

CMocha .h

#pragma once
#include "condimentdecorator.h"
class CBeverage;
class CMocha :
	public CCondimentDecorator
{
public:
	CMocha(CBeverage* pBeverage);
	virtual ~CMocha(void);
public:
	virtual CString GetDescription() const;
	virtual double cost();
private:
	CBeverage* m_pBeverage;
};

 CMocha .cpp

#include "stdafx.h"
#include "Mocha.h"
#include "Beverage.h"
//调料-摩卡
CMocha::CMocha(CBeverage* pBeverage)
{
	m_pBeverage = pBeverage;
}


CMocha::~CMocha(void)
{
}

CString CMocha::GetDescription() const
{
	return m_pBeverage->GetDescription() + _T(", Mocha");
}

double CMocha::cost()
{
	return 0.2 + m_pBeverage->cost(); //0.2为摩卡调料的价格
}

8.2 奶泡调料 

Whip .h

#pragma once
#include "condimentdecorator.h"
class CBeverage;

//奶泡调料
class CWhip :
	public CCondimentDecorator
{
public:
	CWhip(CBeverage* pBeverage);
	virtual ~CWhip(void);
public:
	virtual CString GetDescription() const;
	virtual double cost();
private:
	CBeverage* m_pBeverage;
};

 Whip .cpp

#include "stdafx.h"
#include "Whip.h"


CWhip::CWhip(CBeverage* pBeverage)
{
	m_pBeverage = pBeverage;
}


CWhip::~CWhip(void)
{
}

CString CWhip::GetDescription() const
{
	return m_pBeverage->GetDescription() + _T(", Mocha");
}

double CWhip::cost()
{
	return 0.1 + m_pBeverage->cost(); //0.1为奶泡调料的价格
}

8.3 豆浆调料 

Soy.h

#pragma once
#include "condimentdecorator.h"
class CBeverage;

//豆浆调料
class CSoy :
	public CCondimentDecorator
{
public:
	CSoy(CBeverage* pBeverage);
	virtual ~CSoy(void);
public:
	virtual CString GetDescription() const;
	virtual double cost();
private:
	CBeverage* m_pBeverage;
};

 Soy.cpp

#include "stdafx.h"
#include "Soy.h"


CSoy::CSoy(CBeverage* pBeverage)
{
	m_pBeverage = pBeverage;
}


CSoy::~CSoy(void)
{
}

CString CSoy::GetDescription() const
{
	return m_pBeverage->GetDescription() + _T(", Mocha");
}

double CSoy::cost()
{
	return 0.15 + m_pBeverage->cost(); //0.15为豆浆调料的价格
}

 8.4 牛奶调料

Milk.h

#pragma once
#include "condimentdecorator.h"
class CBeverage;

//牛奶调料
class CMilk :
	public CCondimentDecorator
{
public:
	CMilk(CBeverage* pBeverage);
	virtual ~CMilk(void);
public:
	virtual CString GetDescription() const;
	virtual double cost();
private:
	CBeverage* m_pBeverage;
};

 Milk.cpp

#include "stdafx.h"
#include "Milk.h"


CMilk::CMilk(CBeverage* pBeverage)
{
	m_pBeverage = pBeverage;
}


CMilk::~CMilk(void)
{
}

CString CMilk::GetDescription() const
{
	return m_pBeverage->GetDescription() + _T(", Mocha");
}

double CMilk::cost()
{
	return 0.1 + m_pBeverage->cost(); //0.1为牛奶调料的价格
}

 

九、测试代码

StartbuzzCoffee.h

#pragma once
class CStartbuzzCoffee
{
public:
	CStartbuzzCoffee(void);
	~CStartbuzzCoffee(void);
public:
	void Main();
};

StartbuzzCoffee.cpp 

#include "stdafx.h"
#include "StartbuzzCoffee.h"
#include "Beverage.h"
#include "Espresso.h"
#include "DarkRoast.h"
#include "Mocha.h"
#include "Whip.h"
#include "HouseBlend.h"
#include "Soy.h"


CStartbuzzCoffee::CStartbuzzCoffee(void)
{
}


CStartbuzzCoffee::~CStartbuzzCoffee(void)
{
}

void CStartbuzzCoffee::Main()
{
	//订一杯CEspresso(浓缩咖啡)饮料,不需要调料
	CBeverage* m_pBeverage = new CEspresso;  
	CString strCost;
	CString strResult;
	strCost.Format(_T("%.2f"), m_pBeverage->cost());
	strResult = m_pBeverage->GetDescription() + _T(" $") + strCost;  //打印出它的描述与价格
	AfxMessageBox(strResult, MB_OK);

	//订一杯 CDarkRoast深焙咖啡 饮料
	CBeverage* m_pBeverage2 = new CDarkRoast;  //深焙咖啡 饮料    0.99$
	m_pBeverage2 = new CMocha(m_pBeverage2);   //添加调料摩卡装饰 0.2$
	m_pBeverage2 = new CMocha(m_pBeverage2);   //添加调料摩卡装饰 0.2$
	m_pBeverage2 = new CWhip(m_pBeverage2);    //添加调料奶泡装饰 0.1$
	strCost.Format(_T("%.2f"), m_pBeverage2->cost());  //=1.49$
	strResult = m_pBeverage2->GetDescription() + _T(" $") + strCost;  //打印出它的描述与价格
	AfxMessageBox(strResult, MB_OK);

	//订一杯 CHouseBlend综合咖啡 饮料
	CBeverage* m_pBeverage3 = new CHouseBlend; //综合咖啡 饮料     0.89$                    
	m_pBeverage3 = new CSoy(m_pBeverage3);     //添加调料豆浆装饰  0.15$
	m_pBeverage3 = new CMocha(m_pBeverage3);   //添加调料摩卡装饰  0.2$
	m_pBeverage3 = new CWhip(m_pBeverage3);    //添加调料奶泡装饰  0.1$
	strCost.Format(_T("%.2f"), m_pBeverage3->cost());  //=1.34$
	strResult = m_pBeverage3->GetDescription() + _T(" $") + strCost;  //打印出它的描述与价格
	AfxMessageBox(strResult, MB_OK);

	delete m_pBeverage;
	delete m_pBeverage2;
	delete m_pBeverage3;

}

/*
--咖啡饮料价格--  
HouseBlend 综合 0.89
DarkRoast  深焙 0.99
Decat      低咖啡因 1.05
Espresso   浓缩  1.99

--调料价格-- 
Milk  牛奶 0.10
Mocha 摩卡 0.2
Soy   豆浆 0.15
Whip  奶泡 0.10

*/

十、项目完整代码

https://github.com/mc-liyanliang/Design-patterns/tree/master/DecoratorPattern

十二、参考资料

《Head First设计模式》 

 

 

你可能感兴趣的:(设计模式,设计模式,C++)