Home  /  Questions  /  Question

250   96.9
Mar 08, 2012

Factory pattern Vs Abstract Factory pattern

I want to know what are the difference between them

70   96.1
Mar 13, 2012

Factory: A factory that creates objects that derive from a particular base class.

Abstract factory: A factory that creates other factories, and these factories in turn create objects derived from base classes. You do this because you often don't just want to create a single object (as with Factory method) - rather, you want to create a collection of related objects.

60   95.3
Apr 05, 2012
Factory Pattern:

1. Createobject through inheritance

2. Produceonly one product

3. Implementscode in the abstract creator that make use of the concrete type that sub class produces.

Abstract Factory Pattern:

1. Createobject through composition

2. Produce families ofproducts

3. Concretefactories implements factory method to create product

80   96.4
May 21, 2012

Real Life Example. (Easy to remember)


Imagine you are constructing a house and you approach a carpenter for a door. You give the measurement for the door and your requirements, and he will construct a door for you. In this case, the carpenter is a factory of doors. Your specifications are inputs for the factory, and the door is the output or product from the factory.

Abstract Factory

Now, consider the same example of the door. You can go to a carpenter, or you can go to a plastic door shop or a PVC shop. All of them are door factories. Based on the situation, you decide what kind of factory you need to approach. This is like an Abstract Factory.

I created an account solely for the purpose of thanking you for those perfect analogies. So thank you :) --- Charlie Malbaurn  Jun 05, 2012
Easy to understand :) --- Kien Nguyen  Nov 17, 2016
juz wow.. what a simple explanation... i also signed up just to say thanks to you :) keep it up --- Rajni G  Apr 17, 2017
Two question if you can answer: 1 . Factory design pattern gives one object.. Abstract Factory gives you factories .. What if we need multiple products of one type.. like 5 plastic doors.. would we be making abstract factory in this case? --- Rajni G  Apr 17, 2017
I agree on your first part. But I have some disagreement with 2nd viz. For abstract factory...I think the carpenter would still be the one making doors...but for installing a door, only a door is not enough...you need hinges, knobs, handles, stoppers, nails etc. So we would have a NailFactory, KnobFactory, HandleFactory etc. to make a complete door. So the abstract factory would be solely responsible for making complete doors with the help of NailFactory, HandleFactory etc. --- Gaurav M  Jun 27, 2017

0   0
Sep 17, 2017
Factory : While creating object , you have to pass the concrete- product -class object from Factory.

There is no interaction between client and concrete class. Client Interacts with Factory Only. Client just calls Factory method, and in turn - factory calls respective concrete product via inheriting abstract product

Abstract Factory : While creating object , you have to pass the concrete -factory object to call Abstract Factory method, which in turn, calls  respective factory related Methods.

Again,  no interaction is there between client and concrete class.Client Interacts with Abstract Factory Only. 
Client just calls Abstract Factory method, and in turn - Abstract factory calls respective concrete product via implementing its method in concrete Factory.

Simple Rule : Abstract Factory = Factory 1 + Factory 2 + ------------Factory n , so simply , you have to pass concrete factory object to get respective factory methods , and definitely  intern class calls  methods ----  based on this factory instance. Rest is same as in factory . So this way, you get all the products of required group.