设计模式:装饰者模式(3)C++版
来源:互联网 发布:网站登录的数据流程图 编辑:程序博客网 时间:2024/06/07 01:41
装饰者模式:动态的将责任附加到对象上。
C++示例代码如下:
/** CONTENTS: DESIGN PATTERN, DECORATOR PATTERN* AUTHOR: YAO H. WANG* TIME: 2013-10-19 22:13:19* EDITION: 1** ALL RIGHTS RESERVED!*/#include <string>#include <iostream>using namespace std;//类class Beverage{public:string description;virtual string getDescription(){return description;}virtual double cost() = 0;};//装饰类class CondimentDecorator: public Beverage{public:Beverage *beverage;virtual string getDescription() = 0;};//具体类class Espresso: public Beverage{public:Espresso(){description = "Espresso";}double cost(){return 1.99;}};class HouseBlend: public Beverage{public:HouseBlend(){description = "HouseBlend";}double cost(){return .89;}};class DarkRoast: public Beverage{public:DarkRoast(){description = "DarkRoast";}double cost(){return .99;}};//具体装饰类class Mocha: public CondimentDecorator{public:Mocha(Beverage *beverage){CondimentDecorator::beverage = beverage;}string getDescription(){return beverage->getDescription() + ", Mocha";}double cost(){return .20 + beverage->cost();}};class Whip: public CondimentDecorator{public:Whip(Beverage *beverage){CondimentDecorator::beverage = beverage;}string getDescription(){return beverage->getDescription() + ", Whip";}double cost(){return .10 + beverage->cost();}};class Soy: public CondimentDecorator{public:Soy(Beverage *beverage){CondimentDecorator::beverage = beverage;}string getDescription(){return beverage->getDescription() + ", Soy";}double cost(){return .15 + beverage->cost();}};//测试void main(){Beverage *beverage = new Espresso();cout << beverage->getDescription() << " $" << beverage->cost() << endl;Beverage *beverage2 = new DarkRoast();beverage2 = new Mocha(beverage2);beverage2 = new Mocha(beverage2);beverage2 = new Whip(beverage2);cout << beverage2->getDescription() << " $" << beverage2->cost() << endl;Beverage *beverage3 = new HouseBlend();beverage3 = new Soy(beverage3);beverage3 = new Mocha(beverage3);beverage3 = new Whip(beverage3);cout << beverage3->getDescription() << " $" << beverage3->cost() << endl;delete beverage;delete beverage2;delete beverage3;}
- C#--设计模式之装饰者模式
- 设计模式:装饰者模式(3)C++版
- (C++)设计模式------装饰者模式 decorator
- 设计模式——装饰者模式(c++)
- 设计模式——装饰者模式(C++)
- 设计模式(3)--装饰者模式
- 设计模式3 装饰者模式
- 设计模式-3-装饰者模式
- 【设计模式】## 3、装饰者模式 ##
- Java设计模式3:装饰者模式
- 设计模式:装饰者模式(Python)
- 设计模式(装饰者模式)
- 设计模式(装饰者模式)
- 【设计模式】-装饰者模式(Wrapper)
- 设计模式:9)装饰者模式
- 设计模式(二)装饰者模式
- 设计模式-装饰者模式(6)
- 设计模式之(装饰者模式)
- uncompressing linux.....done,booting the kernel
- uva 216 Getting in Line
- zeromq and jzmq
- webwiew的使用Service入门指南
- solaris下vnc的cde、jdk窗口配置
- 设计模式:装饰者模式(3)C++版
- 详细解析Java中抽象类和接口的区别
- 微策略MicroStrategy校园招聘笔试题2013.10.16杭州
- (int)a、&a、(int)&a、(int&)a区别
- UVa 190 Circle Through Three Points (求不共线三点所确定的圆的方程)
- 基于DM6467的TVP7002 Linux驱动程序开发
- VS2010如何在控制台应用程序中添加对MFC库的支持
- 第七周
- IP报文中的校验和函数实现