How to Implement the Factory Pattern in C#
However, one of the most popular design patterns in development is the Factory Pattern, and it is for a good reason. Factory Pattern provides a loose-coupling format as it allows us to create objects without having to specify the exact class of the object that is created.
In a more technical wheel, Factory Pattern is a design pattern that provides an interface for creating an object in a superclass but allows the subclasses to alter the object type that are created.
In this tutorial, we will walk you through the fundamentals of creating and using the Factory Pattern using C#. Like all things in Design Patterns, it takes time and practice to learn how to implement them properly.
We recommend you to read more about the functionality and working of the Factory Pattern before diving into the code. Check out the following resource:
https://refactoring.guru/design-patterns/factory-method
A Basic Factory Pattern Implementation
In our case, we create an IVehicle interface that declares a method called ignite(). We then create two classes called Car() and Boat() that implement the IVehicle interface.
We then use a “Factory” class to create an instance of either the “Car” or “Boat” classes based on the provided parameter.
Creating an Interface
Let us start by creating the IVehicle interface. We can use basic C# interface definition as shown in the following:
{
void Ignite();
}
Implementing the Interface
Once we define the interface, we can implement it by creating the “Car” and “Boat” classes as demonstrated in the following example code:
{
void Ignite();
}
public class Car : IVehicle
{
public void Ignite()
{
Console.WriteLine("vroom.");
}
}
public class Boat : IVehicle
{
public void Ignite()
{
Console.WriteLine("Splash!!");
}
}
In the given example, we create the “Car” and “Boat” classes that implement the IVehicle interface using the Ignite() method.
Creating a Factory
We can then proceed to create a Car factory as demonstrated in the following example code:
{
public IVehicle CreateVehicle(string vehicleType)
{
switch (vehicleType.ToLower())
{
case "car":
return new Car();
case "boat":
return new Boat();
default:
throw new Exception("Invalid vehicle type");
}
}
}
In the given example, the VehicleFactory class is responsible for creating vehicles. The client code in the main method does not need to know the exact class type of the vehicle, only that it implements the IVehicle interface.
This ensures that the client code is detached from the concrete class. Consider the usage as shown in the following:
class Program
{
static void Main()
{
VehicleFactory vehicleFactory = new VehicleFactory();
IVehicle myCar = vehicleFactory.CreateVehicle("car");
myCar.Ignite();
IVehicle myBoat = vehicleFactory.CreateVehicle("boat");
myBoat.Ignite();
}
}
Conclusion
In this tutorial, we learned about the fundamentals of implementing a basic factory pattern in the C# language. We hope all of the given examples made sense and helped you to understand how to work with the Factory Design Pattern.
Source: linuxhint.com