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();

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