Abstract Factory Pattern Using C# - Real World Example

While learning about design patterns, I came to understand the most frequently used term, Factory Pattern as well as Abstract factory pattern. I searched the internet and came across numerous learning points. After a lot of search and study, I endeavored to find an actual need for the abstract design pattern.

In this article, I will explore this pattern in an easy way so that everyone can have a better understanding of it. Down the level, I'm using a Car (Vehicle) example to demonstrate abstract factory pattern.

An important aspect of software design is the manner in which objects are created. Thus, it is not only important what an object does or what it models, but also in what manner it was created.

Reference: http://www.dofactory.com/

Definition

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Definition 

C#

I'd keep the structure in such a way as depicted below.

Participants

The classes and objects participating in this pattern are:

  • AbstractFactory (IVehicleFactory)
    • declares an interface for operations that create abstract products.
  • ConcreteFactory (MarutiFactory, TataFactory)
    • implements the operations to create concrete product objects.
  • AbstractProduct (ICarEngine, ICarLight)
    • declares an interface for a type of product object.
  • Product (DDis, LED, Revtron, Helogan)
    • defines a product object to be created by the corresponding concrete factory.
    • implements the AbstractProduct interface.
  • Client (Clinet)
    • uses interfaces declared by AbstractFactory and AbstractProduct classes.

Implementation

  1. using System;  
  2.    
  3.    
  4. namespace AbstractFactoryPattern  
  5. {  
  6.     interface IVehicleFactory  
  7.     {  
  8.         ICarEngine GetCarEngine();  
  9.         ICarLight GetCarLight();  
  10.     }  
  11.    
  12.    
  13.     class MarutiFactory : IVehicleFactory  
  14.     {  
  15.         public ICarEngine GetCarEngine()  
  16.         {  
  17.             return new DDiS();  
  18.         }  
  19.    
  20.         public ICarLight GetCarLight()  
  21.         {  
  22.             return new LED();  
  23.         }  
  24.     }  
  25.    
  26.     class TataFactory : IVehicleFactory  
  27.     {  
  28.         public ICarEngine GetCarEngine()  
  29.         {  
  30.             return new Revtron();  
  31.         }  
  32.    
  33.         public ICarLight GetCarLight()  
  34.         {  
  35.             return new Helogen();  
  36.         }  
  37.     }  
  38.    
  39.     class LED : ICarLight  
  40.     {  
  41.         public string GetLightInfo()  
  42.         {  
  43.             return "Led lights";  
  44.         }  
  45.     }  
  46.    
  47.     class DDiS : ICarEngine  
  48.     {  
  49.         public string GetEngineInfo()  
  50.         {  
  51.             return "DDis engine for their diesal cars...";  
  52.         }  
  53.     }  
  54.    
  55.    
  56.    
  57.     internal class Helogen : ICarLight  
  58.     {  
  59.         public string GetLightInfo()  
  60.         {  
  61.             return "Helogan Light...";  
  62.         }  
  63.     }  
  64.    
  65.     internal class Revtron : ICarEngine  
  66.     {  
  67.         public string GetEngineInfo()  
  68.         {  
  69.             return "Revtron engine for their diesal/petrol cars...";  
  70.         }  
  71.     }  
  72.    
  73.     internal interface ICarLight  
  74.     {  
  75.         string GetLightInfo();  
  76.     }  
  77.    
  78.     internal interface ICarEngine  
  79.     {  
  80.         string GetEngineInfo();  
  81.     }  
  82.    
  83.    
  84.     class Client  
  85.     {  
  86.         private IVehicleFactory vehicleFactory = null;  
  87.         public void CreateCarWithLight(string carName)  
  88.         {  
  89.             if (carName.ToLower() == "maruti")  
  90.             {  
  91.                 vehicleFactory = new MarutiFactory();  
  92.                 Console.Write(  
  93.                     $"{carName} uses {vehicleFactory.GetCarEngine().GetEngineInfo()} with {vehicleFactory.GetCarLight().GetLightInfo()} as headlight");  
  94.             }  
  95.             else if (carName.ToLower() == "tata")  
  96.             {  
  97.                 vehicleFactory = new TataFactory();  
  98.                 Console.Write(  
  99.                     $"{carName} uses {vehicleFactory.GetCarEngine().GetEngineInfo()} with {vehicleFactory.GetCarLight().GetLightInfo()} as headlight");  
  100.             }  
  101.         }  
  102.     }  
  103.    
  104.    
  105.     class Program  
  106.     {  
  107.         static void Main(string[] args)  
  108.         {  
  109.             Console.Write("**** Welcome to Abstract Factory pattern By DotnetPiper.com *******\n Kinldy enter your car name...");  
  110.             Client client = new Client();  
  111.             string carName = Console.ReadLine();  
  112.             client.CreateCarWithLight(carName);  
  113.             Console.ReadLine();  
  114.         }  
  115.     }  

Run your application now. The following window will appear.

C#

Put the desired car name you would like to know the information about, like Maruti, Tata etc.

Kindly refer to the following window as a result after you put Maruti.

C#

Up Next
    Ebook Download
    View all
    Learn
    View all