Factory Design Pattern Real World Example

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.


object

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.

  1. using System;  
  2.   
  3. namespace FactoryDesignPattern  
  4. {  
  5.     public interface ICarSupplier  
  6.     {  
  7.         string CarColor  
  8.         {  
  9.             get;  
  10.         }  
  11.         void GetCarModel();  
  12.     }  
  13.     class Honda : ICarSupplier  
  14.     {  
  15.         public string CarColor  
  16.         {  
  17.             get { return "RED"; }  
  18.         }  
  19.   
  20.         public void GetCarModel()  
  21.         {  
  22.             Console.WriteLine("Honda Car Model is Honda 2014");  
  23.         }  
  24.     }  
  25.     class BMW : ICarSupplier  
  26.     {  
  27.         public string CarColor  
  28.         {  
  29.             get { return "WHITE"; }  
  30.         }  
  31.         public void GetCarModel()  
  32.         {  
  33.             Console.WriteLine("BMW Car Model is BMW 2000");  
  34.         }  
  35.     }      
  36.   
  37.     class ClientProgram  
  38.     {  
  39.         static void Main(string[] args)  
  40.         {  
  41.             Honda objHonda = new Honda();  
  42.             objHonda.GetCarModel();  
  43.   
  44.             BMW objBMW = new BMW();  
  45.             objBMW.GetCarModel();           
  46.   
  47.             Console.ReadLine();  
  48.         }  
  49.     }  
  50. }  
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.
  1. class Nano : ICarSupplier  
  2. {  
  3.         public string CarColor  
  4.         {  
  5.             get { return "YELLOW"; }  
  6.         }  
  7.         public void GetCarModel()  
  8.         {  
  9.             Console.WriteLine("Nano Car Model is Nano 2016");  
  10.         }  
  11. }  
Instantiation of Nano class.
  1. Nano objNano = new Nano();  
  2. 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.
  1. static class CarFactory  
  2.  {  
  3.         public static ICarSupplier GetCarInstance(int Id)  
  4.         {  
  5.             switch (Id)  
  6.             {  
  7.                 case 0:  
  8.                     return new Honda();  
  9.                 case 1:  
  10.                     return new BMW();  
  11.                 case 2:  
  12.                     return new Nano();  
  13.                 default:  
  14.                     return null;  
  15.             }  
  16.         }  
  17.  }  
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
  1. 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.
  1. ICarSupplier objCarSupplier = CarFactory.GetCarInstance(3);  
  2. objCarSupplier.GetCarModel();  
  3. Console.WriteLine("And Coloar 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.
  1. using System;  
  2.   
  3. namespace FactoryDesignPattern  
  4. {  
  5.     public interface ICarSupplier  
  6.     {  
  7.         string CarColor  
  8.         {  
  9.             get;  
  10.         }  
  11.         void GetCarModel();  
  12.     }  
  13.     class Honda : ICarSupplier  
  14.     {  
  15.         public string CarColor  
  16.         {  
  17.             get { return "RED"; }  
  18.         }  
  19.   
  20.         public void GetCarModel()  
  21.         {  
  22.             Console.WriteLine("Honda Car Model is Honda 2014");  
  23.         }  
  24.     }  
  25.     class BMW : ICarSupplier  
  26.     {  
  27.         public string CarColor  
  28.         {  
  29.             get { return "WHITE"; }  
  30.         }  
  31.         public void GetCarModel()  
  32.         {  
  33.             Console.WriteLine("BMW Car Model is BMW 2000");  
  34.         }  
  35.     }  
  36.   
  37.     class Nano : ICarSupplier  
  38.     {  
  39.         public string CarColor  
  40.         {  
  41.             get { return "YELLOW"; }  
  42.         }  
  43.         public void GetCarModel()  
  44.         {  
  45.             Console.WriteLine("Nano Car Model is Nano 2016");  
  46.         }  
  47.     }  
  48.     class Suzuki : ICarSupplier  
  49.     {  
  50.         public string CarColor  
  51.         {  
  52.             get { return "Orange"; }  
  53.         }  
  54.         public void GetCarModel()  
  55.         {  
  56.             Console.WriteLine("Suzuki Car Model is Suzuki 2006");  
  57.         }  
  58.     }  
  59.   
  60.     static class CarFactory  
  61.     {  
  62.         public static ICarSupplier GetCarInstance(int Id)  
  63.         {  
  64.             switch (Id)  
  65.             {  
  66.                 case 0:  
  67.                     return new Honda();  
  68.                 case 1:  
  69.                     return new BMW();  
  70.                 case 2:  
  71.                     return new Nano();  
  72.                 case 3:  
  73.                     return new Suzuki();  
  74.                 default:  
  75.                     return null;  
  76.             }  
  77.         }  
  78.     }  
  79.     class ClientProgram  
  80.     {  
  81.         static void Main(string[] args)  
  82.         {  
  83.               
  84.             ICarSupplier objCarSupplier = CarFactory.GetCarInstance(3);  
  85.             objCarSupplier.GetCarModel();  
  86.             Console.WriteLine("And Coloar is " + objCarSupplier.CarColor);  
  87.   
  88.             Console.ReadLine();  
  89.         }  
  90.   
  91.     }  
  92. }  
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. 

Up Next
    Ebook Download
    View all
    Learn
    View all