#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();
}
装饰模式demo
最新推荐文章于 2025-12-18 16:28:22 发布
266

被折叠的 条评论
为什么被折叠?



