Ch4PizzaStore: last part with AbstractFactory. This example in the book is really TERRIBLE.

This commit is contained in:
2025-11-13 18:34:37 +01:00
parent aa967b81a4
commit 08b38b6643
2 changed files with 94 additions and 26 deletions

View File

@@ -1,17 +1,15 @@
namespace Ch4PizzaStore; namespace Ch4PizzaStore;
public abstract class Pizza(string name, string dough, string sauce, List<string> toppings) { public abstract class Pizza(string name) {
public string Name { get; } = name; public string Name { get; internal set; } = name;
public IDough? Dough { get; internal set; } = null;
public ISauce? Sauce { get; internal set; } = null;
public IVeggies[]? VeggiesArray { get; internal set; } = null;
public ICheese? Cheese { get; internal set; } = null;
public IPepperoni? Pepperoni { get; internal set; } = null;
public IClams? Clams { get; internal set; } = null;
internal void Prepare() { internal abstract void Prepare();
Console.WriteLine($"Preparing {Name}");
Console.WriteLine($"Tossing {dough} dough...");
Console.WriteLine($"Adding {sauce} sauce...");
Console.WriteLine("Adding toppings:");
foreach (var topping in toppings) {
Console.WriteLine($"\t{topping}");
}
}
// While in Java all methods are by default `virtual`, C# is the same as C++, // While in Java all methods are by default `virtual`, C# is the same as C++,
// all methods are final/selaed until explicitly marked as virtual. // all methods are final/selaed until explicitly marked as virtual.
@@ -26,15 +24,28 @@ public abstract class Pizza(string name, string dough, string sauce, List<string
internal virtual void Box() { internal virtual void Box() {
Console.WriteLine("Place pizza in official PizzaStore box"); Console.WriteLine("Place pizza in official PizzaStore box");
} }
public override string ToString() {
return "Pizza!"; // TODO: code to print the pizza here
}
} }
public class NYStyleCheesePizza() : Pizza("NY Style Sauce and Cheese Pizza", "Thin Crust Dough", "Marinara Sauce", public class CheesePizza(IPizzaIngredientFactory ingredientFactory, string name) : Pizza(name) {
["Grated Reggiano Cheese"]); internal override void Prepare() {
Console.WriteLine($"Preparing {Name}");
Dough = ingredientFactory.CreateDough();
Sauce = ingredientFactory.CreateSauce();
Cheese = ingredientFactory.CreateCheese();
}
}
public class ChicagoStyleCheesePizza() : Pizza("Chicago Style Deep Dish Cheese Pizza", "Extra Thick Crust Dough", public class ClamPizza(IPizzaIngredientFactory ingredientFactory, string name) : Pizza(name) {
"Plum Tomato Sauce", ["Shredded Mozzarella Cheese"]) { internal override void Prepare() {
internal override void Cut() { Console.WriteLine($"Preparing {Name}");
Console.WriteLine("Cutting the pizza into square slices"); Dough = ingredientFactory.CreateDough();
Sauce = ingredientFactory.CreateSauce();
Cheese = ingredientFactory.CreateCheese();
Clams = ingredientFactory.CreateClams();
} }
} }
@@ -53,18 +64,76 @@ public abstract class PizzaStore {
public class NYPizzaStore : PizzaStore { public class NYPizzaStore : PizzaStore {
protected override Pizza CreatePizza(string type) { protected override Pizza CreatePizza(string type) {
var ingredientFactory = new NYPizzaIngredientFactory();
return type switch { return type switch {
"cheese" => new NYStyleCheesePizza(), "cheese" => new CheesePizza(ingredientFactory, "New York Style Cheese Pizza"),
"clam" => new ClamPizza(ingredientFactory, "New York Style Clam Pizza"),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null) _ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
}; };
} }
} }
public class ChicagoPizzaStore : PizzaStore { public interface IDough;
protected override Pizza CreatePizza(string type) {
return type switch { public interface ISauce;
"cheese" => new ChicagoStyleCheesePizza(),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null) public interface ICheese;
};
public interface IVeggies;
public interface IPepperoni;
public interface IClams;
public interface IPizzaIngredientFactory {
public IDough CreateDough();
public ISauce CreateSauce();
public ICheese CreateCheese();
public IVeggies[] CreateVeggies();
public IPepperoni CreatePepperoni();
public IClams CreateClams();
}
public class ThinCrustDough : IDough;
public class MarinaraSauce : ISauce;
public class ReggianoCheese : ICheese;
public class Garlic : IVeggies;
public class Onion : IVeggies;
public class Mushroom : IVeggies;
public class RedPepper : IVeggies;
public class SlicedPepperoni : IPepperoni;
public class FreshClams : IClams;
public class NYPizzaIngredientFactory : IPizzaIngredientFactory {
public IDough CreateDough() {
return new ThinCrustDough();
}
public ISauce CreateSauce() {
return new MarinaraSauce();
}
public ICheese CreateCheese() {
return new ReggianoCheese();
}
public IVeggies[] CreateVeggies() {
return [new Garlic(), new Onion(), new Mushroom(), new RedPepper()];
}
public IPepperoni CreatePepperoni() {
return new SlicedPepperoni();
}
public IClams CreateClams() {
return new FreshClams();
} }
} }

View File

@@ -3,9 +3,8 @@
using Ch4PizzaStore; using Ch4PizzaStore;
PizzaStore nyStore = new NYPizzaStore(); PizzaStore nyStore = new NYPizzaStore();
PizzaStore chicagoStore = new ChicagoPizzaStore();
var pizza = nyStore.OrderPizza("cheese"); var pizza = nyStore.OrderPizza("cheese");
Console.WriteLine($"Ethan ordered a {pizza.Name}\n"); Console.WriteLine($"Ethan ordered a {pizza.Name}\n");
pizza = chicagoStore.OrderPizza("cheese"); pizza = nyStore.OrderPizza("clam");
Console.WriteLine($"Joel ordered a {pizza.Name}\n"); Console.WriteLine($"Joel ordered a {pizza.Name}\n");