Test04.cpp:Defines the entry point for the console application.
//
Design Pattern 4th Factory model
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
using namespace Std;
Class Pizza
{
Public
String name;
string dough;
string sauce;
Vector<string> toppings;
void Prepare ()
{
cout<< "Preparing" <<name<<endl;
cout<< "tossing dough ..." <<endl;
cout<< "Adding sauce ..." <<endl;
cout<< "Adding toppings:" <<endl;
for (int i = 0;i < Toppings.size (); i++)
{
cout<<toppings[i]<<endl;
}
}
void Bake ()
{
cout<< "Bake for minutes at" <<endl;
}
virtual void cut ()
{
cout<< "cutting ths pizza into diagonal slices" <<endl;
}
void Box ()
{
cout<< "Pizza in official Pizzastone box" <<endl;
}
String GetName ()
{
return name;
}
};
Class Pizzastore
{
Public
pizza* Orderpizza (String type)
{
pizza* Pizza;
Pizza = Createpizza (type);
Pizza->prepare ();
Pizza->bake ();
Pizza->cut ();
Pizza->box ();
return pizza;
}
Protected
Virtual pizza* Createpizza (String type) {return null;};/ /The core of the factory model
};
Class Nystylecheesepizza:public Pizza
{
Public
Nystylecheesepizza () {
Name = "NY Style Sauce and Cheese Pizza";
Dough = "Thin crust dough";
sauce = "marinara sauce";
Toppings.push_back ("grated Reggiano Cheese");//covered with Italian Reggiano premium cheese
}
};
Class Chicagostylecheesepizza:public Pizza
{
Public
Chicagostylecheesepizza ()
{
Name = "Chicago Style deep Dish Cheese Pizza";
Dough = "Extra Thick crust Dough";
sauce = "Plum Tomato sauce";
Toppings.push_back ("shredded mozzarella Cheese");
}
void Cut ()
{
cout<< "cutting the pizza into square slices" <<endl;
}
};
Class Nypizzastore:public Pizzastore
{
pizza* Createpizza (String item)
{
if (item = = "Cheese")
{
return new Nystylecheesepizza ();
}
/*else if (item = = "Veggie")
{
return new Nystyleveggiepizza ();
}
else if (item = = "Clam")
{
return new Nystyleclampizza ();
}
else if (item = = "Pepperoni")
{
return new Nystylepepperonipizza ();
}*/
Else
{
return NULL;
}
}
};
Class Chicagopizzastore:public Pizzastore
{
pizza* Createpizza (String item)
{
if (item = = "Cheese")
{
return new Chicagostylecheesepizza ();
}
Else
{
return NULL;
}
}
};
There is also an abstract factory pattern
int _tmain (int argc, _tchar* argv[])
{
pizzastore* Nystore = new Nypizzastore ();
pizzastore* Chicagostore = new Chicagopizzastore ();
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;
}
Introduction to design patterns, Factory mode, C + + code implementation