目录
一、设计原则
二、认识装饰者设计模式
三、以装饰者构造饮料的订单
四、装饰者模式框架
五、本文的类图
六、接口类
6.1饮料接口类
6.2调料接口类
七、写饮料的代码
7.1 浓缩咖啡饮料
7.2 深焙咖啡
7.3综合咖啡饮料
7.4咖啡因饮料
八、写调料的代码
8.1摩卡调料
8.2 奶泡调料
8.3 豆浆调料
8.4 牛奶调料
九、测试代码
十、项目完整代码
十二、参考资料
开放-关闭原则:类已更改对扩展开放,对修改关闭。
代码设计
价格表
--咖啡饮料价格--
HouseBlend 综合 0.89
DarkRoast 深焙 0.99
Decat 低咖啡因 1.05
Espresso 浓缩 1.99
--调料价格--
Milk 牛奶 0.10
Mocha 摩卡 0.2
Soy 豆浆 0.15
Whip 奶泡 0.10
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;
}
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)
{
}
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; //浓缩咖啡调料的价格
}
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;
}
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; //综合 咖啡饮料价格
}
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;
}
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为摩卡调料的价格
}
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为奶泡调料的价格
}
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为豆浆调料的价格
}
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设计模式》