// 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. 搭建sftp并设置不同权限的多个用户

    一, 设置相关用户,用户组,ssh配置文件 mkdir -pv /opt/ftpsite/{admin,user1,user2} groupadd sftpadmins groupadd sftpus ...

  2. UITableView编辑模式

    UITableView有两种模式,普通模式和编辑模式.在编辑模式下可以对cell进行排序.删除.插入等等. 如何进入编辑模式 调用tableView的setEditing(editing: Bool, ...

  3. Layout1:Grid(补交作业)

    Layout1:Grid 这一节我们来讲解一下一个layout:gird. 首先上一段代码: <Page x:Class="Gridstudy.MainPage" xmlns ...

  4. 浅谈Android选项卡(一)

    选项卡,这样UI设计在很多方面都存在,window,web,ios,Android. 选项卡的主要作用,不用多介绍,可以在有线的空间内,显示出更多内容,同时也是操作起来也很方便.

  5. esp32编程第一例 hollow word

    #include<stdio.h>#include"freertos/FreeRtos.h"#include"freertos/task.h"#in ...

  6. Windows/Ubuntu下,将所有文件名字列举出来并保存到txt文件中

    Windows下 使用如下的DOS命令来实现: dir /s /b > lists.txt 可以将当前路径下的所有文件的"文件路径+文件名"存储在lists.txt中. 其中 ...

  7. paraview isosurface

    参考:https://www.youtube.com/watch?v=UjoSvWdxlTA

  8. Behind the scenes of the C# yield keyword(转)

    https://startbigthinksmall.wordpress.com/2008/06/09/behind-the-scenes-of-the-c-yield-keyword/ Behind ...

  9. EXCEL 使用宏 打水印

    Sub 宏1() ' ' 宏1 宏 //用于调整列宽 加边框 ' ' 快捷键: Ctrl+w ' ActiveCell.Cells.Select ActiveCell.Cells.EntireColu ...

  10. 关于:Mac下IDEA更换Maven仓库

    一.MAC安装配置maven环境变量 1.下载maven包: 下载链接: