#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
/********************************************************************
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
|
/********************************************************************
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
|
/********************************************************************
*********************************************************************/
#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;
}
|