UML图:
/******************************************************************** created: 2007/10/12 filename: AbstractCofee.h author: 凌剑 http://blog.csdn.net/missvip
purpose: Decorator抽象的咖啡类与抽象的修饰类 *********************************************************************/ #ifndef ABSTRACTCOFEE_H #define ABSTRACTCOFEE_H
#include <string> using namespace std;
class BeverageCofee { public: virtual double getCost() =0; virtual string getDescription () =0; };
class DecoratorCofee : public BeverageCofee { protected: DecoratorCofee(BeverageCofee *pBever); BeverageCofee *pBeverage; };
#endif |
/******************************************************************** created: 2007/10/12 filename: ConcreteCofee.h author: 凌剑 http://blog.csdn.net/missvip
purpose: Decorator具体的咖啡类与具体的修饰类 *********************************************************************/ #ifndef CONCRETECOFEE_H_H #define CONCRETECOFEE_H_H
#include "AbstractCofee.h"
class HostBlendCofee : public BeverageCofee { public: double getCost(); string getDescription(); };
class DarkHostCofee : public BeverageCofee { public: double getCost(); string getDescription(); };
class Milk : public DecoratorCofee { public: Milk(BeverageCofee *pBever); double getCost(); string getDescription (); };
class Suger : public DecoratorCofee { public: Suger(BeverageCofee *pBever); double getCost(); string getDescription (); };
#endif
|
/******************************************************************** created: 2007/10/12 filename: AbstractCofee.cpp author: 凌剑 http://blog.csdn.net/missvip
purpose: Decorator抽象的咖啡类与抽象的修饰类 *********************************************************************/ #include "AbstractCofee.h"
DecoratorCofee::DecoratorCofee(BeverageCofee *pBever):pBeverage(pBever) {
} |
/******************************************************************** created: 2007/10/12 filename: ConcreteCofee.cpp author: 凌剑 http://blog.csdn.net/missvip
purpose: Decorator具体的咖啡类与具体的修饰类 *********************************************************************/ #include "ConcreteCofee.h"
double HostBlendCofee::getCost() { return 4.0; }
string HostBlendCofee::getDescription() { return "HostBlend咖啡"; }
double DarkHostCofee::getCost() { return 5.0; }
string DarkHostCofee::getDescription() { return "DarkHost咖啡"; }
Milk::Milk(BeverageCofee *pBever):DecoratorCofee(pBever) {
}
double Milk::getCost() { return pBeverage->getCost()+1.5; }
string Milk::getDescription() { return pBeverage->getDescription()+"+牛奶"; }
Suger::Suger(BeverageCofee *pBever):DecoratorCofee(pBever) {
}
double Suger::getCost() { return pBeverage->getCost()+0.5; }
string Suger::getDescription() { return pBeverage->getDescription()+"+糖"; } |
/******************************************************************** created: 2007/10/12 filename: Main.cpp author: 凌剑 http://blog.csdn.net/missvip
purpose: 客户程序测试Decorator *********************************************************************/ #include <iostream> #include "ConcreteCofee.h" using namespace std;
int main() { BeverageCofee *OrderDarkHostCofee =new DarkHostCofee(); cout<<OrderDarkHostCofee->getDescription()<<"价格是:"<<OrderDarkHostCofee->getCost()<<endl;
//加糖后的价格 OrderDarkHostCofee =new Suger(OrderDarkHostCofee); cout<<OrderDarkHostCofee->getDescription()<<"价格是:"<<OrderDarkHostCofee->getCost()<<endl;
//加牛奶后的价格 OrderDarkHostCofee =new Milk(OrderDarkHostCofee); cout<<OrderDarkHostCofee->getDescription()<<"价格是:"<<OrderDarkHostCofee->getCost()<<endl;
return 0; } |
最后运行结果:
可以看到DarkHost咖啡的价格是5元
加入调料糖后的价格变成5+0.5