Factory Method Design Pattern with Real-world example

Prasad Thilakarathne
4 min readJul 29, 2020

--

Here you will understand what Factory Method pattern is and you will see how this design pattern helps to solve a real-world problem.

I thought describing it with solving a real-world problem will help you to understand it rather than just describing it. First I’ll describe it using generic explanations and later in this article you can find a real-world example.

What is Factory Method Pattern?

Factory method is one of Creational pattern that uses a factory method to create objects without specifying concrete classes that needs to be created. Confused? Forget it, You will understand all of this down the line.

What Factory method pattern does?

It allows us to create objects by specifying their common interface at run time without imposing their concrete class creation logic.

When to use the Factory method pattern?

  1. There should be a set of classes which implement a common interface
  2. You don’t know which object to create ( You know which object to create only at run time)
  3. Object initialization is somewhat expensive(Have to do some operations in order to create an object)

Factory method act as an actual factory. It creates our products which we request but we don’t need to aware of how they are created.

Let’s jump into the problem.

Imagine you have to develop a system for a bank that allows it’s customers to open Bank accounts and get bank account details. Let’s say initially your application should support creating 3 account types(Personal, Business, and Checking account), and future there may be more account types.

Class diagram for factory method example case

BankAccount interface has few methods that can be overridden by subclasses: validateUserIdentity(), calculateInterestRate() and registerAccount(); and one attribute called name.

What happens you have to create each of the above concrete classes based on user input? Imagine this is a branch of the bank and we have to create accounts based on user requirements.

If the user input is P -> Create PersaonAccount object.

If the user input is B -> Create BusinessAccount object.

If the user input is C -> Create CheckingAccount object.

Following Branch.java class has openAccount() method which takes user input and creates a bank account based on it.

Then we can call it from our Main method, to check whether it is working.

Even the above code is working fine, directly we can identify three drawbacks.

The first one is, what happens if we have some other places in our code to do the same. Then we have to repeat the above if-else statement in all of those places.

Then the second issue is, what happens if we have to introduce some new bank accounts other than above 3. Then we have to modify our client in this case our openAccount method in the Branch and all other places in order to accommodate new account types.

The third issue is our Branch class is tightly coupled with all the bank account type concrete classes. If bank account types increased later, then there are a number of dependencies for our Branch.

The factory method design pattern addresses all the above issues. What we have to do is create a class called BankAccountFactory and move all the if-else statements which are object creation logic to a single place.

Then we have to improve our Branch to use this factory.

We have to inject BankAccountFactory to Branch constructor and using that factory createBankAccount method will get its bank account object created. Here you have to understand that our Branch doesn’t know how those accounts create, BankAccountFactory will do all of it.

But remember, still we have a problem here. It’s still not The Factory Method Patterns yet. This is called The Simple Factory method. Most of the tutorials people teach Simple Factory pattern as Factory method patterns. But we have to improve this Simple Factory pattern in order to fully encapsulated the object generation part so then we call it Factory Method Pattern.

UML Diagram for Simple Factory Pattern.

UML Diagram — Simple Factory Method
Simple Factory Method

The problem here is we cannot further customize our factory. For example, we have to open a new branch in a foreign country so there we have to have some special requirements when creating a BankAccount. If we create our factory which is BankAccountFactory abstract and make LocalBankAccountFactory and ForeignBankAccountFactory as two separate factories which extend abstract BankAccountFactory.

Now we can inject either of the factories to the Branch.

Now we can call it the Factory Method Design Pattern.

UML Diagram — Factory Method Design Pattern

Factory Method Design Pattern

The Main class can improve to get two inputs: one for account type and the other one for the branch.

I hope now you understand the Factory method pattern. Also please download and run the code sample from Github.

If you have any questions or suggestions please ask in the comment section.

Source code : https://github.com/Prasadct/design-patterns

--

--