<Head First 设计模式>:装饰者模式--Beverage
2015-04-19 14:29
316 查看
#ifndef __BEVERAGE_H__
#define __BEVERAGE_H__
#include <string>
using namespace std;
class Beverage
{
public:
Beverage()
{
}
virtual ~Beverage(){}
virtual string getDescripthion()
{
return "Unknown Beverage";
}
virtual float cost() = 0
{
}
};
class Espresso :public Beverage
{
public:
Espresso()
{
}
virtual ~Espresso(){}
virtual float cost()
{
return 1.99f;
}
virtual string getDescripthion()
{
return "Espresso";
}
};
class HouseBlend :public Beverage
{
public:
HouseBlend(){}
virtual ~HouseBlend(){}
virtual float cost()
{
return 0.89;
}
virtual string getDescripthion()
{
return "HouseBlend";
}
};
#endif
#ifndef __DECORATOR_H__ #define __DECORATOR_H__ #include "Beverage.h" class Decorator : public Beverage { public: Decorator(){} virtual ~Decorator(){} virtual string getDescripthion() = 0 { } virtual float cost() = 0 { } }; class Mocha : public Decorator { private: Beverage *beverage; public: Mocha(Beverage *b) { beverage = b; } virtual ~Mocha(){} virtual string getDescripthion() { return beverage->getDescripthion() + ", Mocha"; } virtual float cost() { return beverage->cost() + 0.20; } }; class Soy : public Decorator { private: Beverage *beverage; public: Soy(Beverage *b) { beverage = b; } virtual ~Soy(){} virtual string getDescripthion() { return beverage->getDescripthion() + ", Soy"; } virtual float cost() { return beverage->cost() + 0.15; } }; class Whip : public Decorator { private: Beverage *beverage; public: Whip(Beverage *b) { beverage = b; } virtual ~Whip(){} virtual string getDescripthion() { return beverage->getDescripthion() + ", Whip"; } virtual float cost() { return beverage->cost() + 0.10; } }; #endif
#include <iostream> #include "Decorator.h" using namespace std; int main() { Beverage *ber = new Espresso(); cout << ber->getDescripthion() << "+"<< ber->cost()<<endl; Beverage *ber2 = new HouseBlend(); ber2 = new Soy(ber2); ber2 = new Mocha(ber2); ber2 = new Whip(ber2); cout << ber2->getDescripthion() << "+" << ber2->cost()<<endl; return 0; }
相关文章推荐
- 我的<<Head First 设计模式>>读后感整理
- <Head First 设计模式>:单件模式:Singleton
- 浅学设计模式之装饰者<Decorator>模式 .
- <Head First 设计模式>:工厂模式1:方法模式--Pizza
- <Head First 设计模式>:迭代器模式1:Iterator
- 浅学设计模式之装饰者<Decorator>模式
- <Head First 设计模式>:模板方法模式:Beverage
- JAVA设计模式(9) —<结构型>外观模式(Facade)
- JAVA设计模式(14) —<行为型>模板方法模式(Template Method)
- 自我学习成长系列之<<FirstHead设计模式>>
- <Head First 设计模式>:工厂模式2:抽象工厂模式--Pizza
- [设计模式]<<设计模式之禅>>关于开闭原则
- (Head First 设计模式)学习笔记(3) --装饰者模式(StarBuzz咖啡店实例)
- unity3d中的设计模式之<单例模式>
- (Boolan)C++设计模式 <三> ——策略模式(Strategy)
- Android中常用的设计模式<一>
- <Java设计模式>---单一职责原则
- Java/Android设计模式<二>观察者模式
- <转载>常见的23种设计模式
- OpenCV 2 学习笔记(13): 算法的基本设计模式<4> :使用Model-View-Controller模式创建一个应用程序