The Factory Design Pattern

Erik Zhou | Apr 16, 2023 min read

The factory design pattern is a widely-used creational design pattern in software development. This pattern provides a way to create objects without explicitly specifying the classes they belong to. In this blog post, we’ll cover the basics of the factory design pattern, why you might want to use it, and how to implement it in your projects using C#.

What is the Factory Design Pattern?

The factory pattern is a design pattern that allows you to create objects through a common interface, without specifying the exact class of the object that will be created. This pattern is particularly useful when working with complex object creation processes or when you need to maintain flexibility and scalability in your code.

The factory pattern encapsulates the object creation process and delegates it to separate factory classes, which can then create objects based on specific requirements.

Advantages of the Factory Design Pattern

Here are some of the key advantages of using the factory design pattern:

  1. Loose coupling: The factory pattern promotes loose coupling between classes by separating the object creation process from the actual objects.
  2. Extensibility: When new classes need to be added, you can simply extend the factory without modifying existing code.
  3. Single Responsibility Principle: By delegating the object creation process to factory classes, each class has a clear responsibility, making the code more maintainable and easier to understand.

Implementing the Factory Design Pattern in C#

Now, let’s take a look at a simple example to demonstrate how to implement the factory pattern in C#. Consider a scenario where we have different types of cars, and we want to create a factory to generate car objects based on the type requested.

Step 1: Define a common interface

First, we need to define a common interface that all car objects will implement.

public interface IPlane
{
    void Fly();
}

Step 2: Create concrete classes

Next, we’ll create concrete plane classes that implement the common interface.

public class F15Eagle : IPlane
{
    public void Fly()
    {
        Console.WriteLine("Flying an F-15 Eagle");
    }
}

public class B17FlyingFortress : IPlane
{
    public void Fly()
    {
        Console.WriteLine("Flying an B-17 Flying Fortress");
    }
}

Step 3: Create the factory class

Now, we’ll create the factory class responsible for creating car objects based on the type requested.

public class PlaneFactory
{
    public static IPlane NewF15() {
        return new F15Eagle();
    }

    public static IPlane NewB17() {
        return new B17FlyingFortress();
    }
}

Step 4: Use the factory to create objects

Finally, we can use the factory class to create plane objects based on the type requested.

IPlane plane1 = PlaneFactory.NewF15();
plane1.Fly();

IPlane plane2 = PlaneFactory.NewB17();
plane2.Fly();

In this example, we’ve demonstrated how to implement the factory design pattern in C# by creating a simple plane factory. By using the factory pattern, we can easily create new plane objects without directly instantiating their classes. This makes it so that when new sub-classes are added, minimal change will be needed to implement them.