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:

Implementation

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

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#