An abstract factory design pattern is one of the constructive patterns. The Abstract Factory pattern works around a super-factory that creates other factories. A factory pattern or factory method pattern says that simply “define an interface or abstract class to create the object, but let the subclass decide which class to instantiate“.
Benefit of Abstract Factory Pattern
- The Abstract Factory Pattern permits the sub-classes to pick the kind of objects to make.
- The Abstract Factory pattern is a focal plan design for dependency injection.
- Abstract Factory Pattern advances the loose coupling by wiping out the need to tie application-explicit classes into the code. That implies the code cooperates exclusively with the resultant interface or conceptual class, so it will work with any classes that execute that interface or that broadens that theoretical class.
- Abstract Factory Pattern is extraordinary for supporting numerous stages while keeping your code-base bound together.
Use of Abstract Factory Method
- At the point, when a class doesn’t have a clue what sub-classes will be needed to make.
- A when a class needs that its sub-classes determine the items to be made.
- At the point when the parent classes pick the production of objects to its sub-classes.
Example of Abstract Factory Pattern
1 – Create an interface
import java.io.*;
interface Banks{
String getBankName();
}
2 – Create concrete classes that implement the Bank interface
class ICICI implements Banks{
private final String BankNAME;
public ICICI(){
BankNAME="ICICI Bank";
}
public String getBankName() {
return BankNAME;
}
}
class PNB implements Banks{
private final String BankNAME;
public PNB(){
BankNAME="Punjab National Bank";
}
public String getBankName(){
return BankNAME;
}
}
3 – Create the abstract class.
abstract class Loan{
protected double rate;
abstract void getInterestRate(double rate);
public void calculateLoanPayment(double loanamount, int year)
{
double EMI;
int num;
num=year*12;
rate=rate/1200;
EMI=((rate*Math.pow((1+rate),num))/((Math.pow((1+rate),num))-1))*loanamount;
System.out.println("Your monthly EMI is "+ EMI +" for the Amount"+loanamount+" You have borrowed");
}
}
4 – Make concrete classes that expand the Loan abstract class
class HouseLoan extends Loan{
public void getInterestRate(double r){
rate=r;
}
}
class EducationLoan extends Loan{
public void getInterestRate(double r){
rate=r;
}
}
5 – Create an abstract class of AbstractFactory
abstract class AbstractFactory{
public abstract Bank getBank(String bank);
public abstract Loan getLoan(String loan);
}
6 – Create the factory classes that inherit the AbstractFactory class to generate the object of the concrete class based on the given information
class BankFactory extends AbstractFactory{
public Banks getBank(String bank){
if(bank == null){
return null;
}
if(bank.equalsIgnoreCase("PNB")){
return new PNB();
} else if(bank.equalsIgnoreCase("ICICI")){
return new ICICI();
}
return null;
}
public Loan getLoan(String loan) {
return null;
}
}
class LoanFactory extends AbstractFactory{
public Bank getBank(String bank){
return null;
}
public Loan getLoan(String loan){
if(loan == null){
return null;
}
if(loan.equalsIgnoreCase("House")){
return new HouseLoan();
} else if(loan.equalsIgnoreCase("Education")){
return new EducationLoan();
}
return null;
}
}
//End of Loan loan factory class
7 – Create a FactoryCreator class to get the factories by passing information such as Bank or Loan
class FactoryCreator {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("Bank")){
return new BankFactory();
} else if(choice.equalsIgnoreCase("Loan")){
return new LoanFactory();
}
return null;
}
}
8 – Use the FactoryCreator to get AbstractFactory to get factories of concrete classes by passing information such as type
class AbstractFactoryPatternExample {
public static void main(String args[])throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Name of Bank from where you want to take loan: ");
String bankName=br.readLine();
System.out.print("\n");
System.out.print("Enter the type of loan, House loan or Education loan : ");
String loanName=br.readLine();
AbstractFactory bankFactory = FactoryCreator.getFactory("Bank");
Bank b=bankFactory.getBank(bankName);
System.out.print("\n");
System.out.print("Enter the Interest Rate for "+b.getBankName()+ ": ");
double rate=Double.parseDouble(br.readLine());
System.out.print("\n");
System.out.print("Enter the Loan Amount you want to take: ");
double loanAmount=Double.parseDouble(br.readLine());
System.out.print("\n");
System.out.print("Enter the number of Years to pay your entire loan amount: ");
int years=Integer.parseInt(br.readLine());
System.out.print("\n");
System.out.println("you are taking the loan from "+ b.getBankName());
AbstractFactory loanFactory = FactoryCreator.getFactory("Loan");
Loan l=loanFactory.getLoan(loanName);
l.getInterestRate(rate);
l.calculateLoanPayment(loanAmount,year);
}
}
Conclusion
In this blog, you understand what is Abstract Factory Method is and where we can use Abstract Factory Method Pattern. I hope you like my blog, for more blogs click Here
References:
https://www.baeldung.com/solid-principles
