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