HI WELCOME TO SIRIS

Abstract Factory Design Pattern - C#

Abstract Factory method pattern falls under Creational Pattern of Gang of Four (GOF) Design Patterns in .Net. It is used to create a set of related objects, or dependent objects. Internally, Abstract Factory use Factory design pattern for creating objects. It may also use Builder design pattern and prototype design pattern for creating objects. It completely depends upon your implementation for creating objects. In this article, I would like share what is abstract factory pattern and how is it work?

What is Abstract Factory Pattern?

Abstract Factory patterns acts a super-factory which creates other factories. This pattern is also called as Factory of factories. In Abstract Factory pattern an interface is responsible for creating a set of related objects, or dependent objects without specifying their concrete classes.

Abstract Factory Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the abstract factory design pattern is given below:


The classes, interfaces and objects in the above UML class diagram are as follows:
  1. AbstractFactory

    This is an interface which is used to create abstract product
  2. ConcreteFactory

    This is a class which implements the AbstractFactory interface to create concrete products.
  3. AbstractProduct

    This is an interface which declares a type of product.
  4. ConcreteProduct

    This is a class which implements the AbstractProduct interface to create product.
  5. Client

    This is a class which use AbstractFactory and AbstractProduct interfaces to create a family of related objects.

C# - Implementation Code

  1. public interface AbstractFactory
  2. {
  3. AbstractProductA CreateProductA();
  4. AbstractProductB CreateProductB();
  5. }
  6. public class ConcreteFactoryA : AbstractFactory
  7. {
  8. public AbstractProductA CreateProductA()
  9. {
  10. return new ProductA1();
  11. }
  12. public AbstractProductB CreateProductB()
  13. {
  14. return new ProductB1();
  15. }
  16. }
  17. public class ConcreteFactoryB : AbstractFactory
  18. {
  19. public AbstractProductA CreateProductA()
  20. {
  21. return new ProductA2();
  22. }
  23. public AbstractProductB CreateProductB()
  24. {
  25. return new ProductB2();
  26. }
  27. }
  28. public interface AbstractProductA { }
  29. public class ProductA1 : AbstractProductA { }
  30. public class ProductA2 : AbstractProductA { }
  31. public interface AbstractProductB { }
  32. public class ProductB1 : AbstractProductB { }
  33. public class ProductB2 : AbstractProductB { }
  34.  
  35. public class Client
  36. {
  37. private AbstractProductA _productA;
  38. private AbstractProductB _productB;
  39. public Client(AbstractFactory factory)
  40. {
  41. _productA = factory.CreateProductA();
  42. _productB = factory.CreateProductB();
  43. }
  44. }

Abstract Factory Pattern - Example

 

Who is what?

The classes, interfaces and objects in the above class diagram can be identified as follows:
  1. VehicleFactory - AbstractFactory interface
  2. HondaFactory & HeroFactory- Concrete Factories
  3. Bike & Scooter - AbstractProduct interface
  4. Regular Bike, Sports Bike, Regular Scooter & Scooty - Concreate Products
  5. VehicleClient - Client

