Home  /  Questions  /  Question



113   96.7
Feb 17, 2010


Abstract Factory versus Factory Method

I have been trying to get my head around Factory patterns, particularly Abstract Factory.
Can anyone, in a couple sentences, explain the difference between Abstract Factory and Factory Method?

Much appreciated.
 1 comment
 
Hi, i think the following def may help you, Factory Method: Creates an instance of several derived classes Abstract Factory: Creates an instance of several families of classes --- Ranjit Kamble  Mar 15, 2010



330   99.9
Feb 23, 2010
It is very simple.

You have a single Factory class and you use it to create many items through it, then it is a 'Factory Method' pattern. e.g. Have a Factory class for Cars say 'CarFactory' and you use it for creating many cars. This is single level of Factory pattern and create Cars choosing its type (SUV/Wagon/etc...) at runtime.

Now assume we have many factory classes for each brand (like Benz/BMW/etc...) choose the factory at runtime and introduce another level 'Factory Method' pattern to choose the actual Factory class, then it is Abstract Factory. (i.e. a Factory Pattern implementation for Factory classes.)

e.g. I have a abstract class as CarFactory. There are may Factory patterns team implemented it, say BenzCarFactory, BmwCarFactory and they create their types (SUV/Wagon/etc...). It is Abstract Factory Pattern.

In few words....

Taking Factory Pattern to 2nd level, it is Abstract Factory Pattern.

Abstract Factory Pattern needs many Factory classes that implements a single 'Abstract Factory'  class. And the 'Factory Method' pattern deals with a single Factory class.

Still, Factory Method uses only Concrete Factory. The other uses 'Abstract Factory' class.

 1 comment
 
Good explanation --- Ashok Gupta  Mar 13, 2010

139   96.7
Feb 22, 2013
I wouldn't worry about it as the differences are conceptional and have blurred over time. 
What matters is the concept of a Factory, which is just a components that creates object instances.  It gives you more control over the object creation process than just newing an object. 

In the real-world that is all you need to know, it seems to me. 

50   50
Mar 13, 2010
 A very good exmple I read once on the web is as follows: 

Say, you want a door for your house:
One way is to call carpenter and let him take the measurements and then get you the door. --- factory method 
Second way is to go to shop which sells doors and get the one which fits your requirements. --- abstract factory

56   94.9
Mar 05, 2010
Factory Methods lets the subclasses to decide which class to instatiate.whereas the abstract factory provides and interface for creating families of related or dependent objects.  Both patterns help us in decoupling our code by creating objects but Factory Methods uses inheritence and the abstract Factory uses object composition

50   50
Mar 01, 2010
In simple words,


Factory pattern ---to create a single object (U will have only one Factory class here)

Abstract Pattern--- To create chain of related objects through multiple Factory Classes.