Simple Factory is the creation mode of a class, also known as the Static Factory method (Factory) mode.
A simple factory model is a factory class that determines which product class to create based on the parameters passed in.
There are 4 characters
Factory class role: A direct caller of the specific product class role.
Abstract product role: an interface or abstract class that is responsible for defining specific product roles and interacting with clients.
Specific product roles: objects created by the factory class, and also the client's actual action object.
Client: Call the factory class to produce the instance and invoke the method of the instance to work accordingly.
C + + implementation
#include <iostream>
#include <list>
using namespace Std;
Class Pizza
{
Public
String name;
string dough;
string sauce;
List<string> pizzalist;
void Prepare ()
{
cout<< "Preparing" +name<<endl;
cout<< "tossing dough ..." <<endl;
cout<< "Adding sauce ..." <<endl;
cout<< "Adding pizzalist:" <<endl;
For (List<string>::iterator It=pizzalist.begin (); It!=pizzalist.end (); it++)
{
cout<< "" <<*it<<endl;
}
}
virtual void bake ()
{
cout<< "Bake for minutes at" <<endl;
}
virtual void cut ()
{
cout<< "cutting the pizza into diagonal slices" <<endl;
}
virtual void box ()
{
cout<< "Place Pizza in official Pizzastore box" <<endl;
}
String GetName ()
{
return name;
}
};
Class Nystylecheesepizza:public Pizza
{
Public
Nystylecheesepizza ()
{
Name= "NY Style Sauce and Cheese Pizza";
dough= "Thin crust dough";
sauce= "marinara sauce";
Pizzalist.push_back ("grated Reggiano Cheese");
}
};
Class Chicagostylecheesepizza:public Pizza
{
Public
Chicagostylecheesepizza ()
{
Name= "Chicago Style deep Dish Cheese Pizza";
Dough= "Extra Thick crust Dough";
sauce= "Plum Tomato sauce";
Pizzalist.push_back ("shredded mozzarella Cheese");
}
void Cut ()
{
cout<< "cutting the pizza into square slices";
}
};
Class Pizzastore
{
Public:
Pizza Orderpizza (String type)
{
Pizza Pizza;
Pizza=creatpizza (type);
Pizza.prepare ();
Pizza.bake ();
Pizza.cut ();
Pizza.box ();
return pizza;
}
Protected
Virtual Pizza Creatpizza (String type) = 0;
};
Class Nystylepizzastore:public Pizzastore
{
Pizza Creatpizza (String item)
{
if (item== "cheese")
{
Nystylecheesepizza NY;
return NY;
}
}
};
Class Chicagostylestore:public Pizzastore
{
Pizza Creatpizza (String item)
{
if (item== "cheese")
{
Chicagostylecheesepizza Chicago;
return Chicago;
}
}
};
int main ()
{
Pizzastore *nystore=new Nystylepizzastore ();
Pizzastore *chicagostore=new Chicagostylestore ();
Pizza Pizza =nystore->orderpizza ("cheese");
cout<< "Ethan ordered a" +pizza.getname () <<endl;
Pizza =chicagostore->orderpizza ("cheese");
cout<< "Joel ordered a" +pizza.getname () <<endl;
return 0;
}