7.3 Factory Pattern Lab

Overview

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.

Checkpoint reference
Store Ingredient factory Expected dough
NYPizzaStore NYIngredientFactory ThinCrustDough
ChicagoPizzaStore ChicagoIngredientFactory ThickCrustDough
Download and extract the start code.
1.Download and extract the start code.

7.3 Factory LastName.zip

7.3 Factory LastName.zip
A Download and extract the start code.
Build a simple factory.
Acceptance Criteria
Acceptance criteria
  • 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.
Instructions
2.Build a simple factory.
A In the Factories folder, define a new class named SimplePizzaFactory with a method called CreatePizza that takes a string…

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.

B Cut the if/else block from the OrderPizza method in the PizzaStore class and paste it in the SimplePizzaFactory class's…

Cut the if/else block from the OrderPizza method in the PizzaStore class and paste it in the SimplePizzaFactory class's CreatePizza method.

C Give the PizzaStore class a constructor with a SimplePizzaFactory type argument named factory and a field of type…

Give the PizzaStore class a constructor with a SimplePizzaFactory type argument named factory and a field of type SimplePizzaFactory named factory.

D In the PizzaStore constructor, assign the factory field to the factory parameter.
E In the Main method, instantiate a SimplePizzaFactory and pass it in to the PizzaStore constructor.
F In the OrderPizza method, call the factory object's CreatePizza method and pass in the type parameter as an argument.
G Check Your Work: Run the console. The output should show that you've ordered a Cheese Pizza.
Add franchises
Acceptance Criteria
Acceptance criteria
  • Run the application. The output should show that a Chicago Style Cheese Pizza was ordered.
  • Run the application. The output should show that a New York Style Cheese Pizza was ordered.
  • The completed implementation satisfies this requirement: Make ChicagoPizzaStore and NYPizzaStore inherit from PizzaStore.
Instructions
3.Add franchises
A Make ChicagoPizzaStore and NYPizzaStore inherit from PizzaStore.
B Remove the PizzaStore constructor, factory field and instantiation of the PizzaStore from the Main method.
C Define an abstract method in the PizzaStore class named CreatePizza that takes a string type named type.
D Make the PizzaStore class abstract.
E Instead of calling the factory's CreatePizza method, call the CreatePizza method from the class.
F Copy the CreatePizza method in the SimplePizzaFactory class and paste it into both the ChicagoPizzaStore and NYPizzaStore…

Copy the CreatePizza method in the SimplePizzaFactory class and paste it into both the ChicagoPizzaStore and NYPizzaStore classes. Make the methods overrides.

G Have the specific "Style" pizzas inherit from their base style pizzas. (Example: ChicagoStyleCheesePizza inherits CheesePizza)
H In the ChicagoPizzaStore class's CreatePizza method, instantiate the Chicago versions of the objects and change the Name…

In the ChicagoPizzaStore class's CreatePizza method, instantiate the Chicago versions of the objects and change the Name properties to read "Chicago Cheese Pizza".

I Repeat the previous step for the NYPizzaStore.
J Delete the SimplePizzaFactory class.
K In the Main method, instantiate a new ChicagoPizzaStore.
PizzaStore pizzaStore = new ChicagoPizzaStore();
L Check Your Work:
1 Run the application. The output should show that a Chicago Style Cheese Pizza was ordered.
2 Stop the application.
3 Change the ChicagoPizzaStore to a NYPizzaStore.
4 Run the application. The output should show that a New York Style Cheese Pizza was ordered.
Make some pizzas
Acceptance Criteria
Acceptance criteria
  • The completed implementation satisfies this requirement: Give the Pizza class properties for Dough and Sauce.
  • The completed implementation satisfies this requirement: Give the Pizza class a protected field of type ArrayList named toppings and instantiate it at the field level.
  • The completed implementation satisfies this requirement: Add the code below to the Prepare method.
Instructions
4.Make some pizzas
A Give the Pizza class properties for Dough and Sauce.
B Give the Pizza class a protected field of type ArrayList named toppings and instantiate it at the field level.
C Add the code below to the Prepare method.
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]);
}
D Add the code below to the Bake method:
Console.WriteLine("Bake for 25 minutes at 350.");
E Add the code below to the Box method:
Console.WriteLine("Place pizza in official PizzaStore box.");
F Add the code below to the Cut method:
Console.WriteLine("Cutting the pizza into diagonal slices.");
G Define a constructor in the NYStyleCheesePizza class and paste the code below inside of it.
this.Name = "New York Style Cheese Pizza";
this.Dough = "Thin Crust Dough";
this.Sauce = "Marinara Sauce";
this.toppings.Add("Grated Reggiano Cheese");
H Define a constructor in the ChicagoStyleCheesePizza class and paste the code below inside of it.
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");
I In the ChicagoStyleCheesePizza class, override the Cut method and paste the code below inside of it.
Console.WriteLine("Cutting the pizza into square slices.");
J In the Main method...
1 Modify the line that prints the order to the Console:
Console.WriteLine($"Ethan ordered a {pizza.Name}. \n");
2 After ordering the first cheese pizza, instantiate a ChicagoPizzaStore and order an additional cheese pizza.
3 Console out what kind of pizza Joel ordered.
K Check Your Work: Run the application. Your output should look like the output below.
Console Output for 7.3 Factory Pattern Lab. Use the adjacent task instructions to identify the required controls, output, or debugger values.
Console Output
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.
Implement an ingredient factory.
Acceptance Criteria
Acceptance criteria
  • Press F11 into the CreatePizza method that should be in the NYPizzaStore.
  • When the debugger returns to PizzaStore, the pizza object should have the following Values/Types.
  • F10 over the next call to OrderPizza in Main. The pizza object should have the following Values/Types.
