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

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