Introduction
Before reading this article, please go through the following article.
- Design Pattern For Beginners - Part 1: Singleton Design Pattern
- Design Pattern For Beginners - Part 2: Factory Design Pattern
- Design Pattern For Beginners - Part 3: Prototype Design Pattern
- Design Pattern For Beginners - Part 4: Decorator Design Pattern
- Design Pattern For Beginners - Part 5: Composite Design Pattern
- Design Pattern For Beginners - Part 6: Adaptor Design Pattern
- Design Pattern For Beginners - Part 7: Bridge Design Pattern
- Design Pattern For Beginners - Part 8: memento Design Pattern
- Design Pattern for Beginners - Part-9: Strategy Design Pattern
- Design Pattern for Beginners - Part-10: Observer Design Pattern
- Design Pattern For Beginners - Part 11: Implement Decouple Classes in Application
Before writing this article, I searched using Google for the keywords "factory design pattern," As expected, I found many good articles with excellent examples. Then I started to think, "Why to write one more?" OK, then I thought, let's check each of them individually. I visited each of the top 10 (from the first page). They are tremendous but stuffy enough; all the articles maintain a typical style and are the same type. Somehow reality is absent.
And that led me to write one more on the same topic. Here we will try to understand that topic with realistic examples and a little fun. And we will enjoy the learning.
So, let's start with the Factory Design Pattern. As promised, "We will try to understand the basic need first before starting any design pattern.".
Let me start with a small practical event that I experienced just a few days before (yes, just a few days). My present office is within one Tech Park and I see many ITians (yes, you are right, they work in various IT fields in many companies within the same tech park) going towards the food court at lunchtime. Of course, I am one of them. There is a long staircase in the middle of the food court (since the food court is underground).
One fine lunchtime, I was going for lunch as usual and heard a beautiful romantic song of love from someone's voice. I observed that a disabled fellow was singing and going just before me using his crutch (you know, walking stick). How happy is he? (Dear reader, I am not making fun of his physical disability. I mean to say, see how happy he is. However, sometimes we may not be a complete person.) I was thinking of being a physical challenger.
OK, now the staircase has come, and it's time to climb. I noticed that the guy (singing a song of love) just folded up his walking stick and, by holding the stair railing, began to climb down. I was thinking about how nice the stick is. Depending on demand, it's behaving.
My story ends here, and the walking stick is just one example of a factory class. A factory class serves the client's demands, and depending on the requirements, it supplies the proper form depending on the requirements.
OK, we have talked too much; let's learn the simple structure of the Factory Design Pattern. We will initially see the logical stricture of the Factory Design Pattern.

As the name implies, a factory is where manufacturing happens—for example, a car factory where many types of car manufacturing happen. Now you want a specific model, and it's always possible to produce it from this car factory. Again your friend's taste is different from yours. He wants a different model; you can request a car factory again to make your friend smile.
OK, one thing is clear from our discussion; the factory class is a supplier class that makes the client happy by supplying their demand.
How to will you implement it in C# code? OK, let's see the following example.
using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
namespace Test1
{
public interface ISupplier
{
void CarSupplier();
}
class namo : ISupplier
{
public void CarSupplier()
{
Console.WriteLine("I am nano supplier");
}
}
class alto : ISupplier
{
public void CarSupplier()
{
Console.WriteLine("I am Alto supplier");
}
}
class CarFactory
{
public static ISupplier GiveMyCar(int Key)
{
if (Key == 0)
return new namo();
else if (Key == 1)
return new alto();
else
return null;
}
}
class Program
{
static void Main(string[] args)
{
ISupplier obj = CarFactory.GiveMyCar(0);
obj.CarSupplier();
obj = CarFactory.GiveMyCar(1);
obj.CarSupplier();
Console.ReadLine();
}
}
}
And here is the output.

Though the code is pretty simple to understand, we will discuss it further. At first, we created one interface and implemented that interface within two classes. The classes are nano and alto.
In addition to them, there is one more class called CarFactory, and it is the story's hero. If we observe closely inside the CarFactory class, we will find the mechanism to create a car. Though here we are producing two low-end cars (nano and alto), we can soon add many more models. Now here is the original beauty of the factory class.


Khaja MoizuddinPosted Mar 2, 2019, 7:23 AM
Hai Sourav i am little confused here, is this article belongs to Factory Method Design Pattern or Abstract Factory Design Pattern, i am new to Design Pattern Concept, can you please revert me back with an answer.
Vadim KobaPosted Nov 9, 2014, 12:39 PM
Really simple and handy!!
Anik SenPosted Jun 10, 2014, 8:35 AM
Thanks, you have changed my point of view :)
sparkle daiPosted Aug 28, 2013, 8:58 PM
GOOD