In this article, we will learn what Factory Design Pattern is and why we use Factory Design Pattern, with a real world example. We will see what type of problems are resolved using Factory Design Pattern.
Now-a-days, if we create a software, it gets outdated soon because of the requirement changes in future. So, that time, we need to write the whole logic again from scratch. The main concern when we work with Software Architecture is how to create object of entity and pass that to some other object without depending on others. One thing we need to consider is architecture should be pluggable so that in the future more objects can be added.

What Factory Design Pattern Is?
The Factory Design Pattern is a commonly used design pattern where we need to create Loosely Coupled System. Basically, it comes under Creational Pattern and it is used to create instance and reuse it. Factory Pattern is based on real time factory concept. As we know, a factory is used to manufacture something as per the requirement and if new items are added in the manufacturing process, the factory starts manufacturing those items as well. Factory class provides abstraction between Client and Car when creating the instance of the Car [Honda, BMW etc].
When to use Factory Design Pattern?
It is used for creating objects to encapsulate the instantiation logic. Client doesn’t know the actual instantiation logic of entity.
Problem
See the following example of code, where we have created two different classes as Honda and BMW. Those classes are implementing the ICarSupplier interface which has one property as CarColor and one method which provides the Car Model.
On the client side, we are simply creating the objects of two classes to get their member function and behavior.
using System;
namespace FactoryDesignPattern
{
public interface ICarSupplier
{
string CarColor { get; }
void GetCarModel();
}
class Honda : ICarSupplier
{
public string CarColor
{
get { return "RED"; }
}
public void GetCarModel()
{
Console.WriteLine("Honda Car Model is Honda 2014");
}
}
class BMW : ICarSupplier
{
public string CarColor
{
get { return "WHITE"; }
}
public void GetCarModel()
{
Console.WriteLine("BMW Car Model is BMW 2000");
}
}
class ClientProgram
{
static void Main(string[] args)
{
Honda objHonda = new Honda();
objHonda.GetCarModel();
BMW objBMW = new BMW();
objBMW.GetCarModel();
Console.ReadLine();
}
}
}
But what is the problem with this code? Just think, if in the future, a new Car [Nano] is introduced and the client has to create an instance of that class to access all the property and member function, they need to modify the object creation logic and add the following code.
Nano is a new class which also implements ICarSupplier.
class Nano : ICarSupplier
{
public string CarColor
{
get { return "YELLOW"; }
}
public void GetCarModel()
{
Console.WriteLine("Nano Car Model is Nano 2016");
}
}
Instantiation of Nano class.
Nano objNano = new Nano();
objNano.GetCarModel();
We can do that if we add new class, we need to modify the instantiation logic at client, as above code shown. The biggest problem is that we don't know how many entities are going to add in future. If new car is added, then we need to write the logic at client end to access that class properties and methods. So, we need to add a Factory which will give you the instance at runtime.
Solution
So, in Factory Design Pattern, there we will add a Factory class where we can add a method which will return the instance of the class based on your requirement. We can see with the following code where GetCarInstance method takes one argument as Id.
On the basis of the Id, it will return the instance of the Car. As per example, if client passes 0, then it will return the instance of the Honda Car, if they pass 1, then it will return the instance of BMW car.
static class CarFactory
{
public static ICarSupplier GetCarInstance(int Id)
{
switch (Id)
{
case 0:
return new Honda();
case 1:
return new BMW();
case 2:
return new Nano();
default:
return null;
}
}
}
In future, if a new car is launched e.g. Suzuki, there is no need to add anything on the client side. You just need to add one case for Suzuki to get the instance.
Case. 3
return new Suzuki();
And, just pass the id=3 when creating the object of CarFactory class. Just notice with following code, we are passing 3 inside the GetCarInstance and as we have defined in CarFactory, it will return the instance of the Suzuki Car.
ICarSupplier objCarSupplier = CarFactory.GetCarInstance(3);
objCarSupplier.GetCarModel();
Console.WriteLine("And Color is " + objCarSupplier.CarColor);
Following is the whole code for demonstration of Factory Design Pattern which will give us an idea of how and where we should implement Factory Design Pattern in Real Life.
using System;
namespace FactoryDesignPattern
{
public interface ICarSupplier
{
string CarColor { get; }
void GetCarModel();
}
class Honda : ICarSupplier
{
public string CarColor
{
get { return "RED"; }
}
public void GetCarModel()
{
Console.WriteLine("Honda Car Model is Honda 2014");
}
}
class BMW : ICarSupplier
{
public string CarColor
{
get { return "WHITE"; }
}
public void GetCarModel()
{
Console.WriteLine("BMW Car Model is BMW 2000");
}
}
class Nano : ICarSupplier
{
public string CarColor
{
get { return "YELLOW"; }
}
public void GetCarModel()
{
Console.WriteLine("Nano Car Model is Nano 2016");
}
}
class Suzuki : ICarSupplier
{
public string CarColor
{
get { return "Orange"; }
}
public void GetCarModel()
{
Console.WriteLine("Suzuki Car Model is Suzuki 2006");
}
}
static class CarFactory
{
public static ICarSupplier GetCarInstance(int Id)
{
switch (Id)
{
case 0:
return new Honda();
case 1:
return new BMW();
case 2:
return new Nano();
case 3:
return new Suzuki();
default:
return null;
}
}
}
class ClientProgram
{
static void Main(string[] args)
{
ICarSupplier objCarSupplier = CarFactory.GetCarInstance(3);
objCarSupplier.GetCarModel();
Console.WriteLine("And Color is " + objCarSupplier.CarColor);
Console.ReadLine();
}
}
}
Conclusion
So, today we learned what Factory Design Pattern is and why and where it can be used, with real world examples.
I hope this post will help you. Please put your feedback in the comment box which helps me improve myself for my next posts.

