博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式入门,装饰着模式,c++代码实现
阅读量:7076 次
发布时间:2019-06-28

本文共 2417 字,大约阅读时间需要 8 分钟。

// 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;
}

转载于:https://www.cnblogs.com/wangting235/p/7111029.html

你可能感兴趣的文章
NAT的转化
查看>>
mysql_Calculator
查看>>
py属性方法和函数
查看>>
nginx get 变为post 解决办法
查看>>
Mantis迁移实录
查看>>
python : 'builtin_function_or_method' object is unsubscriptable
查看>>
如何实现button点击时,按钮变色
查看>>
RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.7 版本发布
查看>>
HDFS的缺点及改进策略
查看>>
linux命令行中命令
查看>>
如何在以太坊上发行自己的代币
查看>>
双击打不开office文件
查看>>
中国最早的CCIE__孙晖
查看>>
java-在非安全网络上建立可信任安全的通道(1/3)
查看>>
《Objective-C 程序设计(第4版)》书评!
查看>>
Xcode真机调试identifier not avaliable错误
查看>>
Swift中打印一个对象所属类型
查看>>
Dockerfile制作LAMP
查看>>
sublime text3安装及配置
查看>>
gitignore配置
查看>>