Abstract Factory Method Design Pattern With .NET Core


In Software Development, design patterns are used to solve common architectural problems when designing an application or system. They must be used carefully and in the right situations for the right reasons.
If correctly used, design patterns may save developers a lot of time because they offer simple algorithms in order to solve complex problems. But, if not correctly used, they can bring a huge boost of complexity to your project, increasing the difficulty to solve the problem. It is crucial to understand design patterns and their behavior to use their biggest advantages.
Design patterns are organized into three main groups, as follows:
  1. Creational, focused to solve problems regarding objects creation mechanisms.
  2. Structural, creating a stable structure by design the relationship between entities.
  3. Behavioral, improving objects behavior applying patterns on how they should communicate.

Benefits of using Design Patterns

  • Increased maintainability with standardized algorithms
  • Easy to apply, solving complex problems with best-practice solutions
  • More efficiency, do more with less code.
Related Articles
Check my previous articles about design patterns with .NET Core:

What is the Abstract Factory Method Design Pattern?

The Abstract Factory Design Pattern gives us a way to encapsulate the creation of a group of common objects without exposing the type of objects that will be returned. In short, the abstract factory design pattern is a factory of factories in which the client selects the factory that would best fit his needs.
With the Abstract Factory Design Pattern, the concrete group of classes returned is completely abstracted for the client, where the client's only concern is to select the factory and call its methods to return their specific objects.
In our practical example, the client is going to request vehicles for a tourism group that is going to travel by land and sea. So, the client selects the group size, large or small, and the picks the vehicles for land and sea where the factory will return specific objects for each of their options.

Why use the Abstract Factory Method Design Pattern?

The main benefit here is when using a group, or family, of common objects that going to be used in the same context. So you only need to instantiate this specific factory in a single place and call the other methods from the factory superclass and manipulate the objects created in the same way.
Saying that we have, among others, these benefits:
  • The client knows which group of objects solves his needs;
  • Each object factory may have his own algorithms to apply while creating the object;
  • One factory does not need to know the other;

Implementation Step-by-Step With .NET Core

Use Case
In this example, we are going to have a C# with a .NET Core Console Application responsible to select the groups of vehicles( land and sea) according to the user group size.
The abstract factory will hold the logic to pick the vehicle that suits best the group according to the group size.

The Vehicle class that will be used as a base for the Car, Boat, Cruise and Bus classes
  1. public abstract class Vehicle  
  2. {  
  3.     internal int capacity;  
  4.     public string GetData()  
  5.     {  
  6.         return this.GetType().Name;  
  7.     }  
  8.     public int GetCapacity()  
  9.     {  
  10.         return capacity;  
  11.     }  
  12. }    
Car, boat, cruise, and bus classes that inherit from the vehicle class. 
  1. class Car : Vehicle  
  2. {  
  3.     public Car()  
  4.     {  
  5.         this.capacity = 5;  
  6.     }  
  7. }  
  8. class Bus :Vehicle  
  9. {  
  10.     public Bus()  
  11.     {  
  12.         this.capacity = 60;  
  13.     }  
  14. }  
  15. class Boat :Vehicle  
  16. {  
  17.     public Boat()  
  18.     {  
  19.         this.capacity = 50;  
  20.     }  
  21. }  
  22. class Cruise : Vehicle  
  23. {  
  24.     public Cruise()  
  25.     {  
  26.         this.capacity = 250;  
  27.     }  
  28. }  
Factory Superclass
The class that will be inherited from the Large Group and Small Group factory classes. 
  1. abstract class AbstractFactory  
  2. {  
  3.     public abstract Vehicle CreateLandVehicle();  
  4.     public abstract Vehicle CreateSeaVehicle();  
  5. }  
Large Group and Small Group factory classes
The factory class responsible to create his own objects. 
  1. class LargeGroupFactory : AbstractFactory  
  2. {  
  3.     public override Vehicle CreateLandVehicle()  
  4.     {  
  5.         return new Bus();  
  6.     }  
  8.     public override Vehicle CreateSeaVehicle()  
  9.     {  
  10.         return new Cruise();  
  11.     }  
  12. }  
  13. class SmallGroupFactory : AbstractFactory  
  14. {  
  15.     public override Vehicle CreateLandVehicle()  
  16.     {  
  17.         return new Car();  
  18.     }  
  20.     public override Vehicle CreateSeaVehicle()  
  21.     {  
  22.         return new Boat();  
  23.     }  
  24. }  
Client code
The code running in the client, picking up the vehicle that the desires but without exposing the returning object type. 
  1. static void Main(string[] args)  
  2. {  
  3.     AbstractFactory factory = null;  
  5.     Console.WriteLine("Hello. How many passengers do you need?");  
  6.     int passengers = Convert.ToInt32(Console.ReadLine());  
  7.     if (passengers > 15)  
  8.         factory = new LargeGroupFactory();  
  9.     else  
  10.         factory = new SmallGroupFactory();  
  12.     var landVehicle = factory.CreateLandVehicle();  
  13.     var seaVehicle = factory.CreateSeaVehicle();  
  15.     Console.WriteLine("Land Vehicle : " + landVehicle.GetData() + ". With capacity of: " + landVehicle.GetCapacity());  
  16.     Console.WriteLine("Sea Vehicle : " + seaVehicle.GetData() + ". With capacity of: " + seaVehicle.GetCapacity());  


Congratulations, you have successfully applied the Abstract Factory Method Design Pattern using C# with .NET Core.