/** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 模板方法模式 Template Method Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Account.java * * */ package com.javapatterns.templatemethod; /** *抽象类 账号 * @author geovindu * * */ abstract public class Account { /** * * * */ protected String accountNumber; /** * * * */ public Account() { accountNumber = null; } /** * * * */ public Account(String accountNumber) { this.accountNumber = accountNumber; } /** * 模板方法,计算利息数据数额 * @return 返回利息数额 * */ final public double calculateInterest() { double interestRate = doCalculateInterestRate(); String accountType = doCalculateAccountType(); double amount = calculateAmount(accountType, accountNumber); return amount * interestRate; } /** *基本方法留给子类实现 *账户类型 * @return * */ abstract protected String doCalculateAccountType() ; /** *基本方法留给子类实现 * @return * */ abstract protected double doCalculateInterestRate() ; /** *基本方法已经实现 * @param accountType 输入类型 * @param accountNumber 数字类型 * @return * */ final public double calculateAmount(String accountType, String accountNumber) { this.accountNumber=accountNumber; double rs=3.00D; if(accountType=="Certificate of Deposite" && accountNumber=="1") { rs=8888.00D; } if(accountType=="Certificate of Deposite" && accountNumber=="2") { rs=100000.00D; } if(accountType=="Money Market" && accountNumber=="1") { rs=8888.00D; } if(accountType=="Money Market" && accountNumber=="2") { rs=100000.00D; } return rs; } }
/** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 模板方法模式 Template Method Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc MoneyMarketAccount.java * * */ package com.javapatterns.templatemethod; /** *货币市场账号 * @author geovindu * * */ public class MoneyMarketAccount extends Account { /** *基本方法这里实现 * * @return * */ public String doCalculateAccountType() { return "Money Market"; } /** *基本方法这里实现 *利率 * @return * */ public double doCalculateInterestRate() { return 0.045D; } }
/** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 模板方法模式 Template Method Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc CDAccount.java * * */ package com.javapatterns.templatemethod; /** *具体子类 定期存款账号 * @author geovindu * */ public class CDAccount extends Account { /** *基本方法这里实现 * @return * */ public String doCalculateAccountType() { return "Certificate of Deposite"; } /** *基本方法这里实现 *利率 * @return * */ public double doCalculateInterestRate() { return 0.065D; } }
调用测试:
//模板方法模式 Account acct = null; acct = new MoneyMarketAccount(); acct.calculateAmount("Money Market","1"); System.out.println("Amount: 8888.00DInterest earned from Money Market account = " + acct.calculateInterest()); acct = new CDAccount(); acct.calculateAmount("Certificate of Deposite","1"); System.out.println("Amount: 8888.00D Interest earned from CD account = " + acct.calculateInterest()); acct = new MoneyMarketAccount(); acct.calculateAmount("Money Market","2"); System.out.println("Amount: 100000.00D Interest earned from Money Market account = " + acct.calculateInterest()); acct = new CDAccount(); acct.calculateAmount("Certificate of Deposite","2"); System.out.println("Amount: 100000.00D Interest earned from CD account = " + acct.calculateInterest());
输出:
Amount: 8888.00DInterest earned from Money Market account = 399.96 Amount: 8888.00D Interest earned from CD account = 577.72 Amount: 100000.00D Interest earned from Money Market account = 4500.0 Amount: 100000.00D Interest earned from CD account = 6500.0
标签:12,return,accountNumber,09,Patterns,2022,acct,Java,Method From: https://www.cnblogs.com/geovindu/p/16724537.html