From 08b38b66431418b13a8315318610af126082ab84 Mon Sep 17 00:00:00 2001 From: "Federico Pasqua (eisterman)" Date: Thu, 13 Nov 2025 18:34:37 +0100 Subject: [PATCH] Ch4PizzaStore: last part with AbstractFactory. This example in the book is really TERRIBLE. --- Ch4PizzaStore/Pizza.cs | 117 +++++++++++++++++++++++++++++++-------- Ch4PizzaStore/Program.cs | 3 +- 2 files changed, 94 insertions(+), 26 deletions(-) diff --git a/Ch4PizzaStore/Pizza.cs b/Ch4PizzaStore/Pizza.cs index 31fb159..b0330a0 100644 --- a/Ch4PizzaStore/Pizza.cs +++ b/Ch4PizzaStore/Pizza.cs @@ -1,17 +1,15 @@ namespace Ch4PizzaStore; -public abstract class Pizza(string name, string dough, string sauce, List toppings) { - public string Name { get; } = name; +public abstract class Pizza(string 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() { - 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}"); - } - } + internal abstract void Prepare(); // 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. @@ -26,15 +24,28 @@ public abstract class Pizza(string name, string dough, string sauce, List 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) }; } } -public class ChicagoPizzaStore : PizzaStore { - protected override Pizza CreatePizza(string type) { - return type switch { - "cheese" => new ChicagoStyleCheesePizza(), - _ => throw new ArgumentOutOfRangeException(nameof(type), type, null) - }; +public interface IDough; + +public interface ISauce; + +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(); } } \ No newline at end of file diff --git a/Ch4PizzaStore/Program.cs b/Ch4PizzaStore/Program.cs index 5ff470a..03426d1 100644 --- a/Ch4PizzaStore/Program.cs +++ b/Ch4PizzaStore/Program.cs @@ -3,9 +3,8 @@ using Ch4PizzaStore; PizzaStore nyStore = new NYPizzaStore(); -PizzaStore chicagoStore = new ChicagoPizzaStore(); var pizza = nyStore.OrderPizza("cheese"); Console.WriteLine($"Ethan ordered a {pizza.Name}\n"); -pizza = chicagoStore.OrderPizza("cheese"); +pizza = nyStore.OrderPizza("clam"); Console.WriteLine($"Joel ordered a {pizza.Name}\n"); \ No newline at end of file