深入浅出设计模式笔记 -----Decorator

读了深入浅出设计模式Decorator篇,对于Decorator有了新的了解与认识,决定将Decorator模式实现一遍,下面用C++语言将咖啡馆的例子重现。
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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值