Decorator设计模式的一个实例

#ifndef DECORATOR_H_
#define DECORATOR_H_
#include <string>
using namespace std;
class Paragraph
{
public:
 Paragraph(const string& inInitialText) : mText(inInitialText) {}
 virtual string getHTML() const { return mText; }
protected:
 string mText;
};

class BoldParagraph : public Paragraph
{
public:
 BoldParagraph(const Paragraph& inParagraph) :
  Paragraph(""), mWrapped(inParagraph) {}
 virtual string getHTML() const {
  return "<B>" + mWrapped.getHTML() + "</B>";
 }
protected:
 const Paragraph& mWrapped;
};

class ItalicParagraph : public Paragraph
{
public:
 ItalicParagraph(const Paragraph& inParagraph) :
  Paragraph(""), mWrapped(inParagraph) {}
 virtual string getHTML() const {
  return "<I>" + mWrapped.getHTML() + "</I>";
 }
protected:
 const Paragraph& mWrapped;
}; 
#endif

#include "Player.h"
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
 Paragraph p("A party? For me? Thanks!");
 // Bold
 cout << BoldParagraph(p).getHTML() << endl;
 // Bold and Italic
 cout<<p.getHTML()<<endl;
 cout << ItalicParagraph(BoldParagraph(p)).getHTML() << endl;
 cout<<p.getHTML()<<endl;
 cout << BoldParagraph(BoldParagraph(p)) .getHTML() << endl;
 return 0;
}

输出的结果是:
<B>A party? For me? Thanks!</B>
<I><B>A party? For me? Thanks!</B></I>
(这里为什么会输出这个结果呢?因为 BoldParagraph(BoldParagraph(p)) .getHTML() 首先进入talicParagraph的getHTML()函数,然后又调用BoldParagraph 的getHTML()函数(通过虚函数的性质))
但这里存在一个副作用
cout << BoldParagraph(BoldParagraph(p)) .getHTML() << endl;
得到的结果是:
<B>A party? For me? Thanks!</B>
这是什么原因呢?
       What’s happening here is that instead of using the BoldParagraph constructor that takes a const Paragraph reference, the compiler is using the built-in copy constructor for BoldParagraph

 

另外一个例子:
UML图:

 

/********************************************************************
 
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

 

/********************************************************************
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
 

 

/********************************************************************
http://blog.csdn.net/missvip
 
*********************************************************************/
#include "AbstractCofee.h"
 
DecoratorCofee ::DecoratorCofee(BeverageCofee *pBever):pBeverage(pBever)
{
 
}

 

#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()+" +糖" ;
}

 

/********************************************************************
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值