3

what makes me confused is that this article says that the motivation of using factory method over the simple factory To override Open closed principle violation (Check Problem with Simple Factory Pattern section here) but in the head first design patterns book the example still violates the open closed principle as in the image in this link

enter image description here

so what is the benefits of using factory method over simple factory ?

Rehab Reda
  • 193
  • 7
  • 16

2 Answers2

1

In Factory Patterns - Simple Factory Pattern article author wrote:

Simple Factory Pattern is not a part of Gang of Four (GoF) book but Factory Method Pattern and Abstract Factory Patterns are part of this standard book.

Simple Factory Pattern (SFP) is described in article almost as Factory Method Pattern (FMP) and looks a little bit like a solution for a lazy developer. SFP introduces only one class which creates all kind of fans. FMP introduces one factory for each fan type. Now, assume that in example console app FanType is provided by client. How you would implement that? You need to create switch or if-else pairs anyway. So it depends from you where you put this kind of code. It looks like SFP: is switch + new based on argument.

From other side in book below this picture you can find:

So, by encapsulating the pizza creating in one class, we now have only one place to make modifications when the implementation changes.

Don’t forget, we are also just about to remove the concrete instantiations from our client code.

So, answer for your question: these two are the same from Factory Method Design Pattern point of view and it's intents. It depends on which level you want to implement switch or if-else pairs. See this example: Factory Method in Java where it is moved to main method.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • Thank you so much but can u clarify more the last 3 lines : ) – Rehab Reda Sep 15 '19 at 04:33
  • @RehabMohamed, have you seen this example: [Factory Method in Java](https://sourcemaking.com/design_patterns/factory_method/java/1)? In `FactoryMethodDemo` class, in `main` method there are `if-else` pairs used to find reader. In `NYPizzaStore` we have this `if-else` pairs inside of factory method. On [Factory Method Design Pattern](https://sourcemaking.com/design_patterns/factory_method) page read "Intent" section. It helps to find a reason when to implement factory method. But it does not tell how to implement it. So, in one example you have `if-else` pairs inside, in other outside a class. – Michał Ziober Sep 15 '19 at 08:24
0

It seems you have a Simple Factory inside a Factory Method pattern. You can add a new PizzaStore by creating new classes (NYPizzaStore, LAPizzaStore, ...), ie without the switch. But the NYPizzaStore uses a Simple Factory to create pizzas (and a switch inside).

See this question for differences between factories.

About the open-closed principle: the PizzaStore is closed for modification (the abstract behaviour is defined and foreign classes can rely on it), but there is no limitation to adding new stores: open for extension.

jferard
  • 7,835
  • 2
  • 22
  • 35
  • Thank you so much, I read the answer of the question but didn't get this line (It is better than a Simple Factory because the type is deferred to a sub-class.) – Rehab Reda Sep 15 '19 at 04:38
  • @RehabMohamed: this line from the link given above means, I think, that the *concrete* type of the `Pizza` is decided by the chosen `PizzaStore` subclass, not by the `PizzaStore` class/interface itself. Open-closed principle: creating a new concrete `Pizza` does not imply a `PizzaStore` update (closed for **modification**), just creating a new `PizzaStore` subclass/implementation (open for **extension**). It is better... unless you don't need it (https://en.wikipedia.org/wiki/KISS_principle). – jferard Sep 16 '19 at 09:33