Overview Of Factory Method Design Pattern

Arshad Suraj
Geek Culture
Published in
4 min readMay 23, 2021
Image by — google

What is Factory Method Design Pattern?

The factory method is a design pattern that provides an interface or abstract class for creating an object and allows its subclasses to decide which class to be instantiated. Factory pattern is classified as a creational pattern.

For easier understanding, Let’s assume a shop contains a variety of products, and the shopkeeper delivers us the things we require based on our requirements. In the same way, the factory pattern works. Typically, we pass parameters. Our factory method determines which class should be instantiated based on the parameter we passed, and creates and provides an instance of that specific class.

Implementation

Class diagram of factory method design pattern

Things to Keep in Mind:

  • All the subclasses Ex: GoldCard, SilverCard etcshould extend same parent class Ex: CardType .
  • Define a factory method inside Factory class. The return type of the method should be the parent class Ex:CardType .
  • Allow subclasses to create an object based on the parameter passed to the factory method.

Let’s implement the code to see how the Factory method design pattern works.

Assume a bank offers credit cards to their customers. They offer 3 types of credit cards such as Silver, Gold, and Platinum cards. Each card type has a different credit limit For Ex: credit limit of Silver card is 100,000 LKR, credit limit of gold card is 250,000 LKR and credit limit of Platinum card is 250,000 LKR.

The type of card a customer receives is dependent on his or her monthly salary. A Silver card is available to anyone with a monthly salary of less than 50,000LKR. He can receive a Gold card if his monthly income is less than 100,000LKR. He is eligible for a Platinum card if his monthly salary exceeds 100,000LKR. For EX: If your salary is 85,000LKR, then you can get a Gold card

Now let’s code the above scenario using Factory method design pattern.

Code snippet for parent class of all the subclasses

Above is the parent abstract class for all subclasses.

code snippet of sub-class 1
code snippet of sub-class 2
code snippet of sub-class 3

Above are the subclasses which extends from same parent class.

The Factory class which contains the Factory method is shown below,

Code snippet of Factory class

This Factory method is responsible for determining which class to instantiate. This method will create and return an instance of the appropriate class based on the parameter we provide to it.

below is the main class,

The outputs for the above codes are below,

Output 1
Output 2
Output 3

As a result, we can use the Factory design pattern to retrieve different objects from different classes based on our parameter. factory pattern will decide which class to instantiate without showing the implementation details to the client.

Advantage of Factory Design Pattern

  • Factory Method Pattern allows the sub-classes to choose the type of objects to create.
  • The implementation only interacts with parent abstract class or interface. Therefore, it can work with any classes that implement that interface or that extends that abstract class.

When to use Factory Design Pattern

  • When a class doesn’t know which sub-class should create the instance.
  • When a class wants that its sub-classes should specify the objects to be created.
  • When the parent classes choose the creation of objects to its sub-classes.
  • when you want to offer your library’s users the ability to extend its internal components

Real-world examples where Factory Method design pattern is used

This design pattern is commonly utilized in JDK, for example: getInstance() method of java.util.Calendar, NumberFormat, and ResourceBundle use factory method design pattern.

Keep Learning ❤️

--

--