Definition:
In terms of the design pattern type, the simple factory pattern belongs to the creation pattern, also known as the staticfactory method pattern, but not one of 23 gof design patterns. The simple factory mode is determined by a factory object to create a product instance. The simple factory model is the simplest and Practical Model in the factory model family. It can be understood as a special implementation of different factory models.
Scenario:
We want to open a pizza store and define a simple Pizza Factory that can make cheese, sausage, clam, and vegetarian based on customer needs. Produce the customer's desired pizza from the Pizza Factory Based on the imported pizza type.
Class diagram:
The C ++ code is as follows:
#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;
}
The result is as follows:
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
Reference books: head first design model