Monday, March 26, 2018

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

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#


Link to download sample application Abstract Factory Pattern Using C# - Real World Example