Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory Pattern but with object Parameters

Tags:

Take the following classic factory pattern:

public interface IPizza {     decimal Price { get; } }  public class HamAndMushroomPizza : IPizza {     decimal IPizza.Price     {         get         {             return 8.5m;         }     } } public abstract class PizzaFactory {     public abstract IPizza CreatePizza(ItalianPizzaFactory.PizzaType pizzaType); }  public class ItalianPizzaFactory : PizzaFactory {     public enum PizzaType     {         HamMushroom,         Deluxe,         Hawaiian     }      public override IPizza CreatePizza(PizzaType pizzaType)     {         switch (pizzaType)         {             case PizzaType.HamMushroom:                 return new HamAndMushroomPizza();             case PizzaType.Hawaiian:                 return new HawaiianPizza();             default:                 throw new ArgumentException("The pizza type " + pizzaType + " is not recognized.");         }     } } 

What if one (or many) of the Concrete Pizzas requires a parameter specific to the concrete implementation at construction. For example, lets say the HamAndMushroom factory requires a parameter called, MushroomType and this parameter would be required to instantiate the object?

like image 997
guazz Avatar asked Jul 28 '10 14:07

guazz


People also ask

What is the difference between Factory Pattern and facade pattern?

The facade pattern is used when you want to hide an implementation or it is about changing interface of some class or set of classes. Builder hides the process of construction by decomposing it in smaller steps. Abstract factory pattern is used when you want to hide the details on constructing instances.

What is the difference between factory method and prototype design pattern?

Factory pattern is used to introduce loose coupling between objects as the factory will take care of all the instantiation logic hiding it from the clients. Prototype pattern on the other hand is used when the cost of creating an object is large and it is ok to copy an existing instance than creating a new instance.

Can we use abstract class in Factory Design Pattern?

Yes, as also a java 8 interface with an implemented default method.

What is the difference between MVC and Factory Pattern?

MVC is a Model-View-Controller - high-level architectural pattern which intend is separating UI from from model. Factory is a low-level pattern which intend is a creation of objects.


1 Answers

You can add parameters to the creator method(s) of your factory. However, if the number of parameters is getting higher (for me that would be more than 2-3), and especially if some or all of those parameters are optional with reasonable default values, you may consider turning the factory into a Builder instead.

That may be especially appropriate for pizzas, where you usually have the same crust, just with different (combinations) of toppings. A Builder models very closely the common way of ordering e.g. "a pizza with salami, tomatoes, maize and double cheese". OTOH for "predefined" pizzas you may want to define helper factory methods, e.g. createMargaritaPizza or createHawaiiPizza which then internally use the builder to create a pizza with the toppings specific to that kind of pizza.

like image 77
Péter Török Avatar answered Sep 21 '22 16:09

Péter Török