Creational Design Pattern (Factory Method)

Jeremy Loh
4 min readOct 30, 2020

--

There are 5 creational patterns:

  1. Factory Method
  2. Abstract Factory
  3. Builder
  4. Prototype
  5. Singleton

In this post, I will be covering one of the Creational Design Patterns: the Factory Method. But before that, some information about design patterns.

What are design patterns?

In software design, there are a set of commonly occurring problems that have pre-made blueprints that we can use to solve these design problems.

You cannot find and use a pattern directly in a program, the way you can do so with external libraries. A pattern is not a specific set of code but is a general idea for how to solve a particular design problem (it is a high level description of a solution).

Why should we learn design patterns?

Design patterns can be thought of as a toolkit of tried and tested solutions to software design problems that occur frequently. Knowing these patterns can teach us how to solve different kinds of problems using principles of object-oriented design.

Design patterns also allow you and your teammates to communicate in a more effective manner. You can refer to a specific design pattern and everyone will be able to understand the idea behind your suggestion.

Types of design patterns

  1. Creational patterns

Provide object creation mechanisms that increase flexibility and reuse of existing code

2. Structural patterns

Explain how to assemble objects and classes into larger structures, while keeping structures flexible and efficient

3. Behavioral patterns

Take care of efficient communication and the assignment of responsibilities between objects

Factory Method

Intent of Factory Method

  • Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

Usage of Factory Method

Imagine that you are developing a logistics management application. The application can currently handle only trucks, so most of the code is present in the Truck class

After some time, your application gets popular and you receive many requests from sea transportation companies to incorporate sea logistics into the application.

This is hard to do as most of the code is coupled to the Truck class. Adding Ships will require making changes to the entire codebase. If you want to add different types of transportation to the application, you will have to repeat this process of modification again! This will cause the code to have multiple conditionals that switch the application’s behaviour depending on the class of transportation given, which will be very hard to maintain.

Solution

The Factory Method pattern suggests that direct object construction calls (using the new operator) will be replaced with class to a special factory method. This factory method will create the classes and return them as an Object. The returned Objects are often referred to as products.

Now we can override the factory method in a subclass and change the class of products created by the method! We need to make the subclass objects part of a common base class or interface as the factory method in the base class should have its return type declared by the interface.

The code that uses the factory method (often called client code) does not see a difference between the products returned by the various subclasses as they are treated as a common interface/class. The common interface/class will have methods that are implemented and can be directly used by the client. The inner workings are abstracted away from the client!

  1. The Product is an interface that is common to all objects that can be produced by the creator and its subclasses
  2. The various implementations of the Product interface are the Concrete classes (Concrete ProductA and Concrete ProductB)
  3. The Creator class declares the factory method that returns new Product objects (the return type matches the interface)
  4. Concrete Creators override the base factory method so that a different type of Product is returned

The factory method does not have to create new instances all the time. It can return existing objects from an existing source (e.g. cache).

When the Factory Method should be used

  • When you don’t know beforehand the exact types and dependencies of the objects your code should work with
  • When you want to provide users of your library or framework a way to extend its internal components
  • When you want to save system resources by reusing existing objects instead of rebuilding them each time

Advantages of Factory Method

  • Avoid tight coupling between the creator and the concrete products
  • Single Responsibility Principle. You can move the product creation code into another place in the program instead of bundling it.
  • Open/Closed Principle. You can introduce new types of products into the program without breaking existing client code.

Disadvantages of Factory Method

  • The code may become more complicated as there is a need to introduce a lot of new subclasses to implement the pattern.

--

--

Responses (1)