/*Beverage饮料*/
class Beverage
{
public:
Beverage()
{
_description = "Unknow Beverage";
}
virtual std::string GetDescription()
{
return _description;
}
virtual double Cost() = 0;
protected:
std::string _description;
};
/*Espresso浓咖啡*/
class Espresso : public Beverage
{
public:
Espresso()
{
_description = "Espresso Coffee";
}
virtual double Cost()
{
return 1.99;
}
};
/*HouseBlend综合咖啡,星巴克独家调配咖啡*/
class HouseBlend : public Beverage
{
public:
HouseBlend()
{
_description = "House Blend Coffee";
}
virtual double Cost()
{
return 1.99;
}
};
/*Condiment调味品*/
class CondimentDecorator : public Beverage
{
public:
CondimentDecorator()
{
_description = "Condiment Decorator";
}
virtual double Cost() = 0;
};
/*Mocha调味品摩卡*/
class Mocha : public CondimentDecorator
{
public:
Mocha(Beverage *beverage)
{
_beverage = beverage;
_description = beverage->GetDescription();
_description += " + Mocha";
}
virtual double Cost()
{
return 0.2 + _beverage->Cost();
}
private:
Beverage *_beverage;
};
/*Whip调味品奶泡*/
class Whip : public CondimentDecorator
{
public:
Whip(Beverage *beverage)
{
_beverage = beverage;
_description = beverage->GetDescription();
_description += " + Whip";
}
virtual double Cost()
{
return 0.2 + _beverage->Cost();
}
private:
Beverage *_beverage;
};
int main()
{
std::cout<<"Decorator patten."<<std::endl<<std::endl;
Beverage *beverage = new Espresso(); /*未添加装饰*/
std::cout<<beverage->GetDescription().c_str()<<" , $"<<beverage->Cost()<<std::endl;
Beverage *beverage2 = new HouseBlend();
beverage2 = new Mocha(beverage2); /*添加装饰*/
beverage2 = new Mocha(beverage2); /*添加装饰*/
beverage2 = new Whip(beverage2); /*添加装饰*/
std::cout<<beverage2->GetDescription().c_str()<<" , $"<<beverage2->Cost()<<std::endl;
return 0;
}