:装饰者模式--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 ...
随机推荐
- python 断言大全
参考链接:https://blog.csdn.net/qq1124794084/article/details/51668672 1. 小数位模糊等于 自动化脚本最重要的是断言,正确设置断言以后才能帮 ...
- OpenGL + VS2015 + Windows10配置
官网下载OpenGL:https://www.opengl.org/resources/libraries/glut/ 解压后得到5个文件:glut.h,glut.dll,glut32.dll,glu ...
- 20165327 2017-2018-2 《JAVA程序设计》第5周学习总结
20165327 2017-2018-2 <JAVA程序设计>第5周学习总结 一.第7.10章内容小结 第7章:内部类与异常类 内容小结: 1. Java支持在一个类中声明另一个类,这样的 ...
- 2017-2018-2 20165303 实验二《Java面向对象程序设计》实验报告
实验一 实验要求 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 提交最后三个JUnit测试用例(正常情况, ...
- Hadoop/HBase Capacity Planning
http://blog.cloudera.com/blog/2010/08/hadoophbase-capacity-planning/
- linux 下如何安装memcached 和启动服务
一.安装gcc # yum -y install gcc 二.安装libevent # wget http://www.monkey.org/~provos/libevent-2.0.12-stabl ...
- Django分页器和自定义分页器
一.自定义分页器 import copy class Pagination(): def __init__(self,request,current_page,all_data_num,each_pa ...
- Oracl 12c安装
Oracl安装部署 一.前置条件准备 修改hostname: hostname oracle 修改/etc/hosts:添加192.168.10.106 oracle 添加软件开发工具 搭建yum源 ...
- [PAT]数字分类
#include <stdio.h> #include <stdlib.h> void A1(int *ar,int i); void A2(int *ar,int i); v ...
- 【其他】【PL/SQL Developer】【1】解决PL/SQL Developer过期的情况
正文: 1,开始菜单,搜索regedit,回车打开(即日常搜索电脑安装的软件的地方,regedit就是注册表) 2,按HKEY_CURRENT_USER\Software\Allround Autom ...