Design Pattern - Factory Design Pattern

Introduction

 
In this article, We are going to explore the need of following design patterns in our daily programming routine and one example of a creational design pattern with the help of a Factory pattern.
 

Overview

 
As a developer, we tend to write a code on daily basis.
 
What is the important key while writing any code?
 
The very obvious answer is that we need to follow some practices (SOLID Principles) and coding standards while writing any code.
 
So, let's get started with a few flaws of coding that need to be taken care of for better application performance.
 

Design Flaws

  • Much of the time, we force the classes to depend on each other. This scenario lands into making classes tightly coupled.
  • Some developers, put more stress on the classes by adding more responsibilities to them. Sometimes the functionality is irrelevant to the class where it has been included.
  • In a few scenarios, the same code gets repeated to multiple places. We can say the code duplicity occurs in the system/ application which may reduce your application performance.
In this article, we are going to discuss the Factory design pattern. 
 

What is a Factory Pattern? 

 
The factory pattern is a creational design pattern. It is mainly used when we want to provide more flexibility to our code.
 
Instead of making a direct constructor call using a new keyword, the Factory pattern defines a method. Derived classes can override this method to change the class of the objects that will be created.
 
Here, we conclude that a Factory pattern is one of the creational design pattern, which solves the problem of creating product objects without mentioning their concrete classes.
 
Let's see how we can achieve this with a practical example.
 
Below is the example of creating the Factory design pattern for the Vehicle class:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace FactoryPattern  
  8. {  
  9.     class Program  
  10.     {  
  11.         abstract class Vehicle  
  12.         {  
  13.             public abstract ICar FactoryMethod();  
  14.             public string ConcreteMethod()  
  15.             {  
  16.               
  17.                 var car = FactoryMethod();  
  18.                 var result = "This is concrete method : " + car.DisplayMessage();   
  19.                 return result;  
  20.             }  
  21.         }  
  22.         class BMWCar : Vehicle  
  23.         {  
  24.             public override ICar FactoryMethod()  
  25.             {  
  26.                 return new BMW();  
  27.             }  
  28.         }  
  29.         class AudiCar : Vehicle  
  30.         {  
  31.             public override ICar FactoryMethod()  
  32.             {  
  33.                 return new Audi();  
  34.             }  
  35.         }  
  36.         interface ICar  
  37.         {  
  38.             string DisplayMessage();  
  39.         }  
  40.          
  41.         class BMW : ICar  
  42.         {  
  43.             public string DisplayMessage()  
  44.             {  
  45.                 return "This is BMW Class implementation ";  
  46.             }  
  47.         }  
  48.         class Audi : ICar  
  49.         {  
  50.             public string DisplayMessage()  
  51.             {  
  52.                 return "This is Audi Class implementation ";  
  53.             }  
  54.         }  
  55.         class Client  
  56.         {  
  57.             public void Main()  
  58.             {  
  59.                 Console.WriteLine("Method for BMW.");  
  60.                 ClientCode(new BMWCar());  
  61.   
  62.                 Console.WriteLine("");  
  63.   
  64.                 Console.WriteLine("Method for Audi.");  
  65.                 ClientCode(new AudiCar());  
  66.             }  
  67.               
  68.             public void ClientCode(Vehicle vehicle)  
  69.             {  
  70.                 Console.WriteLine("Client: I'm not aware of the derived class," +  
  71.                     "but it still works.\n" + vehicle.ConcreteMethod());  
  72.             }  
  73.         }  
  74.   
  75.         static void Main(string[] args)  
  76.         {  
  77.             new Client().Main();  
  78.             Console.ReadLine();  
  79.         }  
  80.   
  81.     }  
  82.   
  83.   
  84.   
  85.   
  86.   
  87.   
  88. }  

Let's step into the coding explanation,

Line No. 11
 
The Vehicle class declares the factory method (ConcreteMethod) i.e. supposed to return an object of a car class. The deriving class of the vehicle class usually provides the implementation of this method.
 
Note that the Vehicle may also provide some default implementation of the factory method (ConcreteMethod).

Despite the name, Vehicle's primary responsibility is not creating cars. Mainly, it contains some core business logic that relies on Car objects, returned by the factory method(Concrete Method). Derived subclasses can indirectly change that business logic by overriding the factory method and returning a different type of vehicle object i.e. car from it.

Line No. 17
 
Call the factory method to create a car object. 
 
Line No. 18
 
Use a car object. 
 
Line No. 22 to 35
 
BMWCar and AudiCar override the Concrete Method to create an object of respective car type.

Note that the signature of the method still uses the abstract car type, even though the concrete car is actually returned from the method. This way the BMWCar and AudiCar can stay independent of concrete car classes.

Line No 36
 
Car declares method - DisplayMessage that all concrete cars must implement. 
 
Line No. 42 to 54
 
Concrete cars provide implementation of the Car interface.
 
Line No. 68
 
The client code works with an instance of a concrete Vehicle, through its base interface. As long as the client keeps working with Vehicle via the base interface, you can pass it to any Vehicle’s subclass.
 
After a successful build and execution, you will see the below output screen:
 
 

Summary

 
In the above article, We explored the need for following design principles(SOLID) and patterns in our daily programming routine to avoid design flaws and one of the examples of a creational design pattern with the help of a Factory pattern. I hope you liked the article  Until next time - Happy Reading  Cheers