C# - Sample Code

  1. /// <summary>
  2. /// The 'AbstractFactory' interface.
  3. /// </summary>
  4. interface VehicleFactory
  5. {
  6. Bike GetBike(string Bike);
  7. Scooter GetScooter(string Scooter);
  8. }
  9.  
  10. /// <summary>
  11. /// The 'ConcreteFactory1' class.
  12. /// </summary>
  13. class HondaFactory : VehicleFactory
  14. {
  15. public Bike GetBike(string Bike)
  16. {
  17. switch (Bike)
  18. {
  19. case "Sports":
  20. return new SportsBike();
  21. case "Regular":
  22. return new RegularBike();
  23. default:
  24. throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Bike));
  25. }
  26.  
  27. }
  28.  
  29. public Scooter GetScooter(string Scooter)
  30. {
  31. switch (Scooter)
  32. {
  33. case "Sports":
  34. return new Scooty();
  35. case "Regular":
  36. return new RegularScooter();
  37. default:
  38. throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Scooter));
  39. }
  40.  
  41. }
  42. }
  43.  
  44. /// <summary>
  45. /// The 'ConcreteFactory2' class.
  46. /// </summary>
  47. class HeroFactory : VehicleFactory
  48. {
  49. public Bike GetBike(string Bike)
  50. {
  51. switch (Bike)
  52. {
  53. case "Sports":
  54. return new SportsBike();
  55. case "Regular":
  56. return new RegularBike();
  57. default:
  58. throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Bike));
  59. }
  60.  
  61. }
  62.  
  63. public Scooter GetScooter(string Scooter)
  64. {
  65. switch (Scooter)
  66. {
  67. case "Sports":
  68. return new Scooty();
  69. case "Regular":
  70. return new RegularScooter();
  71. default:
  72. throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Scooter));
  73. }
  74.  
  75. }
  76. }
  77.  
  78. /// <summary>
  79. /// The 'AbstractProductA' interface
  80. /// </summary>
  81. interface Bike
  82. {
  83. string Name();
  84. }
  85.  
  86. /// <summary>
  87. /// The 'AbstractProductB' interface
  88. /// </summary>
  89. interface Scooter
  90. {
  91. string Name();
  92. }
  93.  
  94. /// <summary>
  95. /// The 'ProductA1' class
  96. /// </summary>
  97. class RegularBike : Bike
  98. {
  99. public string Name()
  100. {
  101. return "Regular Bike- Name";
  102. }
  103. }
  104.  
  105. /// <summary>
  106. /// The 'ProductA2' class
  107. /// </summary>
  108. class SportsBike : Bike
  109. {
  110. public string Name()
  111. {
  112. return "Sports Bike- Name";
  113. }
  114. }
  115.  
  116. /// <summary>
  117. /// The 'ProductB1' class
  118. /// </summary>
  119. class RegularScooter : Scooter
  120. {
  121. public string Name()
  122. {
  123. return "Regular Scooter- Name";
  124. }
  125. }
  126.  
  127. /// <summary>
  128. /// The 'ProductB2' class
  129. /// </summary>
  130. class Scooty : Scooter
  131. {
  132. public string Name()
  133. {
  134. return "Scooty- Name";
  135. }
  136. }
  137.  
  138. /// <summary>
  139. /// The 'Client' class
  140. /// </summary>
  141. class VehicleClient
  142. {
  143. Bike bike;
  144. Scooter scooter;
  145.  
  146. public VehicleClient(VehicleFactory factory, string type)
  147. {
  148. bike = factory.GetBike(type);
  149. scooter = factory.GetScooter(type);
  150. }
  151.  
  152. public string GetBikeName()
  153. {
  154. return bike.Name();
  155. }
  156.  
  157. public string GetScooterName()
  158. {
  159. return scooter.Name();
  160. }
  161.  
  162. }
  163.  
  164. /// <summary>
  165. /// Abstract Factory Pattern Demo
  166. /// </summary>
  167. class Program
  168. {
  169. static void Main(string[] args)
  170. {
  171. VehicleFactory honda = new HondaFactory();
  172. VehicleClient hondaclient = new VehicleClient(honda, "Regular");
  173. Console.WriteLine("******* Honda **********");
  174. Console.WriteLine(hondaclient.GetBikeName());
  175. Console.WriteLine(hondaclient.GetScooterName());
  176. hondaclient = new VehicleClient(honda, "Sports");
  177. Console.WriteLine(hondaclient.GetBikeName());
  178. Console.WriteLine(hondaclient.GetScooterName());
  179. VehicleFactory hero = new HeroFactory();
  180. VehicleClient heroclient = new VehicleClient(hero, "Regular");
  181. Console.WriteLine("******* Hero **********");
  182. Console.WriteLine(heroclient.GetBikeName());
  183. Console.WriteLine(heroclient.GetScooterName());
  184. heroclient = new VehicleClient(hero, "Sports");
  185. Console.WriteLine(heroclient.GetBikeName());
  186. Console.WriteLine(heroclient.GetScooterName());
  187. Console.ReadKey();
  188. }
  189. }

Abstract Factory Pattern Demo - Output

When to use it?

  1. Create a set of related objects, or dependent objects which must be used together.
  2. System should be configured to work with multiple families of products.
  3. The creation of objects should be independent from the utilizing system.
  4. Concrete classes should be decoupled from clients.

Note

  1. Internally, Abstract Factory use Factory design pattern for creating objects. But it can also use Builder design pattern and prototype design pattern for creating objects. It completely depends upon your implementation for creating objects.
  2. Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
  3. When Abstract Factory, Builder, and Prototype define a factory for creating the objects, we should consider the following points :
    1. Abstract Factory use the factory for creating objects of several classes.
    2. Builder use the factory for creating a complex object by using simple objects and a step by step approach.
    3. Prototype use the factory for building a object by copying an existing object.
What do you think?
I hope you will enjoy the Abstract Factory Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.