Factory Method Pattern in C#

The Factory Method Pattern is a creational design pattern that defines an interface for creating objects, but lets each subclass decide which object to create. It removes the need to hard-code object creation in the main logic, making your code easier to extend and modify.

Real-Life Analogy: Payment Gateways

Imagine you run an online store. Customers can pay using different gateways: Stripe, PayPal, or Square. As the store owner, you don't care how each gateway works internally — you just want to process payments.

So, you create a base class with a ProcessPayment() method, and then have each gateway (Stripe, PayPal, Square) implement its own way to handle the transaction. The checkout system simply uses the interface, and the actual payment class is decided at runtime.

Different payment gateways processing transactions using a shared interface
Like switching between Stripe, PayPal, and Square, the Factory Method Pattern lets each subclass decide how to create the object — while keeping the interface consistent.

Benefits of Factory Method

By delegating object creation to subclasses, the Factory Method keeps your code clean and flexible. It helps reduce tight coupling between parts of your app and makes it easier to introduce new types without changing existing code.

Some advantages include:

  • Encapsulates object creation logic
  • Makes adding new types easier without touching existing code
  • Improves testability and maintainability

When Should You Use It?

Many systems get cluttered with new keywords everywhere. This makes it hard to change how objects are created. The Factory Method gives you a better structure — especially when object creation varies by context.

Use it when:

  • You want to let subclasses decide which class to instantiate
  • You need flexibility to introduce new products
  • You want to reduce direct dependencies on concrete classes

When Not to Use It: Avoid it when your code is simple and object creation doesn't change. It may introduce unnecessary complexity.

What to Implement

To implement the Factory Method pattern, you need:

  • Product Interface: An interface or abstract class that defines what all created objects should implement
  • Concrete Products: One or more classes that implement the product interface
  • Creator: An abstract class or interface that declares a Create method
  • Concrete Creators: Subclasses that implement the Create method and return specific product instances

How It Works in C#


// Product Interface
public interface IPaymentGateway
{
    void ProcessPayment(decimal amount);
}

// Concrete Products
public class StripeGateway : IPaymentGateway
{
    public void ProcessPayment(decimal amount) =>
        Console.WriteLine("Processing $" + amount + " through Stripe.");
}

public class PayPalGateway : IPaymentGateway
{
    public void ProcessPayment(decimal amount) =>
        Console.WriteLine("Processing $" + amount + " via PayPal.");
}

// Creator
public abstract class PaymentProcessor
{
    public abstract IPaymentGateway CreateGateway();
}

// Concrete Creators
public class StripeProcessor : PaymentProcessor
{
    public override IPaymentGateway CreateGateway() => new StripeGateway();
}

public class PayPalProcessor : PaymentProcessor
{
    public override IPaymentGateway CreateGateway() => new PayPalGateway();
}

Client code:


PaymentProcessor processor = new StripeProcessor();
IPaymentGateway gateway = processor.CreateGateway();
gateway.ProcessPayment(100);

Final Thoughts

The Factory Method Pattern is like choosing the right payment processor at checkout. You don't need to rewrite your app for each gateway — just plug in a new one that follows the same rules.

This pattern helps you build flexible and scalable systems — especially when new types of products or behaviours may be added later.