装饰者模式-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
原创粉丝点击