// 装饰者模式.cpp : Defines the entry point for the console application.
// 参考headfirst设计模式
# include "stdafx.h"
/*
-------------
IBeverage
-------------
|
|
_________|________________________________________
| | |
------------- ------------- |
Esoresso DarkRoast -----------------------
------------- ------------- ICondimentDecorator
-----------------------
|
|
__________|_______________
| |
------------- -------------
Mocha Whip
------------- -------------
*/
//接口Beverage
//装饰者和被装饰者的共同基类
struct IBeverage
{
public :
virtual double Cost() = 0;
virtual void Desc() = 0;
};
//装饰者类基类
class ICondimentDecorator : public IBeverage
{
};
//具体被装饰者,这是一种饮料
class Esoresso : public IBeverage
{
public :
double Cost()
{
return 1. 99;
}
void Desc()
{
printf( " Esoresso ");
}
};
//具体被装饰者,这是另一种饮料
class DarkRoast : public IBeverage
{
public :
double Cost()
{
return 2. 99;
}
void Desc()
{
printf( " DarkRoast ");
}
};
//具体装饰者,这是一种调料
class Mocha : public ICondimentDecorator
{
IBeverage * m_pBeverage;
public :
Mocha(IBeverage * pBeverage)
{
m_pBeverage = pBeverage;
}
double Cost()
{
return 0. 20 + m_pBeverage - >Cost();
}
void Desc()
{
m_pBeverage - >Desc();
printf( " Mocha ");
}
};
//具体装饰者,这是另一种调料
class Whip : public ICondimentDecorator
{
IBeverage * m_pBeverage;
public :
Whip(IBeverage * pBeverage)
{
m_pBeverage = pBeverage;
}
double Cost()
{
return 0. 10 + m_pBeverage - >Cost();
}
void Desc()
{
m_pBeverage - >Desc();
printf( " Whip ");
}
};
int _tmain( int argc, _TCHAR * argv[])
{
//客户代码
IBeverage * pBeverage = new DarkRoast(); //来杯饮料
pBeverage = new Mocha(pBeverage); //加点Mocha
pBeverage = new Whip(pBeverage); //加点Whip
//看看我们的饮料及价格
pBeverage - >Desc();
printf( "/n----------------/nCost: %lf RMB/n/n", pBeverage - >Cost());
pBeverage = new Esoresso(); //再来杯饮料
pBeverage = new Mocha(pBeverage); //加点Mocha
pBeverage = new Mocha(pBeverage); //再加点Mocha
pBeverage = new Whip(pBeverage); //加点Whip
//再来看看我们的饮料及价格
pBeverage - >Desc();
printf( "/n----------------/nCost: %lf RMB/n/n", pBeverage - >Cost());
return 0;
}
// 参考headfirst设计模式
# include "stdafx.h"
/*
-------------
IBeverage
-------------
|
|
_________|________________________________________
| | |
------------- ------------- |
Esoresso DarkRoast -----------------------
------------- ------------- ICondimentDecorator
-----------------------
|
|
__________|_______________
| |
------------- -------------
Mocha Whip
------------- -------------
*/
//接口Beverage
//装饰者和被装饰者的共同基类
struct IBeverage
{
public :
virtual double Cost() = 0;
virtual void Desc() = 0;
};
//装饰者类基类
class ICondimentDecorator : public IBeverage
{
};
//具体被装饰者,这是一种饮料
class Esoresso : public IBeverage
{
public :
double Cost()
{
return 1. 99;
}
void Desc()
{
printf( " Esoresso ");
}
};
//具体被装饰者,这是另一种饮料
class DarkRoast : public IBeverage
{
public :
double Cost()
{
return 2. 99;
}
void Desc()
{
printf( " DarkRoast ");
}
};
//具体装饰者,这是一种调料
class Mocha : public ICondimentDecorator
{
IBeverage * m_pBeverage;
public :
Mocha(IBeverage * pBeverage)
{
m_pBeverage = pBeverage;
}
double Cost()
{
return 0. 20 + m_pBeverage - >Cost();
}
void Desc()
{
m_pBeverage - >Desc();
printf( " Mocha ");
}
};
//具体装饰者,这是另一种调料
class Whip : public ICondimentDecorator
{
IBeverage * m_pBeverage;
public :
Whip(IBeverage * pBeverage)
{
m_pBeverage = pBeverage;
}
double Cost()
{
return 0. 10 + m_pBeverage - >Cost();
}
void Desc()
{
m_pBeverage - >Desc();
printf( " Whip ");
}
};
int _tmain( int argc, _TCHAR * argv[])
{
//客户代码
IBeverage * pBeverage = new DarkRoast(); //来杯饮料
pBeverage = new Mocha(pBeverage); //加点Mocha
pBeverage = new Whip(pBeverage); //加点Whip
//看看我们的饮料及价格
pBeverage - >Desc();
printf( "/n----------------/nCost: %lf RMB/n/n", pBeverage - >Cost());
pBeverage = new Esoresso(); //再来杯饮料
pBeverage = new Mocha(pBeverage); //加点Mocha
pBeverage = new Mocha(pBeverage); //再加点Mocha
pBeverage = new Whip(pBeverage); //加点Whip
//再来看看我们的饮料及价格
pBeverage - >Desc();
printf( "/n----------------/nCost: %lf RMB/n/n", pBeverage - >Cost());
return 0;
}