Blog / Design Patterns
The Factory Design Pattern in C# and .Net
  • Dec 09, 2020
  • 73
  • 84

The second design pattern in Creational category is the Factory Pattern. Let's take a look at this pattern to see what it is and how to implement it in our program.

Note: Code can be downloaded at my Github


Advertisement
 


1. What is the Factory Design Pattern?

In real world applications, Factory Pattern is one of the most used design patterns. By using this pattern, objects are created without exposing the creation logic to the client by using a common interface.

Factory Design Pattern defines an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses.


2. When to use the Factory Design Pattern?

The Factory Design Pattern is used when you have classes which don't know what exact sub-classes it has to create. It happens in real world application when the Product implementation tends to change over time but the Client needs to remain unchanged.

For example, you are working at a logistics company which uses trucks as main vehicles to ship goods. You, as a developer, may come up with below design when developing a logistics application for your company.

Create a Truck class:

public class Truck
{
    public string Type { get; set; }
 
    public Truck()
    {
        Type = "Truck";
    }
 
    public void Delivery()
    {
        Console.WriteLine($"{Type} is used for delivery.");
    }
}

Then, you need to initiate an object from your Truck class and call delivery function:

class Program
{
    static void Main(string[] args)
    {
        Truck truck = new Truck();
        truck.Delivery();
    }
}


This design works fine but it can’t scale up. For example, if your company attracts more clients and many of them prefer air or sea shipping methods, you would need to modify the entire project code which is now coupled to the Truck class. Here, you need to add a new Ship class for sea shipping method and the Client's code need to be modified to initiate an object from the Ship class.

public class Ship
{
    public string Type { get; set; }
 
    public Ship()
    {
        Type = "Ship";
    }
 
    public void Delivery()
    {
        Console.WriteLine($"{Type} is used for delivery.");
    }
}
 
class Program
{
    static void Main(string[] args)
    {
        Ship ship = new Ship();
        ship.Delivery();
    }
}


So, we need to come up with a better design which the initiated objects are not coupled to the existing or new classes. The Factory Design Pattern can resolve this problem.

3. How to Implement Factory Design Pattern?

Look back at the definition of Factory Design Pattern, it says: "define an interface for creating objects". We can follow below step to implement the Factory Design Pattern in C# and .Net.

Step 1: Create a generic class

We should create an abstract Vehicle class and child classes for each type of vehicles. I also have an enum for VehicleType.

public enum VehicleType
{
    Truck,
    Ship,
    Airplane
}
 
public abstract class Vehicle
{
    public abstract string Type { get; set;  }
 
    public abstract void Delivery();
}

Classes for each vehicle type are inherited from Vehicle abstract class:

public class TruckVehicle : Vehicle
{
    public TruckVehicle()
    {
        Type = VehicleType.Truck.ToString();
    }
 
    public override string Type { get; set; }
 
    public override void Delivery()
    {
        Console.WriteLine($"{Type} is used for delivery.");
    }
}
 
public class ShipVehicle : Vehicle
{
    public ShipVehicle()
    {
        Type = VehicleType.Ship.ToString();
    }
 
    public override string Type { get; set; }
 
    public override void Delivery()
    {
        Console.WriteLine($"{Type} is used for delivery.");
    }
}
 
public class AirplaneVehicle : Vehicle
{
    public AirplaneVehicle()
    {
        Type = VehicleType.Airplane.ToString();
    }
 
    public override string Type { get; set; }
 
    public override void Delivery()
    {
        Console.WriteLine($"{Type} is used for delivery.");
    }
}


Step 2: Create a Factory class

We can now create a VehicleFactory class which will be used to create different vehicle types such as trucks, ships, or airplanes.

public interface IVehicleFactory
{
    Vehicle NewTruck();
    Vehicle NewShip();
    Vehicle NewAirplane();
}
 
public class VehicleFactory : IVehicleFactory
{
    public Vehicle NewAirplane()
    {
        return new AirplaneVehicle();
    }
 
    public Vehicle NewShip()
    {
        return new ShipVehicle();
    }
 
    public Vehicle NewTruck()
    {
        return new TruckVehicle();
    }
}

Now, we can initiate different vehicle objects depending on the Client needs by using the Factory we just created.

class Program
{
    static void Main(string[] args)
    {
        VehicleFactory vf = new VehicleFactory();
        Vehicle truckVehicle = vf.NewTruck();
        truckVehicle.Delivery();
 
        Vehicle shipVehicle = vf.NewShip();
        shipVehicle.Delivery();
 
        Vehicle airplaneVehicle = vf.NewAirplane();
        airplaneVehicle.Delivery();
    }
}

We can see that the VehicleFactory class is serving as an interface which is used to create objects for each vehicle type. The instantiation is not coupled to the child classes anymore. So, when we have a new type of shipping vehicle, we still can call the VehicleFactory to create an object of the new class.



If you have a Website or a Web API developed by using .Net Core and looking for a way to publish your applications, this post will explain how to do it using GoDaddy Windows Hosting.Note: at this mome ...

Search text in Stored Procedure in SQL SELECT DISTINCT o.name AS Object_Name, o.type_desc FROM sys.sql_modules m INNER JOIN sys.objects o ON m.object_id = o ...

Using cherry-pick to select specific commits for your Pull Request.1. Create a new branch based on the target of the Pull Requestgit branch cherry-branch origin/master2. Switch to a new branchgit chec ...

After deployment Angular and API on IIS, it's working fine unless I refresh the page. Once refreshed, the web encountered 404 error. In this article, I will explain how to resolve this.Since Angular i ...

There are some benefits of keeping both UI and API parts in the same place for small projects. In this article, I will explain how I did to deploy Angular Web and ASP .Net Core API in the same folder ...

I got CORS error after publishing my API and Angular app to IIS even though CORS is enabled and the origins of the Angular app is added. Below is how I resolved this issue.Just simple, make sure you s ...

1. The Situation:Error Message: Pulse Secure Application failed to load Java. Please install correct JRE version.Description: This issue happens when I'm using a M1 Mac with a correct version of ...

Accelerated Mobile Pages (AMP) focuses on delivering static content from publishers as quickly as possible and possibly rewards early adopters with a boost in rank. Let's see how to implement it ...

Below is how to decrypt/convert a Hex string value into text using VB.Net:Decrypting Hex string value to string in VB.Net Function HexToString(ByVal hex As String) As String Dim text As New Sy ...