Dofactory.com
Dofactory.com
 Back to list
Views:   84.1K
Replies:  5
Archived

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.
Bogusław Dobrogost, Feb 17, 2010
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
Mar 15, 2010
Reply 1
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. 
Christopher Ronny, Feb 22, 2013
Reply 2
 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
Ashok Gupta, Mar 13, 2010
Reply 3
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
Jebby Philip, Mar 05, 2010
Reply 4
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.


Pathik Rawal, Mar 01, 2010
Reply 5
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.

Karthikkeyan Vijayan, Feb 23, 2010
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.