Instructions
5.Implement an ingredient factory.
A In the Factories folder, define a new interface named IPizzaIngredientFactory
B Copy the code below into the interface.
Dough CreateDough();
Sauce CreateSauce();
Cheese CreateCheese();
Veggie[] CreateVeggies();
Pepperoni CreatePepperoni();
Clam CreateClams();
C In the Factories folder, define a new ChicagoIngredientFactory class and a new NYIngredientFactory class. Have them both…

In the Factories folder, define a new ChicagoIngredientFactory class and a new NYIngredientFactory class. Have them both implement the IPizzaIngredientFactory interface.

D In the Chicago and New York Ingedients folders, have all classes inherit from their proper parent class.
E In the NYIngredientFactory, return the proper ingredient object in each method and return a list of Garlic, Onion, Mushroom…

In the NYIngredientFactory, return the proper ingredient object in each method and return a list of Garlic, Onion, Mushroom and RedPepper for the Veggies.

F In the ChicagoIngredientFactory, return the proper ingredient object in each method and return a list of Garlic and…

In the ChicagoIngredientFactory, return the proper ingredient object in each method and return a list of Garlic and RedPepper for the Veggies.

G In the Pizza class...
1 Define properties for Dough, Sauce, Veggies, Cheese, Pepperoni and Clams and remove the string properties for Dough, Sauce…

Define properties for Dough, Sauce, Veggies, Cheese, Pepperoni and Clams and remove the string properties for Dough, Sauce and toppings.

2 Make the Prepare method abstract and the Pizza class abstract.
H In the CheesePizza class...
1 Define a private field of type IPizzaIngredientFactory named ingredientFactory.
2 Define a constructor with a parameter of type IPizzaIngredientFactory named ingredientFactory and inside the constructor…

Define a constructor with a parameter of type IPizzaIngredientFactory named ingredientFactory and inside the constructor assign the field.

3 Override the Prepare method.
4 In the Prepare method, paste the code below:
Console.WriteLine("Preparing: " + this.Name);
this.Dough = this.ingredientFactory.CreateDough();
this.Sauce = this.ingredientFactory.CreateSauce();
this.Cheese = this.ingredientFactory.CreateCheese();
I Repeat the previous step for the ClamPizza, PepperoniPizza and VeggiePizza classes and call the proper methods. (i.e. the…

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).

J Remove the Chicago Pizzas and NY Pizzas and all classes inside them.

This is where the beauty of the Factory Pattern comes in. We do not need more "concrete" classes to make the object we want.

K Change all of the pizzas in the store classes from the specific to generic types.
L In the ChicagoPizzaStore and NYPizzaStore classes...
1 At the beginning of CreatePizza, instantiate ChicagoIngredientFactory in ChicagoPizzaStore and NYIngredientFactory in…

At the beginning of CreatePizza, instantiate ChicagoIngredientFactory in ChicagoPizzaStore and NYIngredientFactory in NYPizzaStore. Name the local variable ingredientFactory.

2 Pass the ingredientFactory into each Pizza constructor.
M Check Your Work:
1 Change the pizza Joel orders from cheese to clam.
2 Set a breakpoint in the Main method on the first call to OrderPizza.
3 Start the application.
4 Press F11 into the CreatePizza method that should be in the NYPizzaStore.
5 Step into the Prepare method.
6 Step through the Prepare method.
7 When the debugger returns to PizzaStore, the pizza object should have the following Values/Types.
NY Pizza for 7.3 Factory Pattern Lab. Use the adjacent task instructions to identify the required controls, output, or debugger values.
NY Pizza
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}}
8 F10 over the next call to OrderPizza in Main. The pizza object should have the following Values/Types.
Chicago Pizza for 7.3 Factory Pattern Lab. Use the adjacent task instructions to identify the required controls, output, or debugger values.
Chicago Pizza
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}}
Zip up your assignment and submit it to Canvas.
6.Zip up your assignment and submit it to Canvas.
A Zip up your assignment and submit it to Canvas.
Completion review
7.Review the feature acceptance criteria
A Confirm the required results

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.