装饰者模式 C++实现
2014-11-30 09:57
381 查看
装饰者模式:动态地将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有弹性的替代方案。装饰者和被装饰者必须是一样的类型,也就是有共同的超类(超类我是设计成接口或者定义虚函数),利用继承达到类型匹配。而不是利用继承获得行为。将装饰者与组件组合时,就是加入新的行为。所得的行为,不是继承自超类,而是由组合对象得来的。利用继承设计子类的行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为。然而,如果能够利用组合的做法扩展对象的行为,就可以运行时动态的扩展。通过动态的组合对象,可以写新的代码添加新功能,而无须修改现有代码。也就是开放-关闭原则。对扩展开放,对修改关闭。-----------------------------C++星巴滋咖啡实现——————————————————————————————————————
#include<iostream> #include<string> using namespace std; class Beverage { public: string describtion; virtual string getDescribition()//抽象主键,如果不设计成virtural,并不能动态的改变 { return describtion; } virtual double cost()=0; virtual ~Beverage(){} }; class CondimentDecorator:public Beverage //装饰组件抽象类 { public: virtual string getDescribition()=0;//装饰者与被装饰者必须有一样的类型,利用继承达到类型匹配 }; class Expresso :public Beverage//具体组件 { public: Expresso() { describtion="Expresso"; } double cost() { return 1.99; } }; class DarkRost :public Beverage { public: DarkRost() { describtion="DarkRost"; } double cost() { return 0.99; } }; class HouseBlend:public Beverage { public: HouseBlend() { describtion="HouseBlend"; } double cost() { return 0.89; } }; class Mocha:public CondimentDecorator//具体装饰者组件 { private: Beverage *beverage; public: Mocha(Beverage *beverage) { this->beverage=beverage; } string getDescribition() { return beverage->getDescribition() + " ,Mocha"; } double cost() { return 0.20+beverage->cost(); } }; class Whip:public CondimentDecorator { private: Beverage *beverage; string addstr; public: Whip(Beverage *beverage) { this->beverage=beverage; } string getDescribition() { return beverage->getDescribition()+" ,Whip"; } double cost() { return 0.1+beverage->cost(); } }; class Soy:public CondimentDecorator { private: Beverage *beverage; public: Soy(Beverage *beverage) { this->beverage=beverage; } string getDescribition() { return beverage->getDescribition()+" ,Soy"; } double cost() { return 0.15+beverage->cost(); } }; int main() { Beverage *beverage=new Expresso(); cout<<beverage->getDescribition()<<" $"<<beverage->cost()<<endl; Beverage *beverage1=new DarkRost(); beverage1=new Mocha(beverage1); beverage1=new Mocha(beverage1); beverage1=new Whip(beverage1); cout<<beverage1->getDescribition()<<" $"<<beverage1->cost()<<endl; delete beverage; delete beverage1; return 0; }
相关文章推荐
- 《Head First设计模式》之装饰者模式实例代码C++实现
- C++实现装饰者模式
- (C++实现)——装饰者模式(Decorator Pattern)
- 设计模式之装饰者模式(c++实现)
- 装饰者模式(Decorator)的c++实现(改进)
- 转florian大神blog之 - 从实现装饰者模式中思考C++指针和引用的选择
- 大话设计模式--装饰者模式 Decorator -- C++实现实例
- HeadFirst设计模式 之 C++实现(三):Decorator(装饰者模式)
- Head First设计模式C++实现--第三章:装饰者(Decorator)模式
- 《Head First设计模式》之装饰者模式实例代码C++实现
- 我所理解的设计模式(C++实现)——装饰者模式(Decorator Pattern)
- 从实现装饰者模式中思考C++指针和引用的选择
- 装饰者模式及C++代码实现
- 装饰者模式(Decorator)的c++实现示例
- 装饰者模式及C++实现
- Head First 设计模式 (三) 装饰者模式(decorator pattern) C++实现
- 我所理解的设计模式(C++实现)——装饰者模式(Decorator Pattern)
- 设计模式C++实现——装饰者模式
- 我所理解的设计模式(C++实现)——装饰者模式(Decorator Pattern)
- 大话设计模式--装饰者模式 Decorator -- C++实现实例