在我们之前的讨论中,我们探索了工厂方法模式——一种简化单一产品创建的设计模式。现在,我们将视角转向抽象工厂模式,这是一种更高层次的设计模式,用于管理和创建一系列相关的产品。
抽象工厂模式是创建型设计模式的一个高级形式,它允许生成一系列相关或相互依赖的对象。与工厂方法模式相比,抽象工厂模式在创建多种相关产品时提供了更大的灵活性和扩展性。
通过定义一个中心化的工厂类接口来创建一组相关或相互依赖的对象,而具体的创建逻辑由实现该接口的具体工厂类完成。
public interface Bank {
String getBankName();
}
步骤 2: 实现银行接口的具体类
class HDFC implements Bank{
private final String BNAME;
public HDFC(){
BNAME="HDFC BANK";
}
@Override
public String getBankName() {
return BNAME;
}
}
class ICICI implements Bank{
private final String BNAME;
ICICI(){
BNAME="ICICI BANK";
}
@Override
public String getBankName() {
return BNAME;
}
}
class SBI implements Bank{
private final String BNAME;
public SBI(){
BNAME="SBI BANK";
}
@Override
public String getBankName(){
return BNAME;
}
}
步骤 3: 创建贷款抽象类
public 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("每月等额还款:"+ EMI +" 贷款总额:"+loanamount+"");
}
}
步骤 4: 继承贷款抽象类的具体类
//家庭贷款
class HomeLoan extends Loan {
@Override
public void getInterestRate(double r){
rate=r;
}
}
//商业贷款
class BussinessLoan extends Loan{
@Override
public void getInterestRate(double r){
rate=r;
}
}
//教育贷款
class EducationLoan extends Loan{
@Override
public void getInterestRate(double r){
rate=r;
}
}
步骤 5: 创建抽象工厂类
public abstract class AbstractFactory {
public abstract Bank getBank(String bank);
public abstract Loan getLoan(String loan);
}
步骤 6: 创建继承AbstractFactory类的工厂类,根据给定的信息生成具体类的对象。
public class BankFactory extends AbstractFactory{
@Override
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;
}
@Override
public Loan getLoan(String loan) {
return null;
}
}
public class LoanFactory extends AbstractFactory{
@Override
public Bank getBank(String bank){
return null;
}
@Override
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;
}
}
**步骤 7:**创建一个FactoryCreator类,通过传递Bank或Loan之类的信息来获取工厂。
public 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:**使用FactoryCreator获取AbstractFactory,以便通过传递类型等信息来获取具体类的工厂。
public class AbstractFactoryPatternExample {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("输入你想贷款银行的名称: ");
String bankName = br.readLine();
System.out.print("\n");
System.out.print("输入你想贷款的类型: ");
String loanName = br.readLine();
AbstractFactory bankFactory = FactoryCreator.getFactory("Bank");
Bank b = bankFactory.getBank(bankName);
System.out.print("\n");
System.out.print("输入利率 " + b.getBankName() + ": ");
double rate = Double.parseDouble(br.readLine());
System.out.print("\n");
System.out.print("输入贷款金额: ");
double loanAmount = Double.parseDouble(br.readLine());
System.out.print("\n");
System.out.print("输入贷款年限: ");
int years = Integer.parseInt(br.readLine());
System.out.print("\n");
System.out.println("你正在从银行贷款 " + b.getBankName());
AbstractFactory loanFactory = FactoryCreator.getFactory("Loan");
Loan l = loanFactory.getLoan(loanName);
l.getInterestRate(rate);
l.calculateLoanPayment(loanAmount, years);
}
}
以上就是一个简单的抽象工厂示例代码,运行代码我们可以看到:
输入相应的数据就可以算出不同银行,不同贷款类型下具体贷款明细了。
23种设计模式相关代码后续会逐步提交到github上,方便学习,欢迎指点:
代码地址
https://github.com/RuofeiSun/lf-23Pattern