装饰模式demo

#include <QCoreApplication>
#include <iostream>
using namespace std;

class HuoGuo
{
public:
    virtual  float totalPrice() = 0;
    virtual string totalCai() = 0;
};

class TaoCanHuoGuo : public HuoGuo
{
public:
    float  totalPrice()
    {
        return 200;
    }

    string totalCai()
    {
        return "套餐";
    }
};

class HuoGuoCaiLei : public HuoGuo
{
public :
    HuoGuo *m_huoGuo;
public:
    HuoGuoCaiLei(HuoGuo *hg)
    {
        this->m_huoGuo = hg;
    }

    float totalPrice()
    {
        return m_huoGuo->totalPrice();
    }

    string totalCai()
    {
        return m_huoGuo->totalCai();
    }
};

class HaiDai : public HuoGuoCaiLei
{
public:
    HaiDai(HuoGuo *hg):HuoGuoCaiLei(hg) {}

    float totalPrice()
    {
        return HuoGuoCaiLei::totalPrice() + 30;
    }
    string totalCai()
    {
        return HuoGuoCaiLei::totalCai() + "加了海带配菜";
    }
};

class DaXia : public HuoGuoCaiLei
{
public:
    DaXia(HuoGuo *hg) : HuoGuoCaiLei(hg){}

    float totalPrice()
    {
        return HuoGuoCaiLei::totalPrice() + 60;
    }
    string totalCai()
    {
        return HuoGuoCaiLei::totalCai() + "加了大虾配菜";
    }
};

int main(int argc, char *argv[])
{
    HuoGuo *tchg = new TaoCanHuoGuo();
    cout << tchg->totalCai() << " 总消费: " << tchg->totalPrice() << endl;

    tchg = new HaiDai(tchg);
    cout << tchg->totalCai() << " 总消费: " << tchg->totalPrice() << endl;

    tchg = new DaXia(tchg);
    cout << tchg->totalCai() << " 总消费: " << tchg->totalPrice() << endl;

    tchg = new DaXia(tchg);
    cout << tchg->totalCai() << " 总消费: " << tchg->totalPrice() << endl;
    QCoreApplication a(argc, argv);


    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值