// 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. for循环枚举法,全排列+dfs,补充浮点数注意事项

    其实这个题目我一直没想好应该叫什么,就是在做蓝桥杯的时候会遇到很多的题,给你一等式,abcdef...分别是1-9(||12||15)不重复问你有几种方案? 我之前一直都是用的for循环在做,听说这叫 ...

  2. Vim Clutch | 面向脚踏板编程

    简评:这是使用硬件制作的一个离合器踏板,控制 Vim 的 insert mode 和 normal mode ~ Github 上有个关于 Vim 的项目,项目作者 Aleksandr Levchuk ...

  3. fdisk命令总结

    fdisk - Partition table manipulator for Linux 一.通过fdisk -l 查看机器所挂硬盘个数及分区情况: fdisk 能划分磁盘成为若干个区,同时也能为每 ...

  4. POJ3349 Snowflake Snow Snowflakes (JAVA)

    首先声明代码并没有AC,内存超了 但我对此无能为力,有没有哪位大神好心教一下怎么写 哈希,然后比较花瓣数组,这些应该都没问题才对..唉.. 贴MLE代码 import java.util.*; pub ...

  5. Angular material mat-icon 资源参考_Toggle

    ul,li>ol { margin-bottom: 0 } dt { font-weight: 700 } dd { margin: 0 1.5em 1.5em } img { height: ...

  6. PHP的curl实现get,post 和 cookie (转)

    类似于dreamhost这类主机服务商,是显示fopen的使用 的.使用php的curl可以实现支持FTP.FTPS.HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE ...

  7. sql count中加条件

    一般的,我们会在where, 或者 having中加条件,count中只是某个字段 今天看到另外一种写法,不知道性能怎么样 select count( case when xxx>10 and ...

  8. 关于浏览器localhost点击wamp项目跳转出错

    www目录下index.php399行代码 $handle=opendir("."); $projectContents = ''; while (($file = readdir ...

  9. Python学习 day10

    一.默认参数的陷阱 先看如下例子: def func(li=[]): li.append(1) print(li) func() func() func(li=['abc']) func() 结果: ...

  10. nginx常用配置2

    ## 一.Nginx中虚拟主机配置 ### 1.基于域名的虚拟主机配置 1.修改宿主机的hosts文件(系统盘/windows/system32/driver/etc/HOSTS) ​ linux : ...