设计模式入门,装饰着模式,c++代码实现
// test03.cpp : Defines the entry point for the console application.
//
//设计模式第3章 装饰者模式
#include "stdafx.h"
#include <string>
#include <iostream>
//#include <cstring>
using namespace std;
class Beverage
{
public:
string description /*= "Unkown Beverage"*/;//只有静态整型常量数据成员能在类中初始化
public:
virtual string getDescription()//必须为虚函数
{
return description;
}
virtual double cost(){return 0;} ;//必须为虚函数
};
class CondimentDecorator : public Beverage
{
virtual string getDescription(){return NULL;} ;
};
class Espresso : public Beverage
{
public:
Espresso()
{
description = "Espresso";
}
double cost(){
return 1.99;
}
};
class HouseBlend : public Beverage
{
public:
HouseBlend()
{
description = "House Blend Coffee";
}
double cost()
{
return .89;
}
};
class DarkRoast : public Beverage
{
public:
DarkRoast()
{
description = "Dark Roast Coffee";
}
double cost()
{
return .99;
}
};
class Mocha : public CondimentDecorator
{
Beverage* beverage;
public:
Mocha(Beverage* beverage)//必须为指针
{
this->beverage = beverage;
}
string getDescription()
{
return beverage->getDescription() + ", Mocha";
}
double cost()
{
return .20 + beverage->cost();
}
};
class Whip : public CondimentDecorator
{
Beverage* beverage;
public:
Whip(Beverage* beverage)
{
this->beverage = beverage;
}
string getDescription()
{
return beverage->getDescription() + ", Whip";
}
double cost()
{
return .10 + beverage->cost();
}
};
class Soy : public CondimentDecorator
{
Beverage* beverage;
public:
Soy(Beverage* beverage)
{
this->beverage = beverage;
}
string getDescription()
{
return beverage->getDescription() + ", Soy";
}
double cost()
{
return .15 + beverage->cost();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
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);
//printf("%s %$ %f",beverage2->getDescription(),beverage2->cost());//printf 不能输出string类型
cout<<beverage2->getDescription()<<" $ "<<beverage2->cost()<<endl;
Beverage* beverage3 = new HouseBlend();
beverage3 = new Soy(beverage3);
beverage3 = new Mocha(beverage3);
beverage3 = new Whip(beverage3);
//printf("%s %$ %f",beverage3->getDescription(),beverage3->cost());
cout<<beverage3->getDescription()<<" $ "<<beverage3->cost()<<endl;
return 0;
}
设计模式入门,装饰着模式,c++代码实现的更多相关文章
- Java 设计模式泛谈&装饰者模式和单例模式
设计模式(Design Pattern) 1.是一套被反复使用.多人知晓的,经过分类编目 的 代码设计经验总结.使用设计模式是为了可重用代码,让代码更容易维护以及扩展. 2.简单的讲:所谓模式就是得到 ...
- C#设计模式(9)——装饰者模式(Decorator Pattern)
一.引言 在软件开发中,我们经常想要对一类对象添加不同的功能,例如要给手机添加贴膜,手机挂件,手机外壳等,如果此时利用继承来实现的话,就需要定义无数的类,如StickerPhone(贴膜是手机类).A ...
- 设计模式之装饰者模式-java实例
设计模式之装饰者模式 需求场景 我们有了别人提供的产品,但是别人提供的产品对我们来说还不够完善,我们需要对这个产品的功能进行补强,此时可以考虑使用装饰者模式. 我们已经有了产品,而且这个产品的功能非常 ...
- 23种设计模式之装饰器模式(Decorator Pattern)
装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...
- Java设计模式 - - 单例模式 装饰者模式
Java设计模式 单例模式 装饰者模式 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 静态代理模式:https://www.cnblogs.com/StanleyBlogs/p/1 ...
- python 设计模式之装饰器模式 Decorator Pattern
#写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...
- PHP设计模式之装饰器模式(Decorator)
PHP设计模式之装饰器模式(Decorator) 装饰器模式 装饰器模式允许我们给一个类添加新的功能,而不改变其原有的结构.这种类型的类属于结构类,它是作为现有的类的一个包装 装饰器模式的应用场景 当 ...
- Python设计模式: 最佳的"策略"模式实践代码
Python设计模式: 最佳的"策略"模式实践代码 今天抽空看了下流畅的python,发现里面介绍了不少python自带的库的使用实例,用起来非常的优雅. 平时用Python来写爬 ...
- 实践GoF的23种设计模式:装饰者模式
摘要:装饰者模式通过组合的方式,提供了能够动态地给对象/模块扩展新功能的能力.理论上,只要没有限制,它可以一直把功能叠加下去,具有很高的灵活性. 本文分享自华为云社区<[Go实现]实践GoF的2 ...
随机推荐
- django2使用xadmin打造适合国人的后台管理系统(1)
python火了之后,学习python的人也越来越多了,python做web开发的话,flask.django是比较火的框架了,django是一个比较大的框架,也是一个快速开发利器.但是,django ...
- JS 与 OC 交互
WebView与JS的几种交互 IOS中 使用JavaScriptCore 实现OC与JS的交互 JavaScriptCore 使用
- 蒙版 mask
一句话理解: "被蒙版"层 只显示的区域为: "蒙版"层中不透明的部分 (即:最终显示的内容是父层的, 区域大小受蒙版不透明部分控制)
- Codeforces - 规律题 [占坑]
发现自己容易被卡水题,需要强行苟一下规律题 CF上并没有对应的tag,所以本题集大部分对应百毒搜索按顺序刷 本题集侧重于找规律的过程(不然做这些垃圾题有什么用) Codeforces - 1008C ...
- CentOS7 firewalld打开关闭防火墙 开放端口
firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status fir ...
- 2.使用ngx_http_auth_basic_module模块为不带认证的资源添加授权
1.首先需要生成用户名和密码 使用openssl来生成,生成命令(openssl在安装nginx的时候已经安装) echo "kibana:$(openssl passwd -crypt y ...
- Android微信支付流程及返回码-1之坑
http://www.51testing.com/html/36/n-3724336.html 之前做微信支付的时候,直接是以库形式引入项目的,虽然一直觉得微信支付的开发文档不太理想,但是印象中也没有 ...
- 腾讯地图添加多个Marker
//重置地图 init(){ var self = this; this.wSize = { wHeight: window.innerHeight-, wWidth: window.innerWid ...
- ifconfig command not found
CentOS minimal 命令做了修改 可以运行 ip addr
- Android Watchdog
http://androidxref.com/6.0.1_r10/xref/frameworks/base/services/core/java/com/android/server/Watchdog ...