The Factory Method Pattern

The Factory Method Pattern

Introduction

The Factory Method Pattern is a fundamental principle for creating objects without specifying their concrete classes at runtime, promoting flexibility and code reusability.

By delegating object creation to subclasses, it enhances maintainability and adaptability.

Understanding the Factory Method Pattern

The Factory Method Pattern revolves around the factory method, a method that returns an object of a particular type.

Subclasses can override this method to provide their own implementations, encapsulating the creation logic for specific types of objects.

Benefits of the Factory Method Pattern

The Factory Method Pattern offers several advantages, including:

  • Flexible Object Creation: It promotes flexible object creation by delegating object creation to subclasses.
  • Code Reusability: It encourages code reuse by promoting the creation of families of related objects.
  • Encapsulated Object Creation Logic: It encapsulates object creation logic in subclasses, making it easier to maintain and extend.
  • Easier to Test: It makes testing objects independently of their concrete factories easier.

Types of Factory Method Patterns

The Factory Method Pattern encompasses several variations, each with its specific implementation and application:

  • Simple Factory: This pattern provides a single factory method that returns objects of a specific type.
  • Abstract Factory: This pattern extends the Simple Factory Pattern by introducing multiple factory methods for creating related families of objects.
  • Virtual Constructor: This pattern utilises virtual functions to delegate object creation to classes.

Implementing the Factory Method Pattern in C#

To illustrate the implementation of the Factory Method Pattern in C#, consider a simplified scenario of creating computer components:

ComputerComponent Interface

public interface IComputerComponent
{
    void DisplayInformation();
}

Concrete Computer Components

public class Processor : IComputerComponent
{
    public void DisplayInformation()
    {
        Console.WriteLine("Processor: Intel i7-11700K");
    }
}

public class RAM : IComputerComponent
{
    public void DisplayInformation()
    {
        Console.WriteLine("RAM: 32GB DDR4");
    }
}

Abstract Factory

public abstract class ComputerComponentFactory
{
    public abstract IComputerComponent CreateProcessor();

    public abstract IComputerComponent CreateRAM();
}

Concrete Computer Factories

public class DellComputerComponentFactory : ComputerComponentFactory
{
    public IComputerComponent CreateProcessor()
    {
        return new DellProcessor();
    }

    public IComputerComponent CreateRAM()
    {
        return new DellRAM();
    }
}

Using the Factory Method

DellComputerComponentFactory dellFactory = new DellComputerComponentFactory();

IComputerComponent dellProcessor = dellFactory.CreateProcessor();
dellProcessor.DisplayInformation();

IComputerComponent dellRAM = dellFactory.CreateRAM();
dellRAM.DisplayInformation();

This example demonstrates a basic implementation of the Factory Method Pattern for creating computer components.

Conclusion

The Factory Method Pattern is a versatile tool for creating objects in C#.

It promotes flexibility, code reusability, and maintainability by delegating object creation to subclasses.

Developers can leverage this pattern to create adaptable and extensible applications catering to different configurations and requirements.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.