I am here to continue the discussion of Design Patterns. Today we will explain another creational design pattern called Simple Factory.
In case you have not had a look at our previous articles, go through the following link:
Before talking about its implementation let's begin with some fundamental questions as in the following.
Purpose of the Factory pattern
I can think of two main objectives of using the Factory pattern. One is to achieve loose coupling between the client and business layer, another is to keep all the code for all the object instantiation logic in one place.
Purpose of loose coupling
In modern software development where changes in existing systems are frequent and software design is expected to be scalable, not having a loosely-coupled design can create many problems.
For example, in an application with a 3-layer architecture, if object creation logic is at the client side, for any new addition of concrete classes, the developer needs to modify not just the business but the client layer as well. Think about the maintainability and added testing effort.
How about if the client is only aware of the high-level contracts and not about its concreate implementation?
Yes, you got it right! The client just must pass the type of the object it needs and it will get it using the Factory Pattern.
Enough theory. Now let's talk about implementation.
How to use the Simple Factory Pattern
Let's try to understand using a simple example.
Assume the client wants to know the on-road price of various brands of cars and have an interface as in the following.
- interface ICar
- {
- string GetOnRoadPrice(string model);
- }
We need to create a Factory class now that will sit between the client and business layers and it will provide the required object to the client based on the car brand passed.
- class CarFactory
- {
- static public ICar GetCar(string carBrand)
- {
- if (carBrand == "Maruti")
- {
- return new Maruti();
- }
- else if (carBrand == "Hyundai")
- {
- return new Hyundai();
- }
- return null;
- }
- }
And here goes the concreate business classes.
- class Maruti: ICar
- {
- public string GetOnRoadPrice(string model)
- {
- if (model == "Alto 800 VXI")
- {
- return "3.4 Lakhs INR";
- }
- else
- {
- return "Information not available!";
- }
- }
- }
- class Hyundai: ICar
- {
- public string GetOnRoadPrice(string model)
- {
- if (model == "Grand i10 Magna 1.2 BSV")
- {
- return "5.4 Lakhs INR";
- }
- else
- {
- return "Information not available!";
- }
- }
- }
Now let's see how the client can use the setup we have created so far.
- //Client side
- Console.Title = "Factory pattern demo";
- ICar car = null;
- string model = null;
- //Maruti
- car = CarFactory.GetCar("Maruti");
- model = "Alto 800 VXI";
- Console.WriteLine("On-road price for {0} car is: {1} ", model, car.GetOnRoadPrice(model));
- //Hyundai
- car = CarFactory.GetCar("Hyundai");
- model = "Grand i10 Magna 1.2 BSV";
- Console.WriteLine("On-road price for {0} car is: {1} ", model, car.GetOnRoadPrice(model));
- Loose coupling between client and business layers.
- Placed object creation logic at common place.
- Abstracted concreate classes (Maruti, Hyundai) from client.
I hope you have liked this article. I look forward to your comments/suggestions.

Khaja MoizuddinPosted Apr 23, 2020, 12:37 AM
Nice explanation Prakash Sir, with simple example.
Prakash TripathiPosted Apr 11, 2016, 3:42 AM
Thnx Humayun.
Humayun Kabir MamunPosted Apr 11, 2016, 3:38 AM
Nice...
Prakash TripathiPosted Apr 9, 2016, 10:39 AM
Thnx Nagaraj.
Kuppurasu NagarajPosted Apr 9, 2016, 10:35 AM
Nice article..
Prakash TripathiPosted Mar 11, 2016, 5:05 AM
Thnx Amit.
Amit Kumar SinghPosted Mar 11, 2016, 4:50 AM
Nice Share
Prakash TripathiPosted Mar 6, 2016, 10:47 PM
Thnx Kumaresh.
Prakash TripathiPosted Mar 6, 2016, 10:46 PM
Thnx Karthiga.
Prakash TripathiPosted Mar 6, 2016, 10:45 PM
Thnx Ankur.
Prakash TripathiPosted Mar 6, 2016, 10:45 PM
Thnx Humayun.
Kumaresh RajalingamPosted Mar 6, 2016, 8:27 PM
good one
Kumaresh RajalingamPosted Mar 6, 2016, 8:27 PM
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 8:16 PM
good one
Sr KarthigaPosted Mar 6, 2016, 8:16 PM
Nice explanation
Prakash TripathiPosted Dec 30, 2015, 12:51 PM
Thnx Rupesh.
Rupesh KahanePosted Dec 30, 2015, 12:40 PM
good one
Ankur MistryPosted Dec 30, 2015, 9:01 AM
nice explained
Humayun Kabir MamunPosted Dec 14, 2015, 1:25 AM
Nice...
Prakash TripathiPosted Dec 14, 2015, 12:45 AM
Thanks all for liking the article.
Santhakumar MunuswamyPosted Aug 18, 2015, 2:41 PM
Good one
Jaipal ReddyPosted Aug 17, 2015, 7:16 AM
good one
Nilesh JadavPosted Aug 17, 2015, 4:22 AM
Great Article sir
Sibeesh VenuPosted Aug 17, 2015, 4:11 AM
Nice Share. Thank you.
Ankit BansalPosted Aug 17, 2015, 1:25 AM
thanks for share...
Vaikesh K PPosted Aug 17, 2015, 12:35 AM
Nice work
Gopi ChandPosted Aug 16, 2015, 11:33 PM
Good work
Mohammed IbrahimPosted Aug 16, 2015, 11:27 PM
nice
Karthikeyan KPosted Aug 16, 2015, 11:19 PM
Good one...