文章中部分内容和思路来自《Head First设计模式》


模式定义

动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案


模式类图

c++/qt设计模式-装饰者模式


典型案例

1.案例说明

去咖啡店喝咖啡,我们可以要求店员在咖啡中加各种调味品,如牛奶、摩卡等。我们可以将这些调味品称为装饰


2.代码实现

------------

IComponent.h

------------

#ifndef ICOMPONENT_H
#define ICOMPONENT_H

class IComponent
{
public:
    virtual float cost() = 0;
};

#endif // ICOMPONENT_H

 

------------

Decoration.h

------------

#ifndef DECORATION_H
#define DECORATION_H

#include "IComponent.h"

class Decoration : public IComponent
{
public:
    Decoration();
    virtual ~Decoration();
};

#endif // DECORATION_H

 

--------------

Decoration.cpp

--------------

#include "Decoration.h"

Decoration::Decoration()
{

}

Decoration::~Decoration()
{

}

 

------------

HouseBlend.h

------------

#ifndef HOUSEBLEND_H
#define HOUSEBLEND_H

#include "IComponent.h"

class HouseBlend : public IComponent
{
public:
    HouseBlend();
    virtual ~HouseBlend();

public:
    virtual float cost();
};

#endif // HOUSEBLEND_H

 

--------------

HouseBlend.cpp

--------------

#include "HouseBlend.h"

HouseBlend::HouseBlend()
{

}

HouseBlend::~HouseBlend()
{

}

float HouseBlend::cost()
{
    return 4.5;
}

 

----------------

MilkDecoration.h

----------------

#ifndef MILKDECORATION_H
#define MILKDECORATION_H

#include "Decoration.h"

class MilkDecoration : public Decoration
{
public:
    MilkDecoration(IComponent *cp);
    virtual ~MilkDecoration();

public:
    virtual float cost();

private:
    IComponent *_cp;
};

#endif // MILKDECORATION_H

 

------------------

MilkDecoration.cpp

------------------

#include "MilkDecoration.h"

MilkDecoration::MilkDecoration(IComponent *cp)
{
    _cp = cp;
}

MilkDecoration::~MilkDecoration()
{

}

float MilkDecoration::cost()
{
    return _cp->cost() + 0.3;
}

 

------

Moka.h

------

#ifndef MOKA_H
#define MOKA_H

#include "Decoration.h"

class Moka : public Decoration
{
public:
    Moka(IComponent *cp);
    virtual ~Moka();

public:
    virtual float cost();

private:
    IComponent *_cp;
};

#endif // MOKA_H

 

--------

Moka.cpp

--------

#include "Moka.h"

Moka::Moka(IComponent *cp)
{
    _cp = cp;
}

Moka::~Moka()
{

}

float Moka::cost()
{
    return _cp->cost() + 1.6;
}

 

--------

main.cpp

--------

/**
 * 设计模式-装饰者模式
 * 要点:1,组件与装饰有共同超类 2,装饰可以装饰到任何组件
 */
#include <QCoreApplication>

#include <QDebug>

#include "MilkDecoration.h"
#include "HouseBlend.h"
#include "Moka.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    
    HouseBlend *blend = new HouseBlend;

    MilkDecoration *deco = new MilkDecoration(blend); // houseblend加牛奶    
    qDebug() << deco->cost();

    
    Moka *mokaDeco = new Moka(blend); // houseblend加摩卡    
    qDebug() << mokaDeco->cost();

    
    Moka *mokaDeco2 = new Moka(deco); // houseblend加牛奶加摩卡  
    qDebug() << mokaDeco2->cost();

    
    delete mokaDeco2;
    mokaDeco2 = NULL;

    
    delete mokaDeco;
    mokaDeco = NULL;

    
    delete deco;
    deco = NULL;

    
    delete blend;
    blend = NULL;

    
    return a.exec();
}


相关文章: