I ran into a question on stackoverflow the other day that sort of shocked me.  It was a piece of code, with the author asking why it wasn't a factory pattern.  The thing that shocked me was that the pattern that everyone was agreeing was a factory method pattern, really wasn't. I’m pretty sure that this a rare occurrence for stackoverflow, they seem to have an abundance of gifted developers who love to answer questions.  Regardless, here is my proverbial kick at can, trying to explain the differences as clearly as I can.   (BTW, if you're looking for a different take on patterns, I'd recommend the Head First Design Patterns book, which I thought provided a pretty good introduction into patterns, in way that doesn't put you to sleep.)

For this post, I simply want to discuss three factory designs: the Simple Factory, the Factory Method Pattern, and the Abstract Factory Pattern.  But instead of concentrating on learning the patterns (you can find all these and more at  dofactory ), I’m going to concentrate on what I see as the key differences between the three, and how you can easily recognize them.

As a bit of background, the thing that all three have in common is that they are responsible for creating objects.  The calling class (which we call the “client”) wants an object, but wants the factory to create it. I guess if you wanted to sound really professional, you could say that factories are used to encapsulate instantiation.

So what's the difference between a simple factory, a factory method design pattern, and an abstract factory?



Simple Factory

The easiest way for me to remember this is that a simple factory is called directly by the class which wants to create an object (the calling class is referred to as the "client").  The simple factory returns one of many different classes.  All the classes that a simple factory can return either inherit from the same parent class, or implement the same interface.




Step One:
you call a method in the factory.  Here it makes sense to use a static method.  The parameters for your call tell the factory which class to create.

Step Two:  the factory creates your object. The only thing to note is that of all the objects it can create, the objects have the same parent class, or implement the same interface.

Step Three: factory returns the object, and this is why step two makes sense.  Since the client didn't know what was going to be returned, the client is expecting a type that matches the parent class /interface.

Sticklers will note that this is not an “official” design pattern. While this is true, I find it useful and think that it merits discussion!




Factory Method
 
The official definition of the pattern is something like: a class which defers instantiation of an object to subclasses.  An important thing to note right away is that when we're discussing the factory pattern, we're not concentrating on the implementation of the factory in the client, but instead we're examining the manner in which objects are being created. 

In this example the client doesn't have a direct reference to the classes that are creating the object, but instead has reference to the abstract "Creator". (Just because the creator is abstract, doesn't mean this is the Abstract Factory!).  It is a Factory Method because the children of “Creator” are responsible for implementing the “Create” method. Another key point is that the creator is returning only one object.   The object could be one of several types, but the types all inherit from the same parent class.

 Step One: the client maintains a reference to the abstract Creator, but instantiates it with one of the subclasses.  (i.e. Creator c = new ConcreteCreator1(); )

 Step Two: the Creator has an abstract method for creation of an object, which we'll call "Create".  It's an abstract method which all child classes must implement.  This abstract method also stipulates that the type that will be returned is the Parent Class or the Interface of the “product”.

Step Three: the concrete creator creates the concrete object.  In the case of Step One, this would be "Child Class A".

Step Four: the concrete object is returned to the client.  Note that the client doesn’t really know what the type of the object is, just that it is a child of the parent.




Abstract Factory

This is biggest pattern of the three.  I also find that it is difficult to distinguish this pattern from the Factory Method at a casual glance.  For instance, in the Factory Method, didn’t we use an abstract Creator?  Wouldn’t that mean that the Factory Method I showed was a actually an Abstract Factory?  The big difference is that by its own definition, an Abstract Factory is used to create a family of related products (Factory Method creates one product).


Step One:
  the client maintains a reference to an abstract Factory class, which all Factories must implement.  The abstract Factory is instantiated with a concrete factory.

Step Two: the factory is capable of producing multiple types.  This is where the “family of related products” comes into play.  The objects which can be created still have a parent class or interface that the client knows about, but the key point is there is more than one type of parent.

Step Three: the concrete factory creates the concrete objects.

Step Four: the concrete objects are returned to the client.  Again, the client doesn’t really know what the type of the objects are, just that are a children of the parents.

See those concrete factories?  Notice something vaguely familiar?  There using the Factory Method to create objects.



So, being as brief as I can:
- A Simple factory is normally called by the client via a static method, and returns one of several objects that all inherit/implement the same parent.
- The Factory Method design is really all about a “create” method that is implemented by sub classes.
- Abstract Factory design is about returning a family of related objects to the client.  It normally uses the Factory Method to create the objects.


 


Shyam
04/30/2010 05:43

This is the best explanation I got on the differences between the patterns on the net. Simple and to-the-point.

Thanks.

Reply
01/07/2013 17:58

Indeed this makes my concept clear on factory pattern. Another <a href="http://javarevisited.blogspot.com.br/2013/01/difference-between-factory-and-abstract-factory-design-pattern-java.html">difference between Factory and abstract Factory pattern</a> is that, later is used to create factory which int turns create items.

Reply
Anushree
02/02/2011 23:31

Very correct,this is truly very convincing

Reply
Manju Sandhu
02/02/2011 23:32

Very precise and nicely described the exact difference in these patterns

Reply
rex
04/04/2011 13:29

Hi,

I applied the Simple Factory on one of my Projects and I still do not understand where does the PizzaStore come in on your UML Diagram.

Can you edit your graphic to accommodate the sample from the HFDP book? ^_^

Thanks,
Rex

Reply
Simone
08/08/2011 10:00

Great explanation. thanks a lot!!!

Reply
egyptianMan
03/11/2012 18:35

You are so awesome! I cannot thank you enough for this!

Reply
Roman
03/19/2012 06:16

Very good and comprehensive explanation. The interesting point is that there is no code samples but still everything is absolutely clear.

Reply
SG
04/03/2012 08:47

Very good explanation of the difference b/w the three patterns

Reply
06/20/2012 18:36

I would like to thank you for the articles you are writing.the content is good, and you’re an educated writer unlike most of the blogs I come across when searching on this topic.

Reply
06/20/2012 18:44

I enjoyed your blog. It’s easy to read,The post is quite neat and every word is so simple that is easy to understand. That’s one actually nice post.

Reply
AR
06/26/2012 10:02

Thanks for this article. Though it's good but somehow I am not able to judge, when should I use 'Simple factory' and when should I use 'Factory Method' ?

Please help me understand.

Reply
09/04/2012 05:21

Thanks, Article isv ery precise and nicely described the exact difference in these patterns, If you add one small example for each pattern then it would be more better to understand.

Reply
persian Man
09/11/2012 05:54

thank you!

Reply
Manish Patwa
09/17/2012 05:21

This is awesome example!!! I loved it.
Thanks a ton Corey!

Reply
Rubaiyat
11/21/2012 23:01

Really, very simple and helpful. But I need a simple real world example that describes difference

Reply
Jonas Pettersson
12/10/2012 00:23

Hi, I must say "Good Job".

I am reading a course in Design Patterns at a swedish University and though i have the book "Heads First Design Pattern" book you mentioned your explanation is still superior to that of the book.

Thank you!

Reply
Sunil
01/30/2013 19:21

This is the best explanation I got

Reply
Deano
02/08/2013 01:46

As you said, Head First Design Patterns is damn useful for a newbie (or anyone struggling with the basics of a pattern), but this article has helped me over and over again when trying to remember the differences of the different factories, thanks!

Reply
thanks Corey, for the very clear differences!!
02/16/2013 23:10

Reply
Ramana
03/12/2013 10:52

It precisely answers my questions/doubts about simple-factory & factory-method pattern, good Job.

Reply
03/18/2013 10:33

Thanks man! kool

Reply
Jiten Shah
03/27/2013 07:24

thanks a lot. very simple explanation.

Reply
Naveen
04/14/2013 23:31

Superb, a very neat explanation. Thanks for your effort.

Reply
Bhavneesh
05/24/2013 06:12

Simple but most effective!! Must for everyone.

Reply
Robin
05/30/2013 03:14

Good explanation. But 1 very serious doubt.
As per my understanding factory method is for return the subclass object( product ).
In you explanation about factory method, in step 1, you mentioned as Creator c = new ConcreteCreator1(); . And the creator is having an abstract method as 'Create()'. Since you call new ConcreteCreator1(), the object of ConcreteCreator1 is already created, then why we need a 'Create()' method?
Please clarify my query..

Reply
Santosh
06/02/2013 23:49

hear 'Creator c' is a factory and 'Create()' is a method to create different products.

Creator c = new ConcreteCreator1() -- This a factory instance
c.Create()--> return new Product();

Reply
Santosh
06/02/2013 23:44

Really very simple explanation about the differences between the patterns

Reply
mkd
06/09/2013 23:16

Thank you so much for this easy to understand concept. I was so confused before reading this article.

Reply
Shailesh
06/26/2013 21:18

Thank you for explanation. Now I can see the difference between factory method and abstract factory.

Reply
Elham
07/20/2013 20:40

Extraordinary Thanks a lot

Reply
Sanhita
07/25/2013 05:08

Thanks.. it was great explanation

Reply
Jack paul
08/13/2013 23:43

very nice post.

Reply
Mayur
10/04/2013 08:53

Thanks man...really helped in understanding difference between these factory patters

Reply
Rajita
10/11/2013 04:46

Very well explained. Bang on the confusions I have had on factory method and abstract factory. Thanks a ton

Reply
11/12/2013 01:46

Very Nice article.

