Why Should You Use the Factory Design Pattern?

In object-oriented programming, we use objects. Groundbreaking stuff, am I right?

These objects need to be created somehow.

The factory pattern aims to control this creation in an isolated, restricted manner.

Therefore, if you need to create an object, you should ask the factory for one, instead of dealing with the instantiation yourself.

If you think about it, it makes sense to delegate this object creation to someone else (i.e. the factory), especially if this creation logic is complicated.

Thus, the factory encapsulates this fancy business logic and is responsible for creating new objects.

The Java example later in this post will make things clearer.

Factory Pattern Flavours

First of all, let’s establish that there are two definite factory patterns. In addition, there’s also what I like to call a pseudopattern that I want to touch upon in this section.

As one of the main goals of this blog is to make information easier to digest, I’ve decided to treat each of these related patterns within their own blog post.

That way, everyone’s happy.

Firstly, we have the simple factory pseudopattern, and I use the prefix pseudo because it’s actually not considered a pattern in the Gang of Four book. This is basically a class that, based on a given input, returns objects of different types.

I’ve written a small blog post on the simple factory here. Looking at the diagram there will help you develop better intuition about what we’re trying to learn here.
Also, the code example in this blog post builds on the one there, but don’t worry if you decide to skip that post, the code here is self-contained.

Secondly, there’s the factory method pattern. This is what this post is about.

Thirdly, we also have the abstract factory pattern. Briefly, this groups together multiple factories to make a “superfactory”. I’ve covered this in the abstract factory pattern blog post.

I recommend first understanding the current article, since the abstract factory pattern builds on the factory method.

What is the Factory Method?

The factory pattern is a creational pattern that decouples client code from object creation.

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

As we said in the façade pattern post, an interface in this context refers to the concept of abstraction, of hiding away details.

Basically, the definition says that we trust the factory to create the object for us. In addition, the factory also decides the particular object to be created.

To illustrate this concept, let’s consider we’re running a computer manufacturing business. For the moment, we’re only focusing on laptops.

The Laptop interface represents the laptops. This interface is implemented by the two available types of laptops pictured as the NormalLaptop and GamingLaptop concrete classes.

Factory Method Class Diagram

As per the definition of the factory method, we’ll need an interface for creating our objects. This is the role of the LaptopFactory interface.

The next step is to create subclasses which will decide which of the two types of laptops will be instantiated. These subclasses are NormalLaptopFactory and GamingLaptopFactory, both implementing the LaptopFactory interface.

The createLaptop() methods of the concrete factories NormalLaptopFactory and GamingLaptopFactory decide which specific laptop class to instantiate.