Java抽象工厂模式

1 Java抽象工厂模式的介绍

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

2 Java抽象工厂模式的好处

  • 抽象工厂模式将客户代码与具体(实现)类隔离。
  • 它简化了对象族的交换。
  • 它促进对象之间的一致性。

3 Java抽象工厂模式的用法

  • 系统何时需要独立于其对象的创建,组成和表示方式。
  • 当相关对象系列必须一起使用时,则需要强制执行此约束。
  • 当您想提供一个对象库时,它不显示实现,而只显示接口。
  • 当系统需要配置多个对象系列之一时。

4 Java抽象工厂模式的UML

  • 我们将创建一个Bank接口和一个Loan抽象类及其子类。
  • 然后,我们将创建AbstractFactory类作为下一步。
  • 然后,我们将创建具体的类BankFactory和LoanFactory来扩展AbstractFactory类
  • 之后,AbstractFactoryPatternExample类使用FactoryCreator来获取AbstractFactory类的对象。

UML图如下:

5 Java抽象工厂模式的案例

以下案例模拟计算HDFC,ICICI,SBI等不同银行的贷款偿还额。

5.1 建立Band接口

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.io.*;
interface Bank{  
        String getBankName();  
}  

5.2 创建实现Bank接口的具体类

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
class HDFC implements Bank{
         private final String BNAME;  
         public HDFC(){  
                BNAME="HDFC BANK";  
        }  
        public String getBankName() {  
                  return BNAME;  
        }  
}  
package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
class ICICI implements Bank{
       private final String BNAME;  
       ICICI(){  
                BNAME="ICICI BANK";  
        }  
        public String getBankName() {  
                  return BNAME;  
       }  
}  
package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
class SBI implements Bank{
      private final String BNAME;  
      public SBI(){  
                BNAME="SBI BANK";  
        }  
       public String getBankName(){  
                  return BNAME;  
       }  
}  

5.3 创建Loan抽象类

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
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 have borrowed");
 }  
}

5.4 创建扩展Loan抽象类的具体类

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
class HomeLoan extends Loan{
     public void getInterestRate(double r){  
         rate=r;  
    }  
}
package com.yiidian;

class EducationLoan extends Loan{
     public void getInterestRate(double r){  
       rate=r;  
 }  
}
package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
class BussinessLoan extends Loan{
    public void getInterestRate(double r){  
          rate=r;  
     }  
  
}

5.5 创建抽象类AbstractFactory

package com.yiidian;

abstract class AbstractFactory{
  public abstract Bank getBank(String bank);  
  public abstract Loan getLoan(String loan);  
} 

5.6 创建继承AbstractFactory类的工厂类

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
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;  
   }  
}
package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
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;  
   }  
     
}  

5.7 创建FactoryCreator类

package com.yiidian;

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

5.8 使用FactoryCreator来获取AbstractFactory

package com.yiidian;

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

上面代码的输出结果为:

热门文章

优秀文章