设计模式入门,装饰着模式,c++代码实现
// 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++代码实现的更多相关文章
- Java 设计模式泛谈&装饰者模式和单例模式
设计模式(Design Pattern) 1.是一套被反复使用.多人知晓的,经过分类编目 的 代码设计经验总结.使用设计模式是为了可重用代码,让代码更容易维护以及扩展. 2.简单的讲:所谓模式就是得到 ...
- C#设计模式(9)——装饰者模式(Decorator Pattern)
一.引言 在软件开发中,我们经常想要对一类对象添加不同的功能,例如要给手机添加贴膜,手机挂件,手机外壳等,如果此时利用继承来实现的话,就需要定义无数的类,如StickerPhone(贴膜是手机类).A ...
- 设计模式之装饰者模式-java实例
设计模式之装饰者模式 需求场景 我们有了别人提供的产品,但是别人提供的产品对我们来说还不够完善,我们需要对这个产品的功能进行补强,此时可以考虑使用装饰者模式. 我们已经有了产品,而且这个产品的功能非常 ...
- 23种设计模式之装饰器模式(Decorator Pattern)
装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...
- Java设计模式 - - 单例模式 装饰者模式
Java设计模式 单例模式 装饰者模式 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 静态代理模式:https://www.cnblogs.com/StanleyBlogs/p/1 ...
- python 设计模式之装饰器模式 Decorator Pattern
#写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...
- PHP设计模式之装饰器模式(Decorator)
PHP设计模式之装饰器模式(Decorator) 装饰器模式 装饰器模式允许我们给一个类添加新的功能,而不改变其原有的结构.这种类型的类属于结构类,它是作为现有的类的一个包装 装饰器模式的应用场景 当 ...
- Python设计模式: 最佳的"策略"模式实践代码
Python设计模式: 最佳的"策略"模式实践代码 今天抽空看了下流畅的python,发现里面介绍了不少python自带的库的使用实例,用起来非常的优雅. 平时用Python来写爬 ...
- 实践GoF的23种设计模式:装饰者模式
摘要:装饰者模式通过组合的方式,提供了能够动态地给对象/模块扩展新功能的能力.理论上,只要没有限制,它可以一直把功能叠加下去,具有很高的灵活性. 本文分享自华为云社区<[Go实现]实践GoF的2 ...
随机推荐
- objectARX加载lisp函数、源码的一种方式
//感谢高飞鸟highflybird版主的思路以及研究. //先声明非公开函数acedEvaluateLisp extern int acedEvaluateLisp(const ACHAR*,str ...
- centos下搭建高可用redis
Linux下搭建高可用Redis缓存 Redis是一个高性能的key-value数据库,现时越来越多企业与应用使用Redis作为缓存服务器.楼主是一枚JAVA后端程序员,也算是半个运维工程师了.在Li ...
- Oracle数据库count的一些操作
--统计数量 select count(*) from table; --统计某一列的数量(去空) select count(col) from table; --统计某一列的值大于或小于另一个值的数 ...
- leetcode-118-Pascal's Triangle(生成具有n行的帕斯卡三角形)
题目描述: Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. Example ...
- leetcode-475-Heaters
题目描述: Winter is coming! Your first job during the contest is to design a standard heater with fixed ...
- Nginx反向代理与负载简单实现
反向代理 1.proxy_pass 通过反向代理把请求转发到百度 2.proxy_pass 既可以是ip地址,也可以是域名,同时还可以指定端口 3.proxy_pass 指定的地址携带了URI,如果前 ...
- Go语言命名
Go语言关键字 1.Go语言有25个关键字 2.关键字用途 var :用于变量的声明const :用于常量的声明type :用于声明类型func :用于声明函数和方法package :用于声明包文件i ...
- diff与patch
1.diff diff就是用来比较两个文件之间的区别的,并且是以行为单位比较的,通常用在同一文件或软件的新旧版本区别上. 用法: diff [-bBi] from-file to-file from- ...
- Spark & Python
技术文章 https://www.cnblogs.com/yangzhang-home/p/6056133.html 基于Python Spark的推荐系统 https://blog.csdn.net ...
- pandas中获取数据框的行、列数
获取数据框的行.列数 # 获取行数 df.shape[0] # 获取行数 len(df) # 获取列数 df.shape[1]