装饰者模式
来源:互联网 发布:php后台管理app 编辑:程序博客网 时间:2024/05/16 07:25
装饰者模式:动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
基本模型:
利用基本组件和装饰物共同继承自一个超类的设计,在超类中拥有所需的方法接口,在所有的装饰物类中保存超类的指针,将此指针视为此装饰物装饰之前的状态,基于此实现此装饰物装饰后的状态,所有的装饰物都有之前和之后的状态,也即只完成属于自己的那个步骤,因此符合开放关闭原则。核心就是利用多态来保存各个步骤的属性,而形式完全一致。
一个例子:
#include<iostream>#include<string>using namespace std;class Beverage{public:virtual void getDescription() = 0;virtual float cost() = 0;};class HouseBlend :public Beverage{public :void getDescription()override{ cout << "HouseBlend ----- $" << cost()<<endl; }float cost()override{ return 2.1; }};class DarkRoast :public Beverage{public:void getDescription()override{ cout << "DarkRoast ----- $" << cost() << endl; }float cost()override{ return 3.0f; }};class Espresso :public Beverage{public:void getDescription()override{ cout << "Espresso ----- $" << cost() << endl; }float cost()override{ return 2.3f; }};class Decaf :public Beverage{public:void getDescription() override{ cout << "Decaf ----- $" << cost() << endl; }float cost() override{ return 3.1f; }};class CondimentDecorator :public Beverage{public:Beverage *_beverage;};class Milk :public CondimentDecorator{public:Milk(Beverage *beverage){ _beverage = beverage; };void getDescription() override { _beverage->getDescription(); cout << "Milk ----- $0.8" << endl; }float cost() override{ return 0.8f+_beverage->cost(); }};class Mocha :public CondimentDecorator{public:Mocha(Beverage *beverage){ _beverage = beverage; }void getDescription() override { _beverage->getDescription(); cout << "Mocha ----- $0.6" << endl; }float cost() override { return 0.6f + _beverage->cost(); }};class Soy :public CondimentDecorator{public:Soy(Beverage *beverage){ _beverage = beverage; }void getDescription() override { _beverage->getDescription(); cout << "Soy ----- $0.4" << endl; }float cost() override { return 0.4f + _beverage->cost(); }};class Whip :public CondimentDecorator{public:Whip(Beverage *beverage){ _beverage = beverage; }void getDescription() override { _beverage->getDescription(); cout << "Whip ----- $0.3" << endl; }float cost() override{ return 0.3f + _beverage->cost(); }};int main(){Beverage *beverage =new DarkRoast();beverage =new Milk(beverage);beverage = new Mocha(beverage);beverage = new Whip(beverage);beverage->getDescription();cout<<"Total : $"<<beverage->cost()<<endl;Beverage *beverage1 = new Decaf();beverage1 = new Soy(beverage1);beverage1 = new Mocha(beverage1);beverage1 = new Mocha(beverage1);beverage1 = new Milk(beverage1);beverage1->getDescription();cout << "Total : $"<<beverage1->cost() << endl; system("pause");return 0;}
结果:
0 0
- 装饰者模式(Derector)
- 装饰者模式
- Decorator 装饰者模式
- 装饰者设计模式
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式(Decorator)
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式 DecoratorPattern
- 设计模式 - 装饰者
- 装饰者模式
- 装饰者模式
- 装饰者模式 - 2
- 装饰者模式
- 装饰者模式
- 【百度之星资格赛】 1002 列变位法解密
- Android屏幕适配全攻略(最权威的官方适配指导)
- 第十二周项目三 日期时间类
- 5-6
- iOS浅谈如何进行网络判断
- 装饰者模式
- Python模块常用的几种安装方式
- Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(上)
- 【浅入浅出】后台代码完结篇
- 希尔排序
- c#连接Oracle库整理
- [MultiMedia][实验5(前景去除)教程]
- iOS 正则表达式判断邮箱格式,手机号码
- 文件与内存的桥梁:页缓存