What is the motivation of using factory method pattern rather than simple factory

RehabMohamed :

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 ?

Michał Ziober :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=164592&siteId=1