// 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++代码实现的更多相关文章

  1. Java 设计模式泛谈&装饰者模式和单例模式

    设计模式(Design Pattern) 1.是一套被反复使用.多人知晓的,经过分类编目 的 代码设计经验总结.使用设计模式是为了可重用代码,让代码更容易维护以及扩展. 2.简单的讲:所谓模式就是得到 ...

  2. C#设计模式(9)——装饰者模式(Decorator Pattern)

    一.引言 在软件开发中,我们经常想要对一类对象添加不同的功能,例如要给手机添加贴膜,手机挂件,手机外壳等,如果此时利用继承来实现的话,就需要定义无数的类,如StickerPhone(贴膜是手机类).A ...

  3. 设计模式之装饰者模式-java实例

    设计模式之装饰者模式 需求场景 我们有了别人提供的产品,但是别人提供的产品对我们来说还不够完善,我们需要对这个产品的功能进行补强,此时可以考虑使用装饰者模式. 我们已经有了产品,而且这个产品的功能非常 ...

  4. 23种设计模式之装饰器模式(Decorator Pattern)

    装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...

  5. Java设计模式 - - 单例模式 装饰者模式

    Java设计模式 单例模式 装饰者模式 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 静态代理模式:https://www.cnblogs.com/StanleyBlogs/p/1 ...

  6. python 设计模式之装饰器模式 Decorator Pattern

    #写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...

  7. PHP设计模式之装饰器模式(Decorator)

    PHP设计模式之装饰器模式(Decorator) 装饰器模式 装饰器模式允许我们给一个类添加新的功能,而不改变其原有的结构.这种类型的类属于结构类,它是作为现有的类的一个包装 装饰器模式的应用场景 当 ...

  8. Python设计模式: 最佳的"策略"模式实践代码

    Python设计模式: 最佳的"策略"模式实践代码 今天抽空看了下流畅的python,发现里面介绍了不少python自带的库的使用实例,用起来非常的优雅. 平时用Python来写爬 ...

  9. 实践GoF的23种设计模式:装饰者模式

    摘要:装饰者模式通过组合的方式,提供了能够动态地给对象/模块扩展新功能的能力.理论上,只要没有限制,它可以一直把功能叠加下去,具有很高的灵活性. 本文分享自华为云社区<[Go实现]实践GoF的2 ...

随机推荐

  1. BeginEditorCommand的原理

    代码来源 :http://www.arch-pub.com/problem-about-CPropertyPage_10682271.html CWnd* pAcadWnd = CWnd::FromH ...

  2. HTTP请求的两种方式get和post的区别

    1,get从服务器获取数据:post向服务器发送数据: 2,安全性,get请求的数据会显示在地址栏中,post请求的数据放在http协议的消息体: 3,从提交数据的大小看,http协议本身没有限制数据 ...

  3. 常用博客 API地址

    新浪博客 http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php 网易博客 http://os.blog.163.com/ap ...

  4. 案例3-ubuntu和centos中自动部署tomcat相关服务的脚本

    涉及redis,mysql,xtrabackup, tomcat 1. ubuntu中 #!/bin/bash #first, change to root #出错立刻中断 set -e apt-ge ...

  5. 编写第一个Go程序

    编码格式 Go语言源码文件编码格式必须是 UTF-8 格式,否则会导致编译器出错. 结束语句 在 Go 程序中,一行代表一个语句结束.每个语句不需要像其它语言一样以分号 ";"结尾 ...

  6. 128th LeetCode Weekly Contest Capacity To Ship Packages Within D Days

    A conveyor belt has packages that must be shipped from one port to another within D days. The i-th p ...

  7. thinkphp5.1的安装

    首先,下载composer.建议选择全屏安装.参考资料https://docs.phpcomposer.com/00-intro.html.安装到c盘.这个跟之前nodejs一样,都是装c盘.大概是全 ...

  8. Git命令之回退篇 git revert git reset

    Git command之回退篇 欲练回退 必先了解:HEAD.index.WorkingCopy HEAD: 当前所在的分支版本顶端的别名,也就是最新的一次commit. git commit 之后与 ...

  9. xtts v4for oracle 11g&12c(文档ID 2471245

    xtts v4for oracle 11g&12c(文档ID 2471245.1) 序号 主机 操作项目 操作内容 备注: 阶段一:初始阶段 1.1 源端 环境验证 migrate_check ...

  10. 换个角度看Salesforce之基础配置学习笔记(二)

    1. 登录后无法使用Developer Console? 先找到当前登录用户的Profie,然后勾选Profile中的View All Data(Modify All Data)即可: 2. Pers ...