#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的更多相关文章

  1. 《Head First 设计模式》之装饰者模式

    作者:Grey 原文地址:http://www.cnblogs.com/greyzeng/p/5922248.html 模式名称 装饰者模式(Decorator Pattern) 需求 定义咖啡厅中的 ...

  2. [Head First设计模式]山西面馆中的设计模式——装饰者模式

    引言 在山西面馆吃鸡蛋面的时候突然想起装饰者这个模式,觉得面馆这个场景跟书中的星巴兹咖啡的场景很像,边吃边思考装饰者模式.这里也就依葫芦画瓢,换汤不换药的用装饰者模式来模拟一碗鸡蛋面是怎么出来的吧.吃 ...

  3. Head First设计模式之装饰者模式(Decorator Pattern)

    前言: 本节将深度讨论继承滥用问题,将会学到使用对象组合的方式,在运行时装饰类,在不修改任何底层代码的情况下,给对象赋予新的职责. 1.    基本需求:咖啡连锁店业务扩张需要重新设计订单系统 背景: ...

  4. Java IO 装饰者模式

    装饰模式(Decorator) 装饰模式又名包装(Wrapper)模式. 装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式通过创建一个包装对象,也就是装饰,来包裹真实的 ...

  5. Head First 设计模式 --3 装饰者模式 开闭原则

    装饰者模式:动态的将责任附加到对象上,若要扩展功能,装饰者提供了比集成更有弹性的替代方案.设计原则:1:封装变化2:多用组合,少用继承3:针对接口编程,不针对实现编程4:为对象之间的松耦合设计而努力5 ...

  6. 装饰者模式--《Head First DesignPattern》

    装饰者模式动态地将责任附加到对象杭,若要拓展功能,装设置提供了比继承更有弹性的替代方案. 星巴兹有多种咖啡,它们具有不同的价格.在购买咖啡时,也可以要求在其中加入各种调料,例如豆浆.摩卡.奶泡等等.需 ...

  7. 【设计模式 - 9】之装饰者模式(Decorator)

    1      模式简介 装饰者模式允许向一个现有的对象添加新的功能,同时又不改变其结构. 装饰者模式的思路是用"调料"对象将原始对象进行层层包裹,同时其属性.动作层层传递,达到最终 ...

  8. java_设计模式_装饰者模式_Decorator Pattern(2016-07-28)

    装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式的结构 装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任.换言之,客户 ...

  9. 设计模式之装饰者模式(Decorator Pattern)

    一.什么是装饰者模式? 装饰者模式能够完美实现“对修改关闭,对扩展开放”的原则,也就是说我们可以在不修改被装饰者的前提下,扩展被装饰者的功能. 再来看看我们的文件操作代码: 1 InputStream ...

随机推荐

  1. Transcranial magnetic stimulation (TMS)

    Transcranial magnetic stimulation (TMS) Effect of Transcranial Magnetic Stimulation on Free Will Tra ...

  2. You Don't Know JS: this & Object Prototypes( 第4章 Mixing "Class" Objects)

    本章移到“Object oriented programming”和"classes". 看‘class orientation‘ 的设计模式: instantiation, in ...

  3. 利用phpqrcode二维码生成类库和imagecopymerge函数制拼接图片的经验

    前期准备 引入phpqrcode类库(下载地址:https://sourceforge.net/projects/phpqrcode/) PHP开启GD扩展库支持 1.利用phpqrcode生成二维码 ...

  4. DHCP机制

    DHCP概念:局域网的网络协议,使用UDP协议工作,在工作过程中,它有两个对象,DHCP客户端和DHCP服务端,DHCP服务运行在67端口和68端口. 用途:1)个内部网络或网络服务供应商自动分配IP ...

  5. SpringBoot项目Shiro的实现(二)

    在看此小节前,您可能需要先看:http://www.cnblogs.com/conswin/p/7478557.html 紧接上一篇,在上一篇我们简单实现了一个Springboot的小程序,但我们发现 ...

  6. Mono jexus

    Mono:测试环境 linux系统定义:.NET在Linux上使用的开源工程 C#语言的编译器 在Linux上用C#开发程序 jexus:测试环境linux系统Linux系统下部署Jexus应用服务器 ...

  7. Git:非Fast forward下的合并(--no-ff方式的git merge)

    创建dev分支,并且修改readme.txt的内容,然后提交 使用git merge --no-ff -m "说明内容" 分支名称合并分支 使用git log --graph -- ...

  8. 前端web的图标的样式

  9. C++ leetcode Longest Substring Without Repeating Characters

    要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...

  10. 记一次搭建vsftp服务器坑

    避免踩坑,特此记录... yum -y install vsftpd useradd -d /www -s /sbin/nologin sui # 修改vsftpd配置文件/etc/vsftpd/vs ...