The Factory Method Pattern

Definition

Defines an interface for creating an object, but lets subclasses decide which class to instantiate. The Factory Method lets a class defer instantiation to subclasses.

How does it work?

The factory method defers the creation of objects to subclasses. This means that instead of creating your objects in your classes like below, you are are deferring to the factory and letting it give you the concrete object.

MyObject myConcreteObject = new MyConcreteObject();

myConcreteObject.SomeMethod();

Above you will see that we are creating an object and then using that object. The problem with this is, if we were to want to add a new concrete object later on, we would have to come back and change this code. We would have to change the code everywhere else that we were creating this object.

Instead of the above code, we can use the Factory Method Pattern and write code like this.

MyObject myConcreteObject = myConcreteObjectFactory.GetObject();

myConcreteObject.SomeMethod();

Notice how we are now relying on this GetObject method to give us the correct object based on our needs. If we were to want to change the ConcreteObject later we would only have to change what gets returned by the GetObject() method and the rest of our code would reflect that change.

Why is this useful?

  • As mentioned above, there is only one place to change the code to add new concrete objects
  • The factory method pattern decouples the creation of an object from its use. This helps us adhere to the dependency inversion principle.
  • You can change the object your code uses at runtime

Example Class Diagram

Example Code

For this example, we are going to mock out a vehicle factory that will give us the correct vehicle.

Note: The sample code is in C#.

First we need to create Vehicle class that

all of our vehicles will implement. It will be very simple and only give us a quick description of the Vehicle. Check out the code below.

public abstract class Vehicle
{
    protected string _description = "Unknown Vehicle";

    public string GetDescription()
    {
        return _description;
    }
}

Now we can create our Creator class. Let’s call it “VehicleCreator”. This will be the class we call to get our vehicle objects.

public abstract class VehicleCreator
{
    public abstract Vehicle GetVehicle();
}

Lets create a couple of concrete vehicles, car and boat. They will both implement the abstract Vehicle class. They will be very simple and change the description.

public class Car : Vehicle
{
    public Car()
    {
        _description = "Red Corvette";
    }
}

public class Boat : Vehicle
{
    public Boat()
    {
        _description = "Blue Pontoon Boat";
    }
}

Now that we have our concrete vehicles we can create the ConcreteCreator classes that will create our Vehicles and return them. Notice how they implement the VehicleStore we created above.

public class CarCreator : VehicleCreator
{
    public override Vehicle GetVehicle()
    {
        return new Car();
    }
}

public class BoatCreator : VehicleCreator
{
    public override Vehicle GetVehicle()
    {
        return new Boat();
    }
}

Now that our Factory Method Pattern is complete, it is time to use it.

static void Main(string[] args)
{
    VehicleCreator carCreator = new CarCreator();
    VehicleCreator boatCreator = new BoatCreator();

    Vehicle vehicle = carCreator.GetVehicle();
    Console.WriteLine(vehicle.GetDescription());

    vehicle = boatCreator.GetVehicle();
    Console.WriteLine(vehicle.GetDescription());
}

Above we are creating two vehicle stores, one for the CarCreator and the other for the BoatCreator. We can then use them to create our vehicles and then get the description.

If you run all of the code above you will see the following:

I know this pattern may seem like overkill for this example and it definitely is but this pattern is very powerful and I recommend you give it a try. I tried to strip this example down to its bare bones for simplicity but imagine if you had a lot of vehicles and those vehicles got created in many places in your code.

I hope you have found this post helpful in learning about the factory method pattern. If so, please share this with your fellow developers so that they too can learn.