It inspired me to create blog (http://simpledesignpatterns.blogspot.co.il/) and explain this three patterns too (http://simpledesignpatterns.blogspot.co.il/2013/11/factory-family.html)

Thanks for inspiration!

Reply
anand
01/29/2014 06:44

very nice. the best explanation i have seen. thank you so much.

Reply
Anonymous
02/03/2014 02:46

one word --> Excellent !

Reply
04/05/2014 23:32

Thanks for your clear explanation on these three related design patterns.

Reply

nice article and an excellent article containing the perfect comparison

Reply
mem
05/19/2014 05:57

Thanks a lot.
you made it very simple and clear.

Reply
Mohsina A v
05/29/2014 19:47

The information given in this page is really helpful to beginners...thanks a lot

Reply
David Brower
07/02/2014 01:38

Excellent introduction. I'd also definitely recommend <i>Head First Design Patterns</i> as a primer on the subject, even before the GoF book.

Reply
ShaQ
07/08/2014 12:35

I had visited your website around the time when you had just created it.
I had read head first and the horrendous example used them to explain factory was downright ridiculous. I then read your article, and 15 minutes later I no longer felt like pulling my hair.

So when you have done such a wonderful job, why not have an issue example here itself? Why have us visit some creepy website?
A programming example here, would simply make it complete!!

Reply
ShaQ
07/08/2014 12:39

I had visited your website around the time when you had just created it.
I had read, head first design patterns book and the horrendous example used by them to explain factory was downright ridiculous. I then read your article, and 15 minutes later, I no longer felt like pulling my hair.

So when you have done such a wonderful job, why not have an example right here itself? Why have us visit some creepy website?
A programming example here, would simply make it complete!!

Reply
ShaQ.
07/08/2014 12:39

Posting again cos of the typos..

I had visited your website around the time when you had just created it.
I had read, head first design patterns book and the horrendous example used by them to explain factory was downright ridiculous. I then read your article, and 15 minutes later, I no longer felt like pulling my hair.

So when you have done such a wonderful job, why not have an example right here itself? Why have us visit some creepy website?
A programming example here, would simply make it complete!!

Reply
ShaQ Blogs
07/08/2014 12:40

Posting again cos of the typos..

I had visited your website around the time when you had just created it.
I had read, head first design patterns book and the horrendous example used by them to explain factory was downright ridiculous. I then read your article, and 15 minutes later, I no longer felt like pulling my hair.

So when you have done such a wonderful job, why not have an example right here itself? Why have us visit some creepy website?
A programming example here, would simply make it complete!!

Reply
07/24/2014 00:54

dfg

Reply
BHReddy
07/29/2014 03:37

I found definition for Factory Design pattern as "Define an interface for creating an object, but let subclasses decide which class to instantiate. " can u give explanation about second sentence of "let subclasses decide which class to instantiate"

Reply
yil
08/09/2014 08:33

Thanks! This is the best explanation so far among the other resources on the web.

Reply
Fareed
08/19/2014 02:38

Thanks for concise explanation. I've been rattling my head trying to understand the 'factory patterns'. I've been through the GOF description, Head First and countless others. However, this was THE place where everything got tied together.

Please someone help me if my concept below is correct.

Factory Method Example Usage: Imagine a factory producing vehicle sub-classes that have the same interface for clients to drive them that includes clutch, gears, steering wheel etc. The concrete factory CreateVehicle() produces vehicle sub-classes like truck, car, van etc. One question here is, 'Will a parametrized simple factory suffice for this example? If yes, then what is the value addition of a factory method pattern?'

Abstract Factory: Now imagine different models of cars. All of them need engine, gearbox, tyres, break-pads and other such common components. ModelA requires a different set of components than ModelB. In this case there would be concrete factory defined for each model type that would have 'simple factory' methods to return each component object like CreateClutch(), CreateBreak() etc. If this is right then my question is, 'How does vehicleBrand incorporate in this example?'

Reply
Moses
08/20/2014 00:14

Thank you very much for the clarification!

Reply
09/10/2014 21:43

I found this post very exciting. I think you will have any other post on this topic? I am also sending it to my friend to enjoy your working style. Cheers!

Reply
09/11/2014 05:59

You can't really say what is beautiful about a place, but the image of the place will remain vividly with you.

Reply
09/25/2014 00:33

your site is very useful but still missiong a pro logo you can use the logo creator

Reply
Max
09/25/2014 21:23

Wanted to stop by and say thanks for this article, it is really helping me grasp the differences between the three Factories.

Reply
Aya
10/07/2014 04:49

very helpful, easy and to the point
Thanks!

Reply
11/01/2014 11:20

I am really very happy to find this particular site. I just wanted to say thank you for this huge read!! I absolutely enjoying every petite bit of it and I have you bookmarked to test out new substance you post.

Reply
11/04/2014 02:52

Hello Corey,

Thank you for your post. Your explanation is very clear indeed.

I have though a doubt which i would be extremely happy if you or someone could help me with.

Are you sure that the Client is not the code in the abstract Creator class? (so the Creator is in fact a Client/Creator)

I am asking this because in my mind from the definition on a Factory Method the factory is a method and on the Abstract Factory the factory is an abstract class (object).

I the Client of the Factory Method is a different class that the abstract Creator then the only difference between the Factory Method and the Abstract Factory is the number and the return type of the create() methods.

Thank you!

Reply
11/09/2014 22:45

Hi

You say that Simple Factory is not an official design pattern. Is this why it is not listed on doFactory (http://www.dofactory.com/net/design-patterns)?

Thanks

Reply



Leave a Reply.

    Author

    I'm a .Net developer, currently living in Montreal.  I've been a developer most of my life, and have been lucky enough to make a living it our of it for the past 7 years.

    Archives

    June 2009
    May 2009

    Categories

    All
    Design Patterns
    Net Svn

    RSS Feed