`
kmplayer
  • 浏览: 510049 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Decorator模式

阅读更多
1,核心:装饰者和被装饰者来自共同的基类.
被装饰者由装饰者一层层地进行包裹.
2,实例代码:
#include <iostream>
#include <list>
#include <cassert>
using namespace std;

class Beverage //装饰者和被装饰者共同的基类
{
protected:
    Beverage() : _description( "Unknown Beverage" )
    {}
public:
    virtual ~Beverage() = 0;
	virtual std::string getDescription() const
	{
		return _description;
	}
	virtual double cost() const = 0;
protected:
    string _description;
};
Beverage::~Beverage() {}
/*****************************装饰者基类*************************************/
class CondimentDecorator : public Beverage
{
protected:
    CondimentDecorator() {}
private:
    CondimentDecorator( const CondimentDecorator& ); // Disable copy constructor
	void operator=( const CondimentDecorator& ); // Disable assignment operator
public:
    virtual ~CondimentDecorator() = 0;
    virtual string getDescription() const = 0;
};
CondimentDecorator::~CondimentDecorator() {}

/*****************************装饰者*************************************/
class Milk : public CondimentDecorator
{
public:
    explicit Milk( const Beverage* beverage ) :
        _beverage( beverage )
    {
        assert( beverage );
	}
	~Milk()
	{
		delete _beverage;
	}
 	string getDescription() const
 	{
		return _beverage->getDescription() + ", Milk";
	}
 	double cost() const
 	{
		return 0.10 + _beverage->cost(); //这里是核心
	}
private:
    const Beverage* _beverage;
};

class Whip : public CondimentDecorator
{
public:
    explicit Whip( const Beverage* beverage ) :
		_beverage( beverage )
    {
        assert( beverage );
	}
	~Whip()
	{
		delete _beverage;
	}
	string getDescription() const
	{
		return _beverage->getDescription() + ", Whip";
	}
	double cost() const
	{
		return 0.10 + _beverage->cost();
	}
private:
    const Beverage* _beverage;
};

class Mocha : public CondimentDecorator
{
public:
    explicit Mocha( Beverage* beverage ) :
		_beverage( beverage )
    {
        assert( beverage );
	}
	~Mocha()
	{
		delete _beverage;
	}
	string getDescription() const
	{
		return _beverage->getDescription() + ", Mocha";
	}
	double cost() const
	{
		return 0.20 + _beverage->cost();
	}
private:
    const Beverage* _beverage;
};

class Soy : public CondimentDecorator
{
public:
    explicit Soy( const Beverage* beverage ) :
		_beverage( beverage )
    {
        assert( beverage );
	}
	~Soy()
	{
		delete _beverage;
	}
	string getDescription() const
	{
		return _beverage->getDescription() + ", Soy";
	}
	double cost() const
	{
		return 0.15 + _beverage->cost();
	}
private:
    const Beverage* _beverage;
};

/*****************************被装饰者*************************************/
class DarkRoast : public Beverage
{
public:
    DarkRoast()
	{
		_description = "Dark Roast Coffee";
	}
	double cost() const
	{
		return 0.99;
	}
};

class Decaf : public Beverage
{
public:
    Decaf()
    {
		_description = "Decaf Coffee";
	}
  	double cost() const
  	{
		return 1.05;
	}
};

class Espresso : public Beverage
{
public:
    Espresso()
    {
		_description = "Espresso";
	}
	double cost() const
	{
		return 1.99;
	}
};

class HouseBlend : public Beverage
{
public:
    HouseBlend()
    {
		_description = "House Blend Coffee";
	}
	double cost() const
	{
		return 0.89;
	}
};

int main()
{
    Beverage* beverage = new Espresso();
	cout.setf( ios::showpoint);
	cout.precision(3);
	cout << beverage->getDescription()
              << " $"
			  << beverage->cost()
			  << endl;

	Beverage* beverage2 = new DarkRoast();
	beverage2 = new Mocha(beverage2); //加咖啡
	beverage2 = new Mocha(beverage2); //加双份咖啡
	beverage2 = new Whip(beverage2);  //加麦
	cout << beverage2->getDescription()
			  << " $"
			  << beverage2->cost()
			  << endl;

	Beverage* beverage3 = new HouseBlend();
	beverage3 = new Soy(beverage3);
	beverage3 = new Mocha(beverage3);
	beverage3 = new Whip(beverage3);
	cout << beverage3->getDescription()
			  << " $"
			  << beverage3->cost()
			  << endl;

	delete beverage3;
	delete beverage2;
	delete beverage;
    return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics