Abstract Factory Pattern

Abstract Factory Pattern

Abstract Factory Pattern says that just define an interface or abstract class for creating families of related (or dependent) objects but without specifying their concrete sub-classes.That means Abstract Factory lets a class returns a factory of classes. So, this is the reason that Abstract Factory Pattern is one level higher than the Factory Pattern.

An Abstract Factory Pattern is also known as Kit.

Advantage of Abstract Factory Pattern

  • Abstract Factory Pattern isolates the client code from concrete (implementation) classes.
  • It eases the exchanging of object families.
  • It promotes consistency among objects.

Usage of Abstract Factory Pattern

  • When the system needs to be independent of how its object are created, composed, and represented.
  • When the family of related objects has to be used together, then this constraint needs to be enforced.
  • When you want to provide a library of objects that does not show implementations and only reveals interfaces.
  • When the system needs to be configured with one of a multiple family of objects.

UML for Abstract Factory Pattern

  • We are going to create a Bank interface and a Loan abstract class as well as their sub-classes.
  • Then we will create AbstractFactory class as next step.
  • Then after we will create concrete classes, BankFactory, and LoanFactory that will extends AbstractFactory class
  • After that, AbstractFactoryPatternExample class uses the FactoryCreator to get an object of AbstractFactory class.
  • See the diagram carefully which is given below:

abstract factory pattern

Example of Abstract Factory Pattern

Here, we are calculating the loan payment for different banks like HDFC, ICICI, SBI etc.

Step 1: Create a Bank interface

import java.io.*;     

interface Bank{  

        String getBankName();  

}  

Step 2: Create concrete classes that implement the Bank interface.

class HDFC implements Bank {

private final String BNAME;

publicHDFC() {

BNAME = “HDFC BANK”;

}

public String getBankName() {

returnBNAME;

}

}

class ICICI implements Bank {

private final String BNAME;

ICICI() {

BNAME = “ICICI BANK”;

}

public String getBankName() {

returnBNAME;

}

}

class SBI implements Bank {

privatefinalString BNAME;

publicSBI() {

BNAME = “SBI BANK”;

}

public String getBankName() {

returnBNAME;

}

}

Step 3: Create the Loan abstract class.

abstract class Loan {

protecteddoublerate;

abstractvoidgetInterestRate(double rate);

public void calculateLoanPayment(double loanamount, int years) {

/*

* to calculate the monthly loan payment i.e. EMI

* 

* rate=annual interest rate/12*100; n=number of monthly installments; 1year=12

* months. so, n=years*12;

* 

*/

doubleEMI;

intn;

n = years * 12;

rate= rate / 1200;

EMI = ((rate * Math.pow((1 + rate), n)) / ((Math.pow((1 + rate), n)) 1)) * loanamount;

System.out.println(“your monthly EMI is ” + EMI + ” for the amount” + loanamount + ” you have borrowed”);

}

}// end of the Loan abstract class.

Step 4: Create concrete classes that extend the Loan abstract class..

class HomeLoan extends Loan {

public void getInterestRate(double r) {

rate = r;

}

}// End of the HomeLoan class.

class BussinessLoan extends Loan {

public void getInterestRate(double r) {

rate = r;

}

}// End of the BusssinessLoan class.

class EducationLoan extends Loan{

public void getInterestRate(double r){

rate=r;

}

}//End of the EducationLoan class.

Step 5: Create an abstract class (i.e AbstractFactory) to get the factories for Bank and Loan Objects.

abstract class AbstractFactory{

public abstract Bank getBank(String bank);

public abstract Loan getLoan(String loan);

}

Step 6: Create the factory classes that inherit AbstractFactory class to generate the object of concrete class based on given information.

class BankFactory extends AbstractFactory {

public Bank getBank(String bank) {

if(bank == null) {

returnnull;

}

if (bank.equalsIgnoreCase(“HDFC”)) {

returnnewHDFC();

} else if (bank.equalsIgnoreCase(“ICICI”)) {

returnnewICICI();

} else if (bank.equalsIgnoreCase(“SBI”)) {

returnnewSBI();

}

returnnull;

}

public Loan getLoan(String loan) {

returnnull;

}

}// End of the BankFactory class.

class LoanFactory extends AbstractFactory {

public Bank getBank(String bank) {

returnnull;

}

public Loan getLoan(String loan) {

if(loan == null) {

returnnull;

}

if (loan.equalsIgnoreCase(“Home”)) {

returnnewHomeLoan();

} else if (loan.equalsIgnoreCase(“Business”)) {

return new BussinessLoan();

} else if (loan.equalsIgnoreCase(“Education”)) {

return new EducationLoan();

}

returnnull;

}

}

Step 7: Create a FactoryCreator class to get the factories by passing an 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();

}

returnnull;

}

}// End of the FactoryCreator.

Step 8: Use the FactoryCreator to get AbstractFactory in order to get factories of concrete classes by passing an information such as type.

import java.io.*;

class AbstractFactoryPatternExample {

publicstaticvoidmain(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 amount: “);

String bankName = br.readLine();

System.out.print(“\n”);

System.out.print(“Enter the type of loan e.g. home loan or business 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, years);

}

}// End of the AbstractFactoryPatternExample

Popular posts from this blog

How to change column name in Dataframe and selection of few columns in Dataframe using Pyspark with example

What is Garbage collection in Spark and its impact and resolution

Window function in PySpark with Joins example using 2 Dataframes (inner join)