Factory pattern or Factory method Design Pattern is one of the most used the creational design pattern, according to this design pattern you creates an object with exposing the underlying logic to the client and assign new object to caller using a common interface or abstract class.
In Factory pattern, we hides actual logic of implementation detail of an object how create it and which class to instantiate it. So client shouldn’t worry about creating, managing and destroying object, factory pattern take responsibility of these tasks. Factory pattern is one of the most used design patterns in Java.
According to the Gang of Four:
“Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.”
As traditionally, we all are aware how to create the using new keyword in java as below:
Account account = new Account();
But this way is not suitable for sometime, because it is hard coded way to create an object, also it is not a best practice to create an object if object might be changed according to the nature of the program. Here creation design pattern provide the flexibility to create an according to the nature of program.
Let’s see the following UML class diagram for the Factory Design Pattern.
Let’s see the classes and objects participating in this pattern are:
Product (Account)
ConcreteProduct (SavingAccount, CurrentAccount)
Creator (AccountFactory)
ConcreteCreator (AccountFactoryImpl)
also read:
Let’s see the following pros of the Factory Method Design Pattern:
Let’s see the following cons of the Factory Method Design Pattern:
There are following cases where we can apply the Factory method pattern in the application.
There are following implementations based on this pattern.
Suppose you have set of classes i.e. SavingAccount, and CurrentAccount which extends a common super class or interface i.e. Account. You have one more class, i.e. a Factory class, define with a method with taking one or more arguments. This method is know as factory method and this factory method has return type a Super class or interface as define Account so that it provide you loose coupling, you can program to interface rather than implementation. So according to passed arguments in the factory method, decides on which sub class to instantiate. This factory method will have the super class as its return type.
Account.java
/** * */ package com.doj.patterns.creational.factory; /** * @author Dinesh.Rajput * */ public interface Account { void accountType(); }
SavingAccount.java
/** * */ package com.doj.patterns.creational.factory; /** * @author Dinesh.Rajput * */ public class SavingAccount implements Account { @Override public void accountType() { System.out.println("SAVING ACCOUNT"); } }
CurrentAccount.java
/** * */ package com.doj.patterns.creational.factory; /** * @author Dinesh.Rajput * */ public class CurrentAccount implements Account { @Override public void accountType() { System.out.println("CURRENT ACCOUNT"); } }
AccountFactory.java
/** * */ package com.doj.patterns.creational.factory; /** * @author Dinesh.Rajput * */ public class AccountFactory { final String CURRENT_ACCOUNT = "CURRENT"; final String SAVING_ACCOUNT = "SAVING"; //use getAccount method to get object of type Account //It is factory method for object of type Account public Account getAccount(String accountType){ if(CURRENT_ACCOUNT.equals(accountType)) { return new CurrentAccount(); }else if(SAVING_ACCOUNT.equals(accountType)){ return new SavingAccount(); } return null; } }
FactoryPatternDemo.java
/** * */ package com.doj.patterns.creational.factory; /** * @author Dinesh.Rajput * */ public class FactoryPatternDemo { /** * @param args */ public static void main(String[] args) { AccountFactory accountFactory = new AccountFactory(); //get an object of SavingAccount and call its accountType() method. Account savingAccount = accountFactory.getAccount("SAVING"); //call accountType method of SavingAccount savingAccount.accountType(); //get an object of CurrentAccount and call its accountType() method. Account currentAccount = accountFactory.getAccount("CURRENT"); //call accountType method of CurrentAccount currentAccount.accountType(); } }
SAVING ACCOUNT CURRENT ACCOUNT
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…