Shiv Ratan KumarPosted Jun 6, 2019, 6:41 AM
I am not getting why you use Switch Case Statement. If we want to add one more car then again need to add in CarFactory Class its Violating Open Closed Principle. Better you can create HonaFactory, BMWFactory etc.
dimpal patelPosted Jun 5, 2019, 5:50 AM
Yes, I agreed with u sir but i have one another doubt is that We can also the same decouple code do with dependency Injection. So what the benefit of the using factory pattern and what the difference, I already read differences on many sites but still i have doubt. Thank You. I hope my question are make sense.
Aadil SyedPosted Mar 30, 2019, 3:01 AM
Nice and simple way to understand the Factory design pattern . Thanks
bharath reddyPosted Mar 24, 2019, 4:20 AM
As per https://www.dofactory.com/net/factory-method-design-pattern ,I have a doubt please clarify.Which one will represent the concretecreator in your example
Dinesh KumarPosted Mar 2, 2018, 10:46 AM
Yes, Arjun Singh we can do such
sudheer babuPosted Dec 12, 2017, 10:48 PM
Do you have any demo project in this article. Please share it for more reference.
sudheer babuPosted Dec 12, 2017, 10:47 PM
Hi Mukesh, Nice article ,i got more idea from your side. thank you so much
Renjith JPosted Dec 1, 2017, 3:55 PM
Great and simple explanation...
Arjun SinghPosted Dec 14, 2016, 11:59 PM
Is there any possibility that we can add more car classes as a separate DLL and without changing any thing in Factory class return the object as per the supplied argument or type from the client?
Anu VPosted Oct 6, 2016, 2:24 AM
Nice..
zubair pathanPosted Sep 28, 2016, 8:06 AM
Nice Article Thank You !!!
Humayun Kabir MamunPosted Sep 18, 2016, 12:54 AM
Nice...
Guest UserPosted Sep 15, 2016, 11:55 AM
Good article
Vikram ChaudharyPosted Sep 14, 2016, 5:59 AM
Hi Mukesh. Nice article. Though I have a doubt here. Every time we have to add a new model, we have to do the changes to the Car Factory. Is there any better way to handle it?
SubashPosted Sep 14, 2016, 12:14 AM
Nice one sir thank you