I'm analyzing the benefits of the Factory Method design pattern compared to a slightly tweaked Simple Factory pattern, similar to the example in 'Head First Design Patterns'. For instance, I have two pizza factories: NewyorkPizzaFactory and ChicagoPizzaFactory, both implementing a PizzaFactory interface with a createPizza method that takes a type parameter. In this setup, PizzaStore accepts a PizzaFactory in its constructor and uses it to create pizzas. I feel this design leans more towards composition rather than inheritance. Can anyone clarify what advantages the Factory Method provides over this kind of Simple Factory?
1 Answer
Your pseudocode seems like it's mixing concepts a bit. Usually, in the context of the Factory Method, each factory should be responsible for creating a single type of pizza without needing to take a type as a parameter. A standard factory pattern would let you decide which factory to use upstream, allowing for greater flexibility in cases where the input can change. Plus, using dependency injection means you can easily swap factories without changing the client code. Just a thought!
I get what you're saying, but the reason I included a type is to allow for variations of New York or Chicago pizzas. What do you think is the core advantage of the Factory Method over this implementation?