Understanding the Factory Pattern Using .NET and C#

Abdelmajid BACO
3 min readJan 1, 2023

Creating Flexible Solutions with the Factory Method Pattern

Photo by carlos aranda on Unsplash

Here is a short article about factory pattern, abstract factory and factory method using C#.

Factory Pattern

The Factory Pattern is an object-oriented design pattern used to create objects without exposing the complexities of their construction. Factory can be implemented in two ways: Factory Method and Abstract Factory.

Factory Method

The Factory Method Pattern can be used as an alternative to constructors in a variety of situations. With this pattern, a factory object is responsible for creating objects from a common interface, thus abstracting the creation process from the user.

Here is an example of how the Factory Method Pattern can be used.

Suppose you need to build a toast notification tool. You could create a ToastCreator class which would be responsible for building the appropriate toast based on the specific notification type. This way, you can create toast notification without needing to know the details of each one.

public abstract class ToastCreator 
{
public abstract Error CreateToastError();
public abstract Warning CreateToastWarning();
public abstract Information CreateToastInformation();
public abstract Success CreateToastSuccess();
}

public class ToastNotification : ToastCreator
{
public override Error CreateToastError()
{
return new Error();
}

public override Warning CreateToastWarning()
{
return new Warning();
}

public override Information CreateToastInformation()
{
return new Information();
}

public override Success CreateToastSuccess()
{
return new Success();
}
}
public class Error
{
public Error() => System.Console.WriteLine("Create toast notification Error");
}
public class Warning
{
public Warning() => System.Console.WriteLine("Create toast notification Warning");
}
public class Information
{
public Information() => System.Console.WriteLine("Create toast notification Information");
}
public class Success
{
public Success() => System.Console.WriteLine("Create toast notification Success");
}

Now, if you needed to create a success toast notification, you could simply call the CreateToastSuccess() method of the ToastCreator and have it return the correct notification.

ToastNotification notification = new ToastNotification();
Success success = notification.CreateToastSuccess(); // output: Create toast notification Success
Information information = notification.CreateToastInformation(); // output: Create toast notification Information

Abstract Factory

The Abstract Factory Pattern allows you to create families of related objects without specifying the exact classes of those objects. It achieves this by providing an interface for creating multiple related objects. This way, you can create an entire family of related objects without needing to know the specifics of each object type.

Here is an example of how the Abstract Factory Pattern can be used.

Suppose you own a car shop that sells both cars and motorcycles. You could create an Abstract Factory object that will generate the appropriate type of vehicle based on the information it receives from the customer.

public interface IFactory
{
T Create<T>() where T : class;
}
public class VehicleFactory : IFactory
{
public T Create<T>() where T : class
{
return typeof(T) switch
{
Type vehicule when vehicule == typeof(Car) => new Car() as T,
Type vehicule when vehicule == typeof(Motorcycle) => new Motorcycle() as T,
_ => throw new IndexOutOfRangeException($"Cannot create this type {typeof(T)}"),
};
}
}
public class Car
{
public Car() => System.Console.WriteLine("Build a Car");
}
public class Motorcycle
{
public Motorcycle() => System.Console.WriteLine("Build a Motorcycle");
}
public class Bike
{
public Bike() => System.Console.WriteLine("Build a Bike");
}

This way, your customers can get the type of vehicle they need without needing to know the specific details of its construction.

IFactory factory = new VehicleFactory(); 
Car car = factory.Create<Car>(); // output : Build a Car
Motorcycle motorcycle = factory.Create<Motorcycle>(); // output : Build a Motorcycle
Bike bike = factory.Create<Bike>(); // throw Unhandled exception. System.IndexOutOfRangeException: Cannot create this type Bike

Conclusion

Using the Factory Pattern with C# allows developers to quickly and easily create objects. Furthermore, this pattern provides an elegant solution that prevents developers from having to repeat the same code in multiple locations.

Thank you so much for reading! If you found this information useful, please consider following me for more helpful tips and resources.

--

--

Abdelmajid BACO

Senior Full Stack .Net / Angular Developer, Cloud & Azure DevOps, Carrier Manager, Husband, and Father.