To advertise contact on Whatsapp: +923041280395 For Backlinks & Guest Posts Email at: [email protected]

Abstract Factory Design Pattern

Abstract Factory Design Pattern

Abstract Factory Design Pattern

Abstract Factory Design  Pattern says that only an interface or abstract class can be specified to construct families of connected (or dependent) objects, without defining their particular subclasses. That is why Abstract Model Factory is one level higher than the Model Factory.

The Kit is also regarded as an Abstract Factory design. 

Advantages of Abstract Factory Design: 

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

Uses of Abstract Factory pattern: 

  • When it comes to making, writing and representing the device independently of its source. 
  • Such limitation must be applied when the class of associated objects is to be used together. 
  • If you wish to supply an object library that shows no implementations and only interfaces. 
  • If the system needs one of several families of objects to be installed. 

Deployment: 

  • We will be building a bank Interface, an abstract loan class and its subclasses. 
  • Then as a next step we construct AbstractFactory class. 
  • Afterwards, the BankFactory and LoanFactory classes are developed that expand the abstractFactory class. 
  • Following this, the FaktoryCreator class uses AbstractFactoryPatternExample to get an AbstractFactory object class. 
  • We will be building a bank Interface, an abstract loan class and its subclasses. 
  • Then as a next step we construct AbstractFactory class. 
  • Afterwards, the BankFactory and LoanFactory classes are developed that expand the abstractFactory class. 
  • Following this, the FaktoryCreator class uses AbstractFactoryPatternExample to get an AbstractFactory object class. 
  • See the following diagram attentively: 
abstract factory design pattern

You may be interested in Java Interview Questions.

Example of Abstract Factory Method: 

We are calculating the loan for various banks like, HBL, ABL, SBL, etc, 

Phase-1: create the bank interface. 

import java.io.*;      

interface Bank{   

        String getBankName();   

}   

Phase-2: Create specific classes to use the interface of the bank. 

class HBL implements Bank{   

         private final String BNAME;   

         public HBL(){   

                BNAME="HBL BANK";   

        }   

        public String getBankName() {   

                  return BNAME;   

        }   

}   

 class ABL implements Bank{   

       private final String BNAME;   

       ABL(){   

                BNAME="ABL BANK";   

        }   

        public String getBankName() {   

                  return BNAME;   

       }   

}   

class SBL implements Bank{   

      private final String BNAME;   

      public SBL(){   

                BNAME="SBL BANK";   

        }   

       public String getBankName(){   

                  return BNAME;   

       }   

}   

 Phase-3: Create the Loan abstract class. 

abstract class Loan{   

   protected double rate;   

   abstract void getInterestRate(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;  

  

            */   

                 

         double EMI;   

         int n;   

   

         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.   

Phase-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.   

Phase-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);   

}   

Phase-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){   

         return null;   

      }   

      if(bank.equalsIgnoreCase("HBL")){   

         return new HBL();   

      } else if(bank.equalsIgnoreCase("ABL")){   

         return new ABL();   

      } else if(bank.equalsIgnoreCase("SBL")){   

         return new SBL();   

      }   

      return null;   

   }   

  public Loan getLoan(String loan) {   

      return null;   

   }   

}//End of the BankFactory class.   

class LoanFactory extends AbstractFactory{   

           public Bank getBank(String bank){   

                return null;   

          }   

         

     public Loan getLoan(String loan){   

      if(loan == null){   

         return null;   

      }   

      if(loan.equalsIgnoreCase("Home")){   

         return new HomeLoan();   

      } else if(loan.equalsIgnoreCase("Business")){   

         return new BussinessLoan();   

      } else if(loan.equalsIgnoreCase("Education")){   

         return new EducationLoan();   

      }   

      return null;   

   }   

      

}   

Phase-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;   

   }   

}//End of the FactoryCreator. 

Phase-8: Use the FactoryCreator to get AbstractFactory in order to get factories of concrete classes by passing a piece of information such as type. 

import java.io.*;   

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 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    

Draw backs of Abstract Factory Design Pattern:  

In the circumstances mentioned below, Abstract Factory design patterns should be used carefully. 

  • Don’t use it if logically not connected to the underlying object factories 
  • Don’t use it if the object underlying factories have different purposes for objects 
  • Don’t use it when there are very different attributes within the object factory. 

In addition to the scenarios mentioned above, the abstract factory model can also increase object complexity as multiple abstract methods are implemented, whereas sub-classes only use selected methods. It’s hard to use the Abstract Factory style template in such scenarios. 

Conclusion of Abstract Factory Design Pattern: 

In scenarios where there are a number of similar object classes, the Abstract Factory Design pattern is useful. To order to simplify object development, these classes require an abstract layer. The emphasis is primarily on establishing this abstract class level in the Abstract Factory Model pattern. This article explained how the abstract concept of a factory is implemented in a practical worst. 



Leave a Reply

Your email address will not be published. Required fields are marked *