Simple Factory Vs Factory Method Vs Abstract Factory by Example
Posted by vivekcek on March 17, 2013
I came across lots of situations in which, I have to explain the difference between a Simple Factory, Factory Method and Abstract Factory patterns.
The main objective of this post is how simply you can explain the difference among these patterns using some example. I hope the readers are aware of these patterns, because I am not going to explain each pattern in depth.
Simple Factory Pattern
Definition:
Creates objects without exposing the instantiation logic to the client.
Refers to the newly created object through a common interface
Diagram:
Explanation:
The heart of above Simple Factory pattern is the ‘MobileFactory’ class. From the Client when we create an instance of this ‘MobileFactory’, this class will load and cache all the classes that implement the ‘IMobile’ interface by using reflection or some other logic. After that we can call the ‘GetMobile(string Name)’ method in the ‘MobileFactory’ class which will return the specified object through the parameter.
The Client will expect an object that implements the ‘IMobile’ interface.
Factory Method
Definition:
Defines an interface for creating objects, but let subclasses to decide which class to instantiate
Refers the newly created object through a common interface.
Diagram:
Explanation:
In Factory Method pattern we will introduce a new interface called ‘IMobileFactory’ and two concrete implementation’s ‘NokiaFactory’ and ‘IphoneFactory’. These concrete classes control the object creation.
In my example the client want a Nokia object. So the steps are given below.
1.The client will load a reference to ‘NokiaFactory’. But Client won’t refer the ‘NokiaFactory’ class directly like the Simple Factory pattern. The client refers the concrete implementation through an interface ‘IMobileFactory’.
2.Then the Client call the ‘CreateMobile()’ method that will return an object of type ‘IMobile’.
3.Here we have to inform the client the concrete implementation to be used through some configuration or parameters.
Abstract Factory
Definition:
Abstract Factory offers the interface for creating a family of related objects, without explicitly specifying their classes
Diagram:
Explanation:
The main point regarding Abstract Factory pattern is that, this pattern creates a family of related objects that have different parent class or interface.
In Abstract Factory pattern the object creation happens in the same way as Factory Method. The only difference is the creation of related objects.
I now updated our old ‘IMobileFactory’ interface with two new methods.
CreateNokiaMobile() – Returns a Nokia objects that implements an ‘INokia’ interface.
CreateAppleMobile()- Returns Iphone objects that implements ‘IApple’ interface.
There are two concrete implementation of ‘IMobileFactory’ named 3GMobileFactory and 4GMobileFactory.
3GMobileFactory – Can return 3G supported mobiles of Nokia and Apple. So as per definition confirms, which returns a family of related objects that have different parents (INokia, IApple).
4GMobileFactory – Can return 4G supported mobiles of Nokia and Apple
Conclusion
By this post my aim was to help you to explain the differences of these three patterns with a simple diagrammatic example.
Dilip said
Excellent!
Very simple and clearly shown the differences… Thanks a lot
Dixit said
awesome explanation and in easy language… thanks 🙂
Allan said
the best explanation that i saw in the net
Sri said
Most the factory method examples I found were not really Factory Method. This is one of the best explanation for three patterns to understand in simple terms. Thanks Vivek.
joe chou said
Excellent!
Dipon Roy said
Great examples!!!
buddyhow said
Thanks buddy. My doubts are cleared on reading your post after 1 month of searching on this stupid Factory patterns. You are great. No post on net is as clear as yours. Best!!
macoratti said
Hi,
I can´t download the project´s source code. It seems that the one-drive isn´t working.