An Overview Of Factory Design Pattern With Real Life Example

When we create any software on broad level then we need to use some design pattern. Today, I am going to demonstrate about Factory Design Pattern.

Factory Design Pattern is a part of creational pattern. Using factory design pattern, we can create instance of the class without exposing the logic to outer world. Sometimes it is required to create the instance of the class on the runtime and then we use factory design pattern. It is used to create decoupled system.

As name suggested “Factory”, it is used to create object of the required class when needed.

Real Time Example

Here I am going to take a real life  example to demonstrate it very clearly. Let suppose there is company which manufactures two types of car, First one is “Suzuki” and other one is “Honda”.

I need to know the details of each car based on their name. I will only pass the name and it will give us all the required details of the particular car, so it can be achieved to create two simple classes.

cms

The following code is for implementing the Factory Design Pattern:

  1. using System;  
  2. namespace FactoryDesignPatternDemo  
  3. {  
  4.     public interface ICar  
  5.     {  
  6.         void GetCarDetails();  
  7.     }  
  8.     public class SuzukiCar: ICar  
  9.     {  
  10.         public void GetCarDetails()  
  11.         {  
  12.             Console.WriteLine("This is Suzuki Car");  
  13.         }  
  14.     }  
  15.     class Program  
  16.     {  
  17.         static void Main(string[] args)  
  18.         {  
  19.             ICar mycar = new SuzukiCar();  
  20.             mycar.GetCarDetails();  
  21.             Console.ReadLine();  
  22.         }  
  23.     }  
  24. }  
But there is a problem, when company launch a new car, we need to modify the code and add new car as well as the logic to get details of the car. Let say 100 cars added in the list, so we need to write the login for 100 cars in the code.

When we use Factory Design Pattern, there is no worry about the logic, we need to write a new class for new car and add logic to create the instance of the class.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace FactoryDesignPatternDemo  
  7. {  
  8.     public interface ICar  
  9.     {  
  10.         string CompanyName  
  11.         {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         string ModelNumber  
  16.         {  
  17.             get;  
  18.             set;  
  19.         }  
  20.         int Speed  
  21.         {  
  22.             get;  
  23.             set;  
  24.         }  
  25.     }  
  26.     class SuzukiCar: ICar  
  27.     {  
  28.         private string _companyName;  
  29.         private string _modelNumber;  
  30.         private int _speed;  
  31.         public SuzukiCar(string modelNumber, int speed)  
  32.         {  
  33.             _companyName = "Suzuki";  
  34.             _modelNumber = modelNumber;  
  35.             _speed = speed;  
  36.         }  
  37.         public string CompanyName  
  38.         {  
  39.             get  
  40.             {  
  41.                 return _companyName;  
  42.             }  
  43.             set  
  44.             {  
  45.                 _companyName = value;  
  46.             }  
  47.         }  
  48.         public string ModelNumber  
  49.         {  
  50.             get  
  51.             {  
  52.                 return _modelNumber;  
  53.             }  
  54.             set  
  55.             {  
  56.                 _modelNumber = value;  
  57.             }  
  58.         }  
  59.         public int Speed  
  60.         {  
  61.             get  
  62.             {  
  63.                 return _speed;  
  64.             }  
  65.             set  
  66.             {  
  67.                 _speed = value;  
  68.             }  
  69.         }  
  70.     }  
  71.     class HondaCar: ICar  
  72.     {  
  73.         private string _companyName;  
  74.         private string _modelNumber;  
  75.         private int _speed;  
  76.         public HondaCar(string modelNumber, int speed)  
  77.         {  
  78.             _companyName = "Honda";  
  79.             _modelNumber = modelNumber;  
  80.             _speed = speed;  
  81.         }  
  82.         public string CompanyName  
  83.         {  
  84.             get  
  85.             {  
  86.                 return _companyName;  
  87.             }  
  88.             set  
  89.             {  
  90.                 _companyName = value;  
  91.             }  
  92.         }  
  93.         public string ModelNumber  
  94.         {  
  95.             get  
  96.             {  
  97.                 return _modelNumber;  
  98.             }  
  99.             set  
  100.             {  
  101.                 _modelNumber = value;  
  102.             }  
  103.         }  
  104.         public int Speed  
  105.         {  
  106.             get  
  107.             {  
  108.                 return _speed;  
  109.             }  
  110.             set  
  111.             {  
  112.                 _speed = value;  
  113.             }  
  114.         }  
  115.     }  
  116.     public abstract class CarFactory  
  117.     {  
  118.         public abstract ICar GetCarDetails();  
  119.     }  
  120.     public class SuzukiCarFactory: CarFactory  
  121.     {  
  122.         private string _modelNumber;  
  123.         private int _speed;  
  124.         public SuzukiCarFactory(string modelNumber, int speed)  
  125.         {  
  126.             _modelNumber = modelNumber;  
  127.             _speed = speed;  
  128.         }  
  129.         public override ICar GetCarDetails()  
  130.         {  
  131.             return new SuzukiCar(_modelNumber, _speed);  
  132.         }  
  133.     }  
  134.     public class HondaCarFactory: CarFactory  
  135.     {  
  136.         private string _modelNumber;  
  137.         private int _speed;  
  138.         public HondaCarFactory(string modelNumber, int speed)  
  139.         {  
  140.             _modelNumber = modelNumber;  
  141.             _speed = speed;  
  142.         }  
  143.         public override ICar GetCarDetails()  
  144.         {  
  145.             return new HondaCar(_modelNumber, _speed);  
  146.         }  
  147.     }  
  148.     class Program  
  149.     {  
  150.         static void Main(string[] args)  
  151.         {  
  152.             CarFactory carFactory = null;  
  153.             Console.WriteLine("Enter your car name : ");  
  154.             string carName = Console.ReadLine();  
  155.             if (carName == "Suzuki")  
  156.             {  
  157.                 carFactory = new SuzukiCarFactory("9002", 45);  
  158.             }  
  159.             else if (carName == "Honda")  
  160.             {  
  161.                 carFactory = new HondaCarFactory("1001", 60);  
  162.             }  
  163.             else  
  164.             {  
  165.                 Console.WriteLine("Wrong Choice ! ");  
  166.                 Console.ReadLine();  
  167.             }  
  168.             if (carFactory != null)  
  169.             {  
  170.                 var mycar = carFactory.GetCarDetails();  
  171.                 Console.WriteLine("Company Name is : " + mycar.CompanyName);  
  172.                 Console.WriteLine("Model Number is : " + mycar.ModelNumber);  
  173.                 Console.WriteLine("Speed is : " + mycar.Speed);  
  174.                 Console.ReadLine();  
  175.             }  
  176.         }  
  177.     }  
  178. }  
Let's suppose company  added a new car “BMW” in the list so how can we add the logic in the code. For this to create the instance of the class, we only need to add the logic here.
  1. else if (carName == "BMW")  
  2. {  
  3.     carFactory = new BMWCarFactory("11001", 160);  
  4. }  
Thanks for reading this article, hope you enjoyed it. 

Up Next
    Ebook Download
    View all
    Learn
    View all