Head First 设计模式 (三) 装饰者模式(decorator pattern) C++实现
来源:互联网 发布:stm8l15x中文数据手册 编辑:程序博客网 时间:2024/05/16 15:39
装饰者模式动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方式
设计原则:类应该对扩展开放,对修改关闭
类图:
C++ 代码如下:
源码下载:http://download.csdn.net/detail/lingyunjinzhu/5184135
#ifndef BEVERAGE_H#define BEVERAGE_H#include<string>class Beverage{ public: Beverage(); virtual ~Beverage(); virtual std::string getDescription(); virtual double cost(){ return 0;}; std::string description; protected: private: // std::string description;};#endif // BEVERAGE_H#include "Beverage.h"#include <string>Beverage::Beverage():description("Unknow name"){}Beverage::~Beverage(){}std::string Beverage::getDescription(){ return description;}
#ifndef HOUSEBLEND_H#define HOUSEBLEND_H#include "Beverage.h"class HouseBlend : public Beverage{ public: HouseBlend(); ~HouseBlend(); double cost(); protected: private:};#endif // HOUSEBLEND_H#include "HouseBlend.h"HouseBlend::HouseBlend(){ description="House Blend Coffee";}HouseBlend::~HouseBlend(){ //dtor}double HouseBlend::cost(){ return 0.89;}
#ifndef DARKROAST_H#define DARKROAST_H#include "Beverage.h"class DarkRoast : public Beverage{ public: DarkRoast(); ~DarkRoast(); double cost(); protected: private:};#endif // DARKROAST_H#include "DarkRoast.h"#include<string>using namespace std;DarkRoast::DarkRoast(){ description= "DarkRoast";}DarkRoast::~DarkRoast(){ //dtor}double DarkRoast::cost(){ return 0.99;}
#ifndef ESPRESSO_H#define ESPRESSO_H#include "Beverage.h"class Espresso : public Beverage{ public: Espresso(); ~Espresso(); double cost(); protected: private:};#endif // ESPRESSO_H#include "Espresso.h"Espresso::Espresso(){ description="Espresso";}Espresso::~Espresso(){ //dtor}double Espresso::cost(){ return 1.99;}
#ifndef DECAT_H#define DECAT_H#include "Beverage.h"class Decat : public Beverage{ public: Decat(); ~Decat(); double cost(); protected: private:};#endif // DECAT_H#include "Decat.h"Decat::Decat(){ description="Decat";}Decat::~Decat(){ //dtor}double Decat::cost(){ return 1.05;}
#ifndef CONDIMENTDECORATOR_H#define CONDIMENTDECORATOR_H#include"Beverage.h"class CondimentDecorator: public Beverage{ public: CondimentDecorator(); virtual ~CondimentDecorator(); virtual std::string getDescription(){return "";}; protected: private:};#endif // CONDIMENTDECORATOR_H#include "CondimentDecorator.h"CondimentDecorator::CondimentDecorator(){ //ctor}CondimentDecorator::~CondimentDecorator(){ //dtor}
#ifndef MILK_H#define MILK_H#include "CondimentDecorator.h"class Milk : public CondimentDecorator{ public: Milk(Beverage *beverage); ~Milk(); std::string getDescription(); double cost(); protected: private: Beverage *beverage;};#endif // MILE_H#include "Milk.h"Milk::Milk(Beverage *beverage){ this->beverage=beverage;}Milk::~Milk(){ //dtor}std::string Milk::getDescription(){ return beverage->getDescription()+", Milk";}double Milk::cost(){ return beverage->cost()+0.10;}
#ifndef MOCHA_H#define MOCHA_H#include "CondimentDecorator.h"class Mocha : public CondimentDecorator{ public: Mocha(Beverage *beverage); ~Mocha(); std::string getDescription(); double cost(); protected: private: Beverage *beverage;};#endif // MOCHA_H#include "Mocha.h"Mocha::Mocha(Beverage *beverage){ this->beverage=beverage;}Mocha::~Mocha(){ //dtor}double Mocha::cost(){ return 0.20+beverage->cost();}std::string Mocha::getDescription(){ return beverage->getDescription() +", Mocha";}
#ifndef SOY_H#define SOY_H#include "CondimentDecorator.h"class Soy : public CondimentDecorator{ public: Soy(Beverage *beverage); ~Soy(); std::string getDescription(); double cost(); protected: private: Beverage *beverage;};#endif // SOY_H#include "Soy.h"Soy::Soy(Beverage *beverage){ this->beverage=beverage;}Soy::~Soy(){ //dtor}std::string Soy::getDescription(){ return beverage->getDescription()+", Spy";}double Soy::cost(){ return 0.15+beverage->cost();}
#ifndef WHIP_H#define WHIP_H#include "CondimentDecorator.h"class Whip : public CondimentDecorator{ public: Whip(Beverage *beverage); ~Whip(); std::string getDescription(); double cost(); protected: private: Beverage *beverage;};#endif // WHIP_H#include "Whip.h"Whip::Whip(Beverage *beverage){ this->beverage=beverage;}Whip::~Whip(){ //dtor}std::string Whip::getDescription(){ return beverage->getDescription()+", Whip";}double Whip::cost(){ return 0.10+beverage->cost();}
测试的主函数:
#include "Beverage.h"#include "CondimentDecorator.h"#include "DarkRoast.h"#include "Decat.h"#include "Espresso.h"#include "HouseBlend.h"#include "Milk.h"#include "Mocha.h"#include "Soy.h"#include "Whip.h"#include <iostream>#include <string>using namespace std;int main(){ 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); cout<<beverage2->getDescription()<<" $"<<beverage2->cost()<<endl; Beverage *beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); cout<<beverage3->getDescription()<<" $"<<beverage3->cost()<<endl;return 0;}
测试结果:
作者原创,转载请注明出处http://blog.csdn.net/lingyunjinzhu/article/details/8722884
- Head First 设计模式 (三) 装饰者模式(decorator pattern) C++实现
- Head First设计模式之装饰者模式(Decorator Pattern)
- Head First 设计模式——装饰器(Decorator Pattern)——Python实现
- 《Head First Design Patterns》笔记三:装饰者模式(Decorator Pattern)
- Head First设计模式C++实现--第三章:装饰者(Decorator)模式
- 设计模式系列(三)装饰者模式(Decorator Pattern)
- Head First设计模式:(三)装饰者模式
- Head First设计模式:(三)装饰者模式
- Head First装饰模式(Decorator)
- Head First 设计模式——装饰者模式(Decorator Pattern)
- 设计模式(三)The Decorator Pattern 装饰器模式
- 设计模式-装饰模式(Decorator Pattern)
- 三、装饰模式(Decorator Pattern)
- 设计模式笔记之装饰者模式(Decorator Pattern)
- 设计模式笔记3:装饰者模式(Decorator Pattern)
- C#设计模式之装饰者模式(Decorator Pattern)
- 设计模式学习(五) -- 装饰者模式 Decorator Pattern
- Java设计模式-装饰者模式(Decorator Pattern)
- 理解和使用Alsa的配置文件
- POJ 1018 滑雪
- sql性能调试-讲解set statistics io 和 set statistics time (转)
- CSS选择器的权重与优先规则
- winform版 有道词典 小程序
- Head First 设计模式 (三) 装饰者模式(decorator pattern) C++实现
- SQL学习资料总结
- ios rate app 资料收集
- JAVA计算文件的crc32校验码
- 深入研究Servlet线程安全性问题
- POJ 2951 Cake Cutting
- ctime pitfall
- Conflicting CPU frequency values detected
- MySQL 视图