0% found this document useful (0 votes)
67 views26 pages

08-Abstract Factory Pattern - Week6

The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes. It uses factory methods to create related or dependent products without specifying their concrete classes. The key aspects are: (1) An abstract factory interface declares creation methods for each product. (2) Concrete factory classes implement the factory methods to return product objects. (3) Clients call factory methods without knowing the created product classes. This allows clients to work with families of related product objects in a consistent way while separating object creation from use.

Uploaded by

Neha Asim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views26 pages

08-Abstract Factory Pattern - Week6

The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes. It uses factory methods to create related or dependent products without specifying their concrete classes. The key aspects are: (1) An abstract factory interface declares creation methods for each product. (2) Concrete factory classes implement the factory methods to return product objects. (3) Clients call factory methods without knowing the created product classes. This allows clients to work with families of related product objects in a consistent way while separating object creation from use.

Uploaded by

Neha Asim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Abstract Factory

Pattern
Abstract Factory Pattern
• Abstract Factory design pattern is one of the
Creational pattern. Abstract Factory pattern is almost similar
to Factory Pattern is considered as another layer
of abstraction over factory pattern. 
• Abstract Factory patterns work around a super-factory which
creates other factories.
Advantage and Usage

• Abstract Factory Pattern isolates the client code from concrete


(implementation) classes.
• Used 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.
• 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.
Example
Elements of Abstract Factory
Pattern
• AbstractFactory - an interface that is used to create concrete
factories.
• Client interact with this interface to access instances of
ConcreteFactory concrete subclasses.
• ConcreteFactory - A subclass of the Abstract factory, these
concrete classes implement the operations/methods which
are used to create concrete product objects.
• AbstractProduct - The AbstractProduct class is used to declare
an interface for a specific type of product object.
• It return the product subclass in relation to the ConcreteFactory
which is currently being used to handle product instantiation.
Elements of Abstract Factory
Pattern
• ConcreteProduct - The concrete product is provided by it’s
ConcreteFactory and defines the individual implementation
details for the specified product through the implementation
of its AbstractProduct interface.
• Client - Only uses and has access to the AbstractFactory and
AbstractProduct classes, making it completely unaware of the
implementation details of the system.
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;  
         public HDFC(){  
                BNAME="HDFC BANK";  
        }  
        public String getBankName() {  
                  return BNAME;  
        }  
}  
Step 2:
class ICICI implements Bank{  
       private final String BNAME;  
       ICICI(){  
                BNAME="ICICI BANK";  
        }  
        public String getBankName() {  
                  return BNAME;  
       }  
}  
Step 2:
class SBI implements Bank{  
      private final String BNAME;  
      public SBI(){  
                BNAME="SBI BANK";  
        }  
       public String getBankName(){  
                  return BNAME;  
       }  
}  
Step 3: Create the Loan abstract class.
abstract class Loan{  
   protected double rate;  
   abstract void getInterestRate(double rate);  
   public void calculateLoanPayment(double loanamount, int years)  
   {  
                       
         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 ha
ve 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;  
    }  

class BussinessLoan extends Loan{  
    public void getInterestRate(double r){  
          rate=r;  
     }  
  
}  
class EducationLoan extends Loan{  
     public void getInterestRate(double r){  
       rate=r;  
 }  
}
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){  
         return null;  
      }  
      if(bank.equalsIgnoreCase("HDFC")){  
         return new HDFC();  
      } else if(bank.equalsIgnoreCase("ICICI")){  
         return new ICICI();  
      } else if(bank.equalsIgnoreCase("SBI")){  
         return new SBI();  
      }  
      return null;  
   }  
  public Loan getLoan(String loan) {  
      return null;  
   }  
}//End of the BankFactory class. 
Step 6:
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;  
   }  
     
}  
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();  
      }  
      return null;  
   }  
• }//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 {  
      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("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);  
  
Step 8
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 amou
nt: ");  
int years=Integer.parseInt(br.readLine());  
  
 
Step 8
System.out.print("\n");  
System.out.println("you are taking the loan from "+ b.getBankNa
me());  
  
AbstractFactory loanFactory = FactoryCreator.getFactory("Loan")
;  
           Loan l=loanFactory.getLoan(loanName);  
           l.getInterestRate(rate);  
           l.calculateLoanPayment(loanAmount,years);  
  }  
}//End of the  AbstractFactoryPatternExample 

You might also like