Ai
1 Star 0 Fork 0

rabbitqyh/DesignPattern

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
DecoratorPattern.cpp 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
yaocoder 提交于 2013-05-31 23:22 +08:00 . decorator
/*
装饰着模式:动态的将责任加到对象上。想要扩展对象,装饰者提供有利于继承的另一种选择。
*/
#include <iostream>
#include <string>
using namespace std;
class Beverage
{
public:
virtual double Cost() = 0;
virtual string GetDescription()
{
return description_;
}
string description_;
};
class CondimentDecorator:public Beverage
{
public:
virtual string GetDescription() = 0;
};
class Espresso:public Beverage
{
public:
Espresso() {description_ = "Espresso";}
double Cost() { return 1.99;}
};
class DarkRoast:public Beverage
{
public:
DarkRoast() {description_ = "DarkRoast coffee";}
double Cost() { return 1.00;}
};
class HouseBlend:public Beverage
{
public:
HouseBlend() {description_ = "HouseBlend coffee";}
double Cost() { return 0.89;}
};
class Mocha :public CondimentDecorator
{
public:
Mocha(Beverage* pBeverage)
{
this->pBeverage_ = pBeverage;
}
string GetDescription()
{
return pBeverage_->GetDescription() + ", Mocha ";
}
double Cost()
{
return 0.20 + pBeverage_->Cost();
}
private:
Beverage* pBeverage_;
};
class Whip :public CondimentDecorator
{
public:
Whip(Beverage* pBeverage)
{
this->pBeverage_ = pBeverage;
}
string GetDescription()
{
return pBeverage_->GetDescription() + ", Whip ";
}
double Cost()
{
return 0.10 + pBeverage_->Cost();
}
private:
Beverage* pBeverage_;
};
class Soy :public CondimentDecorator
{
public:
Soy(Beverage* pBeverage)
{
this->pBeverage_ = pBeverage;
}
string GetDescription()
{
return pBeverage_->GetDescription() + ", Soy ";
}
double Cost()
{
return 0.30 + pBeverage_->Cost();
}
private:
Beverage* pBeverage_;
};
int main()
{
Beverage* pBeverage = new Espresso;
cout << pBeverage->GetDescription() << " $" << pBeverage->Cost() << endl;
Beverage* pBeverage2 = new DarkRoast;
pBeverage2 = new Mocha(pBeverage2);
pBeverage2 = new Whip(pBeverage2);
pBeverage2 = new Whip(pBeverage2);
cout << pBeverage2->GetDescription() << " $" << pBeverage2->Cost() << endl;
Beverage* pBeverage3 = new HouseBlend;
pBeverage3 = new Mocha(pBeverage3);
pBeverage3 = new Soy(pBeverage3);
pBeverage3 = new Whip(pBeverage3);
cout << pBeverage3->GetDescription() << " $" << pBeverage3->Cost() << endl;
system("pause");
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/rabbitqyh/DesignPattern.git
git@gitee.com:rabbitqyh/DesignPattern.git
rabbitqyh
DesignPattern
DesignPattern
master

搜索帮助