定义:
从设计模式的类型上来说,简单工厂模式是属于创建型模式,又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。
场景:
我们要开一家比萨商店,定义了一个简单的披萨工厂,可以根据客户的需要制作芝士披萨、辣肠比萨、蛤蜊披萨、素食披萨。根据传入的披萨类型从披萨工厂生产出客户所需要的披萨。
类图:
c++代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class PizzaType
{
public:
enum EPizzaType{cheese,pepperoni,clam,veggie};
};
class Pizza
{
public:
virtual ~Pizza() {};
string getName();
void prepare();
void bake();
void cut();
void box();
protected:
string name;
string dough;
string sauce;
vector<string> toppings;
};
class CheesePizza : public Pizza
{
public:
CheesePizza();
};
class PepperoniPizza : public Pizza
{
public:
PepperoniPizza();
};
class ClamPizza : public Pizza
{
public:
ClamPizza();
};
class VeggiePizza : public Pizza
{
public:
VeggiePizza();
};
class SimplePizzaFactory
{
public:
Pizza* createPizza(PizzaType::EPizzaType type);
};
class PizzaStore
{
private:
SimplePizzaFactory factory;
public:
Pizza* orderPizza(PizzaType::EPizzaType type);
};
string Pizza::getName()
{
return name;
}
void Pizza::prepare()
{
printf("Preparing %s\n",name.c_str());
}
void Pizza::bake()
{
printf("Baking %s\n",name.c_str());
}
void Pizza::cut()
{
printf("Cutting %s\n",name.c_str());
}
void Pizza::box()
{
printf("Boxing %s\n",name.c_str());
}
CheesePizza::CheesePizza()
{
name = "Cheese Pizza";
dough = "Regular Crust";
sauce = "Marinara Pizza Sauce";
toppings.push_back("Fresh Mozzarella");
toppings.push_back("Parmesan");
}
PepperoniPizza::PepperoniPizza()
{
name = "Pepperoni Pizza";
dough = "Crust";
sauce = "Marinara sauce";
toppings.push_back("Sliced Pepperoni");
toppings.push_back("Sliced Onion");
toppings.push_back("Grated parmesan cheese");
}
ClamPizza::ClamPizza()
{
name = "Clam Pizza";
dough = "Thin crust";
sauce = "White garlic sauce";
toppings.push_back("Clams");
toppings.push_back("Grated parmesan cheese");
}
VeggiePizza::VeggiePizza()
{
name = "Veggie Pizza";
dough = "Crust";
sauce = "Marinara sauce";
toppings.push_back("Shredded mozzarella");
toppings.push_back("Grated parmesan");
toppings.push_back("Diced onion");
toppings.push_back("Sliced mushrooms");
toppings.push_back("Sliced red pepper");
toppings.push_back("Sliced black olives");
}
Pizza* SimplePizzaFactory::createPizza(PizzaType::EPizzaType type)
{
Pizza* pizza = NULL;
if (PizzaType::cheese == type)
{
pizza = new CheesePizza();
}
else if (PizzaType::pepperoni == type)
{
pizza = new PepperoniPizza();
}
else if (PizzaType::clam == type)
{
pizza = new ClamPizza();
}
else if (PizzaType::veggie == type)
{
pizza = new VeggiePizza();
}
return pizza;
}
Pizza* PizzaStore::orderPizza(PizzaType::EPizzaType type)
{
Pizza* pizza = NULL;
pizza = factory.createPizza(type);
pizza->prepare();
pizza->bake();
pizza->cut();
pizza->box();
return pizza;
}
int main()
{
PizzaStore pizzaStore;
Pizza* pCheesePizza = pizzaStore.orderPizza(PizzaType::cheese);
Pizza* pPepperoniPizza = pizzaStore.orderPizza(PizzaType::pepperoni);
Pizza* pClamPizza = pizzaStore.orderPizza(PizzaType::clam);
Pizza* pVeggiePizza = pizzaStore.orderPizza(PizzaType::veggie);
delete pCheesePizza;
delete pPepperoniPizza;
delete pClamPizza;
delete pVeggiePizza;
return 0;
}
运行后结果如下:
Preparing Cheese Pizza
Baking Cheese Pizza
Cutting Cheese Pizza
Boxing Cheese Pizza
Preparing Pepperoni Pizza
Baking Pepperoni Pizza
Cutting Pepperoni Pizza
Boxing Pepperoni Pizza
Preparing Clam Pizza
Baking Clam Pizza
Cutting Clam Pizza
Boxing Clam Pizza
Preparing Veggie Pizza
Baking Veggie Pizza
Cutting Veggie Pizza
Boxing Veggie Pizza
参考图书:《Head First 设计模式》