Head First设计模式C++实现-Decorator模式
2008-12-04 22:02
381 查看
#ifndef DECORATOR_H_
#define DECORATOR_H_
#include <string>
using namespace std;
class Beverage
{
public:
virtual string GetDescription()
{
return "Unknown Beverage";
}
virtual double Cost() = 0;
};
class HouseBlend : public Beverage
{
public:
HouseBlend()
{
}
double Cost()
{
return 0.89;
}
string GetDescription()
{
return "House Blend Coffee";
}
};
class DarkRoast : public Beverage
{
public:
DarkRoast()
{
}
double Cost()
{
return 0.99;
}
string GetDescription()
{
return "Dark Roast Coffee";
}
};
class CondimentDecorator : public Beverage
{
public:
CondimentDecorator(Beverage* bev)
{
beverage = bev;
}
protected:
Beverage* beverage;
};
class Soy : public CondimentDecorator
{
public:
Soy(Beverage* bev):CondimentDecorator(bev)
{
}
string GetDescription()
{
return beverage->GetDescription() + ", Soy";
}
double Cost()
{
return 0.15 + beverage->Cost();
}
};
class SteamedMilk : public CondimentDecorator
{
public:
SteamedMilk(Beverage* bev):CondimentDecorator(bev)
{
}
string GetDescription()
{
return beverage->GetDescription() + ", Steamed Milk";
}
double Cost()
{
return 0.10 + beverage->Cost();
}
};
class Whip : public CondimentDecorator
{
public:
Whip(Beverage* bev):CondimentDecorator(bev)
{
}
string GetDescription()
{
return beverage->GetDescription() + ", Whip";
}
double Cost()
{
return 0.10 + beverage->Cost();
}
};
#endif
/****************测试代码**********/
#include "Decorator.h"
#include <iostream>
using namespace std;
void main()
{
Beverage* beverage = new HouseBlend;
beverage = new SteamedMilk(beverage);
beverage = new Whip(beverage);
cout<<beverage->GetDescription()<<" $ "<<beverage->Cost()<<endl;
}
程序输出:
House Blend Coffee, Steamed Milk, Whip $ 1.09
请按任意键继续. . .
#define DECORATOR_H_
#include <string>
using namespace std;
class Beverage
{
public:
virtual string GetDescription()
{
return "Unknown Beverage";
}
virtual double Cost() = 0;
};
class HouseBlend : public Beverage
{
public:
HouseBlend()
{
}
double Cost()
{
return 0.89;
}
string GetDescription()
{
return "House Blend Coffee";
}
};
class DarkRoast : public Beverage
{
public:
DarkRoast()
{
}
double Cost()
{
return 0.99;
}
string GetDescription()
{
return "Dark Roast Coffee";
}
};
class CondimentDecorator : public Beverage
{
public:
CondimentDecorator(Beverage* bev)
{
beverage = bev;
}
protected:
Beverage* beverage;
};
class Soy : public CondimentDecorator
{
public:
Soy(Beverage* bev):CondimentDecorator(bev)
{
}
string GetDescription()
{
return beverage->GetDescription() + ", Soy";
}
double Cost()
{
return 0.15 + beverage->Cost();
}
};
class SteamedMilk : public CondimentDecorator
{
public:
SteamedMilk(Beverage* bev):CondimentDecorator(bev)
{
}
string GetDescription()
{
return beverage->GetDescription() + ", Steamed Milk";
}
double Cost()
{
return 0.10 + beverage->Cost();
}
};
class Whip : public CondimentDecorator
{
public:
Whip(Beverage* bev):CondimentDecorator(bev)
{
}
string GetDescription()
{
return beverage->GetDescription() + ", Whip";
}
double Cost()
{
return 0.10 + beverage->Cost();
}
};
#endif
/****************测试代码**********/
#include "Decorator.h"
#include <iostream>
using namespace std;
void main()
{
Beverage* beverage = new HouseBlend;
beverage = new SteamedMilk(beverage);
beverage = new Whip(beverage);
cout<<beverage->GetDescription()<<" $ "<<beverage->Cost()<<endl;
}
程序输出:
House Blend Coffee, Steamed Milk, Whip $ 1.09
请按任意键继续. . .
相关文章推荐
- Head First设计模式C++实现--第三章:装饰者(Decorator)模式
- Head First设计模式C++实现-Decorator模式
- Head First设计模式C++实现--第十章:状态(State)模式
- 《Head First设计模式》之装饰者模式实例代码C++实现
- 《Head First设计模式》-策略模式C++实现
- HeadFirst设计模式 之 C++实现(三):Decorator(装饰者模式)
- 《Head First设计模式》之装饰者模式实例代码C++实现
- Head First设计模式 C++实现-Singleton(单件模式)
- Head First设计模式C++实现--第四章:工厂(Factory)模式
- Head First设计模式C++实现--第二章:观察者(Oserver)模式
- (C++实现)——装饰者模式(Decorator Pattern)
- 常见设计模式的解析和实现(C++)之九—Decorator模式
- Decorator模式C++实现
- 我所理解的设计模式(C++实现)——装饰者模式(Decorator Pattern)
- 装饰者模式(Decorator)的c++实现示例
- Head First设计模式C++实现-Strategy模式
- 装饰模式(Decorator)C++实现
- 装饰者模式(Decorator)的c++实现(改进)
- Head First设计模式C++实现--第七章:适配器(Adapter)与外观(Facade)模式
- [GoF设计模式]Decorator模式和Observer模式的C++实现