Factory Method – Tutorial and Example

What is Factory Method?

Factory method is a creational pattern that helps in abstracting the process of object creation. What this means is that the class that uses the object is not necessarily the class that instantiates the object. In fact, the class that uses the object has absolutely no idea about the exact type of the object created! It just has a reference to the interface.We will understand what this means using a car manufacturing algorithm.

Example of Factory Method:

We design a framework that can build cars. We build two types of cars, a Mazda and a Ferrari. The car is built using a CarBuilder.

Lets see how we would build the cars in a non Design Pattern world. The first tab shows two car classes, Mazda and Ferrari. The CarBuilder builds the two cars. It has separate methods to build Ferrari and Mazda. It also has separate methods to set Color to it.

Lets list out the problems with these:

  • The CarBuilder class cannot incorporate change easily. If you get a new car type, you need to change the class to add methods to create the new car type.
  • The CarBuilder stores reference to the concrete car implementation. That’s not a good design practice.
  • Lets say you have a new version of Mazda that extends the original vesion. Mazda2 extends Mazda. With this implementation, you would have to change the Builder class.

To solve these problems, we do the following

  • We have the Cars extend a common interface. The Builder then stores the reference to the Car interface (and not to the concrete implementations). We can now add new cars and the builder does not have to change.
  • We extend the CarBuilder (now abstract) and provide CarBuilders for both Mazda and Ferrari. The instantiation of the cars is done in the concrete implementations.

Class diagram for car builder using Factory Method

Design Pattern Factory Method

What have we changed?

We now have parallel hierarchies. The Car (Product) and its sub classes. The CarBuilder and its sub classes. Both hierarchies can vary independent of each other.

The Instantiation of the Car happens in its appropriate builder. buildCar is the factory method. The abstract CarBuilder class declares an abstract method that is implemented by the various builders.

None of the classes hold reference to concrete implementations. All references are to interfaces. The Builder does not know which Car it will get. It might get a Mazda or a Mazda2

Here are the classes

Another way to implement factory method is to use a parameterized constructor for the Car. In the example below we use a single CarBuilder, but the type of car to be instantiated depends upon a parameter passed in the factory method buildCar

To sum up, use factory method to create new objects so that the process of object creation is encapsulated and separated from the class that uses that objects.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.