- SimplePizzaFactory.CreatePizza accepts the type string and returns the corresponding Pizza.
- PizzaStore receives the factory through its constructor and delegates creation to it.
- The client’s first run orders a Cheese Pizza.
7.3 Factory Pattern Lab
This assignment is based on the Pizza Store in Head First Design Patterns (p. 112-157)
Work through the stories in order. Use the acceptance criteria to check each feature, and complete the nested tasks using the specified names, values, and scenarios.
| Store | Ingredient factory | Expected dough |
|---|---|---|
| NYPizzaStore | NYIngredientFactory | ThinCrustDough |
| ChicagoPizzaStore | ChicagoIngredientFactory | ThickCrustDough |
In the Factories folder, define a new class named SimplePizzaFactory with a method called CreatePizza that takes a string parameter named type and returns a Pizza object.
Cut the if/else block from the OrderPizza method in the PizzaStore class and paste it in the SimplePizzaFactory class's CreatePizza method.
Give the PizzaStore class a constructor with a SimplePizzaFactory type argument named factory and a field of type SimplePizzaFactory named factory.
Copy the CreatePizza method in the SimplePizzaFactory class and paste it into both the ChicagoPizzaStore and NYPizzaStore classes. Make the methods overrides.
In the ChicagoPizzaStore class's CreatePizza method, instantiate the Chicago versions of the objects and change the Name properties to read "Chicago Cheese Pizza".
PizzaStore pizzaStore = new ChicagoPizzaStore();Console.WriteLine("Preparing " + this.Name);
Console.WriteLine("Tossing " + this.Dough);
Console.WriteLine("Adding " + this.Sauce);
Console.WriteLine("Adding toppings: ");
for (int i = 0; i < toppings.Count; i++)
{
Console.WriteLine(" " + toppings[i]);
}Console.WriteLine("Bake for 25 minutes at 350.");Console.WriteLine("Place pizza in official PizzaStore box.");Console.WriteLine("Cutting the pizza into diagonal slices.");this.Name = "New York Style Cheese Pizza";
this.Dough = "Thin Crust Dough";
this.Sauce = "Marinara Sauce";
this.toppings.Add("Grated Reggiano Cheese");this.Name = "Chicago Style Deep Dish Cheese Pizza";
this.Dough = "Extra Thick Crust Dough";
this.Sauce = "Plum Tomato Sauce";
this.toppings.Add("Shredded Mozzarella Cheese");Console.WriteLine("Cutting the pizza into square slices.");Console.WriteLine($"Ethan ordered a {pizza.Name}. \n");
Line 11: : Preparing New York Style Cheese Pizza
Line 2York Style Cheese Pizza
Line 2: : Tossing Thin Crust Dough
Line 3Crust Dough
Line 3: : Adding Marinara Sauce
Line 4Sauce
Line 4: : Adding toppings:
:
Line 55: : Grated Reggiano Cheese
Line 6Cheese
Line 6: : Bake for 25 minutes at 350.
Line 7for 25 minutes at 350.
Line 7: : Cutting the pizza into diagonal slices.
Line 8pizza into diagonal slices.
Line 8: : Place pizza in official PizzaStore box.
Line 9in official PizzaStore box.
Line 9: : Ethan ordered a New York Style Cheese Pizza.
Line 10a New York Style Cheese Pizza.
Line 10:
:
Line 1111: : Preparing Chicago Style Cheese Pizza
Line 12Style Cheese Pizza
Line 12: : Tossing Extra Thick Crust Dough
Line 13Thick Crust Dough
Line 13: : Adding Plum Tomato Sauce
Line 14Tomato Sauce
Line 14: : Adding toppings:
:
Line 1515: : Shredded Mozzarella Cheese
Line 16Cheese
Line 16: : Bake for 25 minutes at 350.
Line 17for 25 minutes at 350.
Line 17: : Cutting the pizza into square slices.
Line 18pizza into square slices.
Line 18: : Place pizza in official PizzaStore box.
Line 19in official PizzaStore box.
Line 19: : Joel ordered a Chicago Style Cheese Pizza.a Chicago Style Cheese Pizza.Dough CreateDough();
Sauce CreateSauce();
Cheese CreateCheese();
Veggie[] CreateVeggies();
Pepperoni CreatePepperoni();
Clam CreateClams();In the Factories folder, define a new ChicagoIngredientFactory class and a new NYIngredientFactory class. Have them both implement the IPizzaIngredientFactory interface.
In the NYIngredientFactory, return the proper ingredient object in each method and return a list of Garlic, Onion, Mushroom and RedPepper for the Veggies.
In the ChicagoIngredientFactory, return the proper ingredient object in each method and return a list of Garlic and RedPepper for the Veggies.
Define properties for Dough, Sauce, Veggies, Cheese, Pepperoni and Clams and remove the string properties for Dough, Sauce and toppings.
Define a constructor with a parameter of type IPizzaIngredientFactory named ingredientFactory and inside the constructor assign the field.
Console.WriteLine("Preparing: " + this.Name);
this.Dough = this.ingredientFactory.CreateDough();
this.Sauce = this.ingredientFactory.CreateSauce();
this.Cheese = this.ingredientFactory.CreateCheese();Repeat the previous step for the ClamPizza, PepperoniPizza and VeggiePizza classes and call the proper methods. (i.e. the clam pizza should be creating clams for the pizza).
This is where the beauty of the Factory Pattern comes in. We do not need more "concrete" classes to make the object we want.
At the beginning of CreatePizza, instantiate ChicagoIngredientFactory in ChicagoPizzaStore and NYIngredientFactory in NYPizzaStore. Name the local variable ingredientFactory.

Type Value
pizza {FactoryPattern.CheesePizzapizza {FactoryPattern.CheesePizza}
}
Cheese {FactoryPattern. ReggianoCheese{FactoryPattern. ReggianoCheese}
}
Clams null
Dough {FactoryPattern.ThinCrustDoughnull
Dough {FactoryPattern.ThinCrustDough}
}
Name "New York Style Cheese Pizza"
Pepperoni null
Sauce {FactoryPattern.MarinaraSauce"New York Style Cheese Pizza"
Pepperoni null
Sauce {FactoryPattern.MarinaraSauce}
}
Veggies null
ingredientFactory {FactoryPattern.NYIngredientFactorynull
ingredientFactory {FactoryPattern.NYIngredientFactory}}
Type Value
pizza {FactoryPattern.ClamPizzapizza {FactoryPattern.ClamPizza}
}
Cheese {FactoryPattern.MozzarellaCheese{FactoryPattern.MozzarellaCheese}
}
Clams {FactoryPattern.FrozenClams{FactoryPattern.FrozenClams}
}
Dough {FactoryPattern.ThickCrustDough{FactoryPattern.ThickCrustDough}
}
Name "Chicago Style Clam Pizza"
Pepperoni null
Sauce {FactoryPattern.PlumTomatoSauce"Chicago Style Clam Pizza"
Pepperoni null
Sauce {FactoryPattern.PlumTomatoSauce}
}
Veggies null
ingredientFactory {FactoryPattern.ChicagoIngredientFactorynull
ingredientFactory {FactoryPattern.ChicagoIngredientFactory}}Return to each story and verify its acceptance criteria. Preserve the submission format and destination stated in the activity; a reading, setup guide, lab, video demonstration, and oral final may require different evidence.
Build a simple factory. Add franchises Make some pizzas Implement an ingredient factory.