Factory Pattern or Factory Method Pattern is a creational Design Pattern which means it is basically required for creating the objects.
In Factory Design Pattern , define an interface or abstract class for creating an object but the subclasses decides which class will be instantiated. In other way, subclasses are required to create the object of the class.
When to use factory pattern?
Factory design pattern enables loose coupling between classes which is the most important aspect one should consider while designing the application architecture.
Loose coupling can be introduced by abstract entities rather than by concrete implementation.
It is required
- When a base class doesn’t know what will be sub-classes required to create
- When the parent classes decides the making of instance to its sub-classes.
UML for Factory Method Pattern
- We will be creating a Bank abstract class and concrete classes that extends the Bank abstract class. A factory class GetBankFactory is defined in next step.
- GenerateEMI class will use GetBankFactory to get a Bank object. It will pass parameter (ICICIBank / HDFCBank) to GetBankFactory to get the type of object it needs.
Calculate EMI : A Real World Example of Factory Design Pattern
Step 1: Create a Bank abstract class. package creational.pattern.factory; import java.lang.Math; public abstract class Bank { protected double rate; abstract void getRate(); double getEMI(double principal,int time) { rate=rate/(12*100); double emi= (principal*rate*Math.pow(1+rate,time))/(Math.pow(1+rate,time)-1); return emi; } }
Step 2: Create the concrete classes that extends Bank abstract class.
package creational.pattern.factory; public class ICICIBank extends Bank { //@override public void getRate() { rate=10; } }
HDFCBank Class
package creational.pattern.factory; public class HDFCBank extends Bank { //@override public void getRate() { rate=9.40; } }
Step 3: Create a GetBankFactory to generate object of concrete classes
package creational.pattern.factory; public class GetBankFactory { //use getBank method to get object of type Bank public Bank getBank(String bankType) { if (bankType == null) { return null; } if (bankType.equalsIgnoreCase("ICICI")) { return new ICICIBank(); } else if (bankType.equalsIgnoreCase("HDFC")) { return new HDFCBank(); } return null; } }
Step 4: Generate EMI by using the GetBankFactory to get the object of concrete classes by passing parameter such as Principal ,tenure in months.
package creational.pattern.factory; public class GenerateEMI { public static void main(String args[]) { GetBankFactory bankFactory = new GetBankFactory(); String bankName="ICICI"; Bank b = bankFactory.getBank(bankName); b.getRate(); //in getEMI method Principal and total tenure in months is passed System.out.println(b.getEMI(1200, 12)); } }
Conclusion
You have learnt what is Factory Design Pattern in Java and how to create step by step process, when to create the Factory Pattern in Java.
very easy to understand and well explained, thanks for sharing.