Ai
1 Star 0 Fork 0

rabbitqyh/DesignPattern

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SimpleFactoryPattern.cpp 965 Bytes
一键复制 编辑 原始数据 按行查看 历史
yaocoder 提交于 2013-06-05 21:18 +08:00 . Update SimpleFactoryPattern.cpp
/*
简单工厂模式
*/
#include <iostream>
#include <string>
using namespace std;
class Pizza
{
public:
virtual void Prapare() = 0;
virtual void Bake() = 0;
};
class CheesePizza:public Pizza
{
public:
void Prapare()
{
cout << "CheesePizza Prapare.\n";
}
void Bake()
{
cout << "CheesePizza Bake.\n";
}
};
class VeggiePizza:public Pizza
{
public:
void Prapare()
{
cout << "VeggiePizza Prapare.\n";
}
void Bake()
{
cout << "VeggiePizza Bake.\n";
}
};
class SimplePizzaFactory
{
public:
static Pizza* CreatePizza(string type)
{
if (0 == type.compare("cheese"))
{
pizza_ = new CheesePizza;
}
else if (0 == type.compare("veggie"))
{
pizza_ = new VeggiePizza;
}
return pizza_;
}
private:
static Pizza* pizza_;
};
Pizza* SimplePizzaFactory::pizza_ = NULL;
int main()
{
SimplePizzaFactory::CreatePizza("cheese")->Prapare();
SimplePizzaFactory::CreatePizza("veggie")->Prapare();
system("pause");
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/rabbitqyh/DesignPattern.git
git@gitee.com:rabbitqyh/DesignPattern.git
rabbitqyh
DesignPattern
DesignPattern
master

搜索帮助