装饰者模式-c++
来源:互联网 发布:c语言编写木马程序 编辑:程序博客网 时间:2024/06/05 00:37
----------------------------------------------------装饰者模式-------------------
00基础:抽象 封装多态 继承
00原则:
封装变化
多用组合,少用继承
针对接口编程,不针对实现编程
为交互对象之间的,松耦合设计而努力
对扩展的开放,对修改的关闭。
00模式:
策略模式
观察者模式
装饰者模式
UML类图:
Beverage类实现:
#pragma once//#include <string>class Beverage{public:Beverage();~Beverage();virtual std::string getDescription() = 0;virtual double cast() = 0;};
drakRoast:(焦烤咖啡)
#pragma once#include "Beverage.h"class darkRoast :public Beverage{public:darkRoast();~darkRoast();std::string getDescription();double cast();};
#include "stdafx.h"#include "darkRoast.h"darkRoast::darkRoast(){}darkRoast::~darkRoast(){}std::string darkRoast::getDescription(){return "darkRoast";}double darkRoast::cast(){return 29.9;}
houseBlend类实现:(综合咖啡)
#pragma once#include "Beverage.h"class houseBlend :public Beverage{public:houseBlend();~houseBlend();std::string getDescription();double cast();};
std::string houseBlend::getDescription(){return "houseBlend";}double houseBlend::cast(){return 19.9;}
condimentDecorator类实现(调料装饰:)
#pragma once#include "Beverage.h"class condimentDecorator :public Beverage{public:condimentDecorator();virtual ~condimentDecorator();private://Beverage * pBeverage;};
milk实现:
#pragma once#include "condimentDecorator.h"class mike :public condimentDecorator{public:mike(Beverage * ptemp);~mike();std::string getDescription();double cast();private:Beverage *pBeverage;};
#include "stdafx.h"#include "mike.h"mike::mike(Beverage * ptemp){pBeverage = ptemp;}mike::~mike(){}std::string mike::getDescription(){//std::string temp = pBeverage->getDescription();//int i = temp.size();//char kong = " ";//if (temp[i] == kong) //*(temp.rbegin()) == " ")//{//return pBeverage->getDescription() + "mike ";//}//else//{return pBeverage->getDescription() + " mike ";//}}double mike::cast(){return 1.5 + pBeverage->cast();}
mocha类实现:(摩卡)
#include "stdafx.h"#include "mocha.h"mocha::mocha(Beverage *ptemp){pBeverage = ptemp;}mocha::~mocha(){}std::string mocha::getDescription(){return pBeverage->getDescription() + " mocha ";}double mocha::cast(){return 3.5 + pBeverage->cast();}
soy类实现:(黄油)
#pragma once#include "condimentDecorator.h"class soy :public condimentDecorator{public:soy(Beverage * ptemp);~soy();std::string getDescription();double cast();private:Beverage * pBeverage;};
#include "stdafx.h"#include "soy.h"soy::soy(Beverage * ptemp){pBeverage = ptemp;}soy::~soy(){}std::string soy::getDescription(){return pBeverage->getDescription() + " soy ";}double soy::cast(){return 5.0 + pBeverage->cast();}
主函数:DecoratorTest.cpp
#include "stdafx.h"#include "Beverage.h"#include "condimentDecorator.h"#include "darkRoast.h"#include "houseBlend.h"#include "mike.h"#include "mocha.h"#include "soy.h"#include <vector>using namespace std;int _tmain(int argc, _TCHAR* argv[]){Beverage *pBever = new houseBlend();condimentDecorator *pcondimentDecorator = new mike(pBever);condimentDecorator *pcondimentDecorator2 = new mocha(pcondimentDecorator);condimentDecorator *soy1 = new soy(pcondimentDecorator2);cout <<"description: " << soy1->getDescription() << "cast : " << soy1->cast() << endl;cin.get();return 0;}
运行结果:综合咖啡 + 牛奶 + 摩卡 + 黄油 需要花费:
0 0
- 装饰者模式(c++)
- 装饰者模式(C++)
- 装饰者模式(C++)
- C#--设计模式之装饰者模式
- 装饰模式(c++)
- 【C++】Chapter3:装饰模式
- Objective C--装饰模式
- Objective C 装饰模式
- 装饰模式 C#
- 【C++】装饰器模式
- 装饰模式(C++)
- (C++)设计模式------装饰者模式 decorator
- 设计模式——装饰者模式(c++)
- 设计模式——装饰者模式(C++)
- C语言设计模式:装饰模式
- 大话设计模式-装饰模式(C++)
- 装饰者模式(Derector)
- 装饰者模式
- 2013-12-19管理员权限设置
- 嵌入式linux应用程序移植方法总结
- JAVA学习.JavaBean概念的描述
- 测试
- 火狐浏览器下载腾讯视频优酷视频组件
- 装饰者模式-c++
- linux终端操作 自己用
- #ifndef#define#endif的用法(整理)
- CO成本中心
- 新手一步一步OpenCV+Win7+Visual Studio 2013环境配置
- java 解析 xml
- 提高 Linux 上 socket 性能
- Java百问
- ubuntu 12.04无线网卡驱动问题