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

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

模式定义

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

模式类图

典型案例

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();
}
 

 ====================================================================

博主有自己的个人主页啦!请求关注,请求支持。QAQ.

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperYang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值