Home  /  Questions  /  Question

60   95.3
Jun 27, 2011

What is the difference between Abstract factory and factory design pattern?


I gone through the Abstract factory and Factory design patterns in this site, but i am having bit confusions.

i.e. What is the difference between Abstract factory and factory design pattern?

What is the difference between Facade and Singleton?

What is the difference between Adapater and Builder?

Thanks in advance

50   50
Jun 30, 2011
1. What is the difference between Abstract factory and factory design pattern

Factory is simple. There is an interface or abstract class having A to N child classes.
Factory method would accept a context, Id, string etc and depedning upon that context, it will create an object of corresponding A, B , C or... N class and return it as Interface or abstract class type. Class containing such a factory method is called a factory.
E.g. Employee is abstarct class. Perm and Contarctor are its children. whenever use logs in, depending upon whether he is Perm or contractor Perm or Contractor class is returned by EmployeeFactory

Abstarct factory has 2 versions:

1. Factory returning a factory rather than an object. On second step pass another context to get concrete object.
E.g. IApplication has App1, App2 objects. there is another interface say IReport which has Excel, PDF and PPT children in form of formats. There will be interfaces IExcel , IPDF, IPPT etc containing App1Excel and App2Excel children. These interfaces would have common interface IDocument . So you pass two context to reach your object. first context returns App1/App2 object which would be factories that would invoke another factory to return Idocument object with concrete class App1Excel , App1PDF, APp1PPt for App1 context and similar for App2. SO here its two step approach. The first step is called abstract factory and second is factory as above.

2. Pass one context and it will return family of objects. interface IX has child classes X1, X2, X3; IY has Y1, Y2, Y3. However, in practical life, X1, Y1; X2, Y2, X3, Y3 always go together and in no other combination. SO I have IA class with A1 containing(not inheriting) X1 and Y1 objects A2 containing X2 and Y2 objects etc. SO. an interface A has IX and IY properties and its concrete children define the set of properties. Pasing a single context fetches an object containing one member from each family comrising a n object having composition of other obejcts from each interface. Such a factory is also called abstract factorty.
 1 comment
Hi Nunu Kohli, Though its a very late reply..! But I am so impressed with this answer that I could not stop myself from creating an account on this site for appreciating this answer. I Google'ed a lot to get an answer to this question, and my search stopped after this answer. Thanks a lot for that explanation..!! I helped a lot..!!! -- Thanks & Regards, Shridhar Kulkarni. --- Shridhar Kulkarni  Oct 27, 2013