Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Motivation for Simple Factory and Factory Method Pattern

I know there are a lot of questions out there about differences of different factory patterns, but the answers are so different and confusing. The books that i read use unclear and (over)simplified examples. I have a number of questions even after reading Wikipedia explanations, and a lot of online explanations about them including all on these site. The book that I'm currently reading is Head First Design Patterns.

In Simple Factory the client uses separate class (Creator) with factory method (which CAN be static) to return Products.

In Factory Method Pattern the Creator and the Client are the same thing and they use abstract method in the same class to create new Products, on which they operate in that same class. Of course the Creator (or Client) are abstract, so the decision about making the Concrete Product is deferred to sub-classes.

  1. Is my understanding correct (for ex. are the Client and Creator in FMP the same thing, I never see Client in FMP diagram)?

  2. In Factory Method Pattern it seams that the create method is not reusable outside of the Creator, so it can only be reused when making some new Creator?

  3. What are the situations where I can choose one over the other?

(P.S. Please don't mark this as duplicate, I want to make this thing clear on this site)

like image 301
croraf Avatar asked Dec 30 '13 21:12

croraf


People also ask

What is simple factory pattern?

In another way, we can say: In simple factory pattern, we have a factory class which has a method that returns different types of object based on given input. Let us understand with an example:

When to use the factory method?

To be precise, if you want to control product creation steps and want to control every step, and steps are customized, then we use the Factory method. To put it a simple way, if you want to control an algorithm/strategy of a family of products, you can think about the Factory method pattern.

What are the advantages of the factory method design pattern?

One of the major advantages of using the Factory Method design pattern is that our code becomes loosely coupled in that the majority of the components of our code are unaware of other components of the same codebase.

How does the simple factory work?

With the Simple Factory, we try to abstract the creation details of the product from our caller. The only thing our caller knows, by calling a Static method and passing the desired parameter it, is that it returns an object of the TV type. But how TV is created, the client code does not know. Actually, they do not care how you create that product.


1 Answers

Simple Factory is a factory in the form of a class. Because of that it doesn't solve the problem with elegance, since for every new subclass of Product you will have to edit the switch statement in the create() method. This is a violation of the Open/Close Principle. A potential way to make a Simple Factory useful would be to use class registration as sawn here: http://www.oodesign.com/factory-pattern.html

Factory Method is a factory in the form of a method (hence the name). This doesn't violate the Open/Close Principle since you deal with change by extending and not by modifying code.

Your understanding is correct. Client and Creator/Factory in FMP are the same since the Factory (method) is part of the Client.

It is true that the create method in FMP is not reusable. That is ok though, because this is not an attempt to create an application-wide Factory of the Product, but a way for the Client to create his depended objects without using new.

I cannot answer you third question since I believe it is based on preference.

like image 186
em_ma Avatar answered Sep 20 '22 06:09

em_ma