Factory Method Design Pattern Explained.

Oshan Vikasith
4 min readMar 14, 2022
Photo by carlos aranda on Unsplash

We’re back with another design pattern after learning about the singleton coding pattern. The factory design pattern is a frequently used coding design pattern among software engineers. We’ll talk about the fundamentals of it as well as how to put it into practice. The procedure will be thoroughly covered in the article.

What is Factory method design pattern?

Define an interface (a java interface or an abstract class) and allow the subclasses to determine which object to instantiate, according to the factory design pattern.

The factory method pattern proceeds the object creation by letting subclasses to decide what objects to create.

This design pattern comes under the creational design pattern.

Generally, factories are involved in the creation of products, and this is one of them which will be dedicated to creation of objects . It’s one of the most effective methods for hiding object creation logic from the client.

Implementation of Factory method design pattern

The factory method pattern encapsulates object creation by allowing subclasses to choose the objects they want to create.

  1. Inside a abstract class or interface define the factory method
  2. Create subclasses that extend the abstract method or implementing the interface. (The subclasses will decide the creation of objects)

Implementation Code

Here is a practical demonstration of how the Factory method design pattern is used.

Creating the interface which holds the method that needs to be implemented This method can also be an abstract method. In here I used an interface to determine the solution. In this design pattern we use OOP concepts such as abstraction.

In below you will find the sub class implementations. The sub classes which I created was Normal, Silver, Gold and Platinum.

Figure 1: Interface implementing of classes.

All these sub classes are implemented by the Travel interface.

Basic package class implementation

Silver package class implementation

Gold package class implementation

Platinum package class implementation

As you can see the classes are implemented by the travel interface, which also can be obtained by extending a travel abstract class.

According to the given parameter in the main method, these objects will be created. In order to create these objects the following classes are used. This class will verify the input and it will deliver the particular object.

I have created an enum to store the types.

Using the following class the input will be identified and subsequently relevant objects will be created.

Here the createPackage() method is static so we can call the method without instantiating an object. This method will be called inside the main method.

Now the remaining implementation is on the main class inside the main method. Now the hard part is done, here what we have to do is call the static method which is defined in the TravelFactory class by including the correct parameter.

output

Figure 2 : Output

This is a sample implementation of the Factory method design pattern. Hope you understood the implementation process. In the Main class you can identify that according to the particular subclass, the object is created. We have to specify the certain sub class as a parameter.

Factory pattern is one of the most used design patterns in Java. This pattern provides one of the best ways to create an object.

Now we are going to discuss some of the differences between singleton design pattern and factory method design pattern.

  1. We don’t use parameters to create instances in a singleton design pattern. When it comes to the Factory method pattern, however, the parameters are always used to determine which instance is required.
  2. Interfaces and abstract methods are used when implementing.
  3. Factory method design method is used widely than the singleton pattern.
  4. Unlike the Singleton design pattern, the Factory method design pattern does not show the instantiated logic, and you will always see only what you get.

Advantages of using factory method design pattern

  • Factory Method Pattern allows the sub-classes to choose the type of objects to create.
  • It promotes the loose-coupling.
  • Good testability

Hope you had a better understanding regarding the Factory method design pattern, Hope to see you again in the future with another interesting article.

Here I have included the sources which I have referred.

(4271) Use of factory method design pattern | Design patterns you MUST know — YouTube

Design Pattern — Factory Pattern (tutorialspoint.com)

Factory method design pattern in Java — GeeksforGeeks

--

--