protected override Pizza CreatePizza(string type) { Pizza pizza = null; IPizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory(); if (type.Equals("cheese")) { pizza = new CheesePizza(ingredientFactory); pizza.name = "New York Style Cheese Pizza"; } else { pizza = new PepperoniClamPizza(ingredientFactory); pizza.name = "New York Style Pepperoni Clam Pizza"; } return(pizza); }
static void Main(string[] args) { IPizzaIngredientFactory pizzaIngredientFactory = new NYPizzaIngredientFactory(); IFactory factory = new CheesePizzaFactory(); Pizza cheesePizza = factory.CreatePizza(pizzaIngredientFactory); cheesePizza.Prepare(); cheesePizza.Cut(); cheesePizza.Bake(); cheesePizza.Box(); //输出: //Preparing Cheese //NYDough //NYSauce //Cutting the Cheese //Baking the Cheese //Boxing the Cheese }
protected override Pizza CreatePizza(string type) { Pizza pizza = null; IPizzaIngredientFactory pizzaIngredientFactory = new NYPizzaIngredientFactory(); switch (type) { case "cheese": pizza = new CheesePizza(pizzaIngredientFactory) { Name = "New York Style Cheese Pizza" }; break; case "veggie": pizza = new VeggiePizza(pizzaIngredientFactory); default: throw new NotImplementedException($"DO NOT Provide {type} "); } }
protected override Pizza CreatePizza(string type) { Pizza pizza = null; // Define qual é a fábrica de ingredientes IPizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory(); if (type.Equals("cheese")) { // Cria uma instancia de pizza passando a fábrica de ingredientes definida (Ex.: Nova York) pizza = new CheesePizza(ingredientFactory); pizza.Name = "Pizza de Queijo no estilo Nova York"; } else if (type.Equals("clam")) { pizza = new ClamPizza(ingredientFactory); pizza.Name = "Pizza de Mariscos no estilo Nova York"; } // etc. return(pizza); }