Factory Method Design Pattern in C#: A Complete Guide

In software development, object creation is a frequent task. But what if your application needs to create different types of objects dynamically, without tightly coupling your code to specific classes? This is where the Factory Method Design Pattern comes in.

The Factory Method Pattern is one of the most commonly used creational design patterns in C#. It provides a flexible way to create objects while hiding the creation logic from the client.

In this article, weโ€™ll explore what the Factory Method pattern is, why itโ€™s useful, how to implement it in C#, and some real-world examples.

๐Ÿ”น What is the Factory Method Pattern?

The Factory Method Pattern defines an interface for creating objects but lets subclasses decide which class to instantiate.

Instead of writing code like this everywhere:

var car = new Car();

You use a factory method that returns the right object for you. This removes the responsibility of object creation from the client and centralizes it.

๐Ÿ”น Structure of the Factory Method Pattern

The pattern usually consists of:

  1. Product (Interface or Abstract Class) โ†’ Declares an interface for objects.
  2. Concrete Products โ†’ Actual implementations (e.g., Car, Bike).
  3. Creator (Factory Class) โ†’ Defines the factory method.
  4. Concrete Creators โ†’ Override the factory method to return specific objects.

๐Ÿ”น Factory Method Example in C#

Letโ€™s implement a simple example of the Factory Method in C#:

// Step 1: Product
public abstract class Vehicle
{
    public abstract void Drive();
}

// Step 2: Concrete Products
public class Car : Vehicle
{
    public override void Drive() => Console.WriteLine("Driving a Car");
}

public class Bike : Vehicle
{
    public override void Drive() => Console.WriteLine("Riding a Bike");
}

// Step 3: Creator
public abstract class VehicleFactory
{
    public abstract Vehicle CreateVehicle();
}

// Step 4: Concrete Creators
public class CarFactory : VehicleFactory
{
    public override Vehicle CreateVehicle() => new Car();
}

public class BikeFactory : VehicleFactory
{
    public override Vehicle CreateVehicle() => new Bike();
}

Usage:

VehicleFactory factory = new CarFactory();
Vehicle vehicle = factory.CreateVehicle();
vehicle.Drive();   // Output: Driving a Car

factory = new BikeFactory();
vehicle = factory.CreateVehicle();
vehicle.Drive();   // Output: Riding a Bike

๐Ÿ”น Real-World Analogy

Think about a Car Showroom.

  • When you go to a showroom and say โ€œI need a vehicle,โ€ the showroom staff decides whether to give you a Car or a Bike based on your choice.
  • You donโ€™t need to know how the car or bike is builtโ€”you just request and the factory (showroom) handles it.

This is exactly what the Factory Method does in programming.

๐Ÿ”น Advantages of Factory Method Pattern

โœ… Loose Coupling โ€“ Client code doesnโ€™t depend on concrete classes.
โœ… Flexibility โ€“ You can add new product types without changing existing code.
โœ… Single Responsibility Principle โ€“ Centralizes object creation logic.
โœ… Improved Testability โ€“ Easy to mock factories for unit testing.

๐Ÿ”น When to Use the Factory Method in C#

  • When your code needs to work with multiple object types.
  • When object creation logic is complex or changes frequently.
  • When you want to decouple client code from concrete implementations.

๐Ÿ”น Comparison: Factory Method vs Simple Factory

FeatureSimple FactoryFactory Method
ImplementationOne central class creates objects.Uses inheritance and abstract methods.
FlexibilityLimited โ€“ Adding new products may break code.High โ€“ Easy to extend by creating new factories.
ExampleVehicleFactory.GetVehicle("Car")CarFactory : VehicleFactory

๐Ÿ† Conclusion

The Factory Method Design Pattern in C# is a powerful creational pattern that promotes loose coupling and scalability. By using it, you can centralize object creation and make your application more flexible to change.

If youโ€™re working on enterprise-level applications, or preparing for a .NET interview, understanding the Factory Method pattern is a must.

By combining theory, C# code examples, and real-world analogies, developers can master the Factory Method and apply it effectively in real projects.

Leave a Comment