:装饰者模式--Beverage
#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;
}
:装饰者模式--Beverage的更多相关文章
- 《Head First 设计模式》之装饰者模式
作者:Grey 原文地址:http://www.cnblogs.com/greyzeng/p/5922248.html 模式名称 装饰者模式(Decorator Pattern) 需求 定义咖啡厅中的 ...
- [Head First设计模式]山西面馆中的设计模式——装饰者模式
引言 在山西面馆吃鸡蛋面的时候突然想起装饰者这个模式,觉得面馆这个场景跟书中的星巴兹咖啡的场景很像,边吃边思考装饰者模式.这里也就依葫芦画瓢,换汤不换药的用装饰者模式来模拟一碗鸡蛋面是怎么出来的吧.吃 ...
- Head First设计模式之装饰者模式(Decorator Pattern)
前言: 本节将深度讨论继承滥用问题,将会学到使用对象组合的方式,在运行时装饰类,在不修改任何底层代码的情况下,给对象赋予新的职责. 1. 基本需求:咖啡连锁店业务扩张需要重新设计订单系统 背景: ...
- Java IO 装饰者模式
装饰模式(Decorator) 装饰模式又名包装(Wrapper)模式. 装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式通过创建一个包装对象,也就是装饰,来包裹真实的 ...
- Head First 设计模式 --3 装饰者模式 开闭原则
装饰者模式:动态的将责任附加到对象上,若要扩展功能,装饰者提供了比集成更有弹性的替代方案.设计原则:1:封装变化2:多用组合,少用继承3:针对接口编程,不针对实现编程4:为对象之间的松耦合设计而努力5 ...
- 装饰者模式--《Head First DesignPattern》
装饰者模式动态地将责任附加到对象杭,若要拓展功能,装设置提供了比继承更有弹性的替代方案. 星巴兹有多种咖啡,它们具有不同的价格.在购买咖啡时,也可以要求在其中加入各种调料,例如豆浆.摩卡.奶泡等等.需 ...
- 【设计模式 - 9】之装饰者模式(Decorator)
1 模式简介 装饰者模式允许向一个现有的对象添加新的功能,同时又不改变其结构. 装饰者模式的思路是用"调料"对象将原始对象进行层层包裹,同时其属性.动作层层传递,达到最终 ...
- java_设计模式_装饰者模式_Decorator Pattern(2016-07-28)
装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式的结构 装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任.换言之,客户 ...
- 设计模式之装饰者模式(Decorator Pattern)
一.什么是装饰者模式? 装饰者模式能够完美实现“对修改关闭,对扩展开放”的原则,也就是说我们可以在不修改被装饰者的前提下,扩展被装饰者的功能. 再来看看我们的文件操作代码: 1 InputStream ...
随机推荐
- echarts画中国地图并上色
任务是画一个中国地图,并在指定区域上颜色,学姐说用arcgis画,乖乖,4个g的安装文件,算了, 还是echarts大法好..如果想熟悉这个牛X的工具,请移步https://www.w3cschool ...
- vs2015多行注释与取消多行注释
注释: 先CTRL+K,然后CTRL+C 取消注释: 先CTRL+K,然后CTRL+U
- Java 发送SOAP请求调用WebService,解析SOAP报文
https://blog.csdn.net/Peng_Hong_fu/article/details/80113196 记录测试代码 SoapUI调用路径 http://localhost:8082/ ...
- 批量显示QC结果的利器(转)
作者:greenhillman MultiQC homepage: http://multiqc.info功能:把多个测序结果的qc结果整合成一个报告.支持fastqc.trimmomatic.bow ...
- python 中的 and / or
逻辑运算符:and or not 优先级: not > and > or 数字:0为假, 非0为真: 字符串:空为假,非空为真: 逻辑表达式的值: x and ...
- Python Web简单加法器的实现--Python
坚持写博客来记录学习过程,哪怕学习的东西多么简单!下面是python中cgi相关知识. Template.py:(模板引擎文件) #模板引擎def start_response(resp=" ...
- 最新的vueWebpack项目
最近优化了我的vueWebpack多入口框架,感觉清新了好多:http://pan.baidu.com/s/1bNYJp0
- Sticks HDU - 1455 (未完成)
George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
- array_column的作用
从记录集中取出 last_name 列,用相应的 "id" 列作为键值: <?php // 表示由数据库返回的可能记录集的数组 $a = array( array( 'id' ...
- hadoop常见面试题
Q1.什么是 Hadoop? Hadoop 是一个开源软件框架,用于存储大量数据,并发处理/查询在具有多个商用硬件(即低成本硬件)节点的集群上的那些数据.总之,Hadoop 包括以下内容: HDFS( ...