Factory Method Pattern in C#: Definition, Examples, Pros & Cons

Factory Method Pattern in C#: Definition, Examples, Pros & Cons

The Factory Method pattern in C# defines an interface for creating an object but lets subclasses decide which class to instantiate.

The Factory Method pattern is a creational design pattern that delegates object creation to subclasses instead of instantiating objects directly. It defines a common interface or abstract method for creating objects, while allowing derived classes to provide specific implementations. This helps remove tight coupling between client code and concrete classes. The pattern promotes flexibility and extensibility by enabling new object types to be introduced without modifying existing code. It is widely used in frameworks and libraries where the exact type of object may vary at runtime.

Why We Use Factory Method Pattern in C#?

We use this pattern to decouple object creation from usage, making the code more flexible and easier to maintain. It allows new types to be added with minimal changes to existing logic. It also supports the Open/Closed Principle by enabling extension without modification.

When to Use Factory Method Pattern in C#?

Use this pattern when a class cannot anticipate the exact type of objects it needs to create. It is useful when object creation logic should be centralized but still customizable by subclasses. It also fits scenarios where you want to delegate responsibility for object creation to derived classes or when working with frameworks that rely on extension points.

Example Use Cases with C# Code

1. Document Creator (Classic Example)

// Product
public abstract class Document
{
    public abstract void Print();
}

// Concrete Products
public class PdfDocument : Document
{
    public override void Print() => Console.WriteLine("Printing PDF Document");
}

public class WordDocument : Document
{
    public override void Print() => Console.WriteLine("Printing Word Document");
}

// Creator
public abstract class DocumentCreator
{
    public abstract Document CreateDocument();

    public void PrintDocument()
    {
        var doc = CreateDocument();
        doc.Print();
    }
}

// Concrete Creators
public class PdfCreator : DocumentCreator
{
    public override Document CreateDocument() => new PdfDocument();
}

public class WordCreator : DocumentCreator
{
    public override Document CreateDocument() => new WordDocument();
}

2. Notification System

public interface INotification
{
    void Send(string message);
}

public class EmailNotification : INotification
{
    public void Send(string message) => Console.WriteLine($"Email: {message}");
}

public class SmsNotification : INotification
{
    public void Send(string message) => Console.WriteLine($"SMS: {message}");
}

public abstract class NotificationFactory
{
    public abstract INotification CreateNotification();
}

public class EmailFactory : NotificationFactory
{
    public override INotification CreateNotification() => new EmailNotification();
}

public class SmsFactory : NotificationFactory
{
    public override INotification CreateNotification() => new SmsNotification();
}

3. Logistics / Transport System

public interface ITransport
{
    void Deliver();
}

public class Truck : ITransport
{
    public void Deliver() => Console.WriteLine("Deliver by land");
}

public class Ship : ITransport
{
    public void Deliver() => Console.WriteLine("Deliver by sea");
}

public abstract class Logistics
{
    public abstract ITransport CreateTransport();

    public void PlanDelivery()
    {
        var transport = CreateTransport();
        transport.Deliver();
    }
}

public class RoadLogistics : Logistics
{
    public override ITransport CreateTransport() => new Truck();
}

public class SeaLogistics : Logistics
{
    public override ITransport CreateTransport() => new Ship();
}

Advantages of Factory Method Pattern in C#

• Reduces tight coupling between client and concrete classes
• Supports Open/Closed Principle
• Makes code more flexible and extensible
• Centralizes object creation logic
• Encourages use of interfaces and abstraction

Disadvantages of Factory Method Pattern in C#

• Increases number of classes in the system
• Can add unnecessary complexity for simple cases
• Requires subclassing to extend functionality
• May make code harder to follow for beginners

Comparison with Similar Patterns

Pattern Purpose Key Difference Use Case
Factory Method Create a single object Delegates creation to subclasses Framework extensibility, plugins
Abstract Factory Create families of related objects Produces multiple related objects UI systems, product families
Builder Construct complex objects step by step Focuses on object construction process Complex configurations
Simple Factory (Not GoF) Create objects via a single method No inheritance, uses conditional logic Basic object creation logic