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
Creates objects without exposing the instantiation logic to the client.
Refers to the newly created object through a common interface
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.
Defines an interface for creating objects, but let subclasses to decide which class to instantiate
Refers the newly created object through a common interface.
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 offers the interface for creating a family of related objects, without explicitly specifying their classes
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
By this post my aim was to help you to explain the differences of these three patterns with a simple diagrammatic example.