这就意味着客户端在计算预期收益时要根据这一状态执行不同的算法。
通过分析,重构3:通过简单工厂返回具体算法类。
预期收益接口
public interface Interest {
/**
* 预期收益
*
* @param invest 出借金额
* @param yearRate 年化率
* @param month 出借期限
* @return
*/
double getInterestCount(double invest, double yearRate, int month);
}
预期收益工厂
public interface InterestFactory {
/**
* 根据还款方式 创建预期收益算法类
*
* @param payMethod 还款方式
* @return
*/
Interest getInterest(int payMethod);
}
第一种算法(到期本息 、先息后本)
public class MaturityInterest implements Interest{
@Override
public double getInterestCount(double invest, double yearRate, int month) {
BigDecimal bigDecimal = new BigDecimal(invest);
BigDecimal bigDecimal12 = new BigDecimal(12);
BigDecimal bigDecimalcount = new BigDecimal(month);
BigDecimal multiply1 = bigDecimal.multiply(new BigDecimal(yearRate));//总收入
BigDecimal divide = multiply1.divide(bigDecimal12, 4, BigDecimal.ROUND_HALF_EVEN); //单月收入保留4位
BigDecimal bigDecimal1 = divide.setScale(3, BigDecimal.ROUND_HALF_EVEN);//单月收入保留3位
BigDecimal multiply = bigDecimal1.multiply(bigDecimalcount); //预期收入
return multiply.doubleValue();
}
}
第二种算法(等额本息)
public class PrincipalInterest implements Interest {
@Override
public double getInterestCount(double invest, double yearRate, int month) {
double monthRate = yearRate / 12; //月利率
double monthInterest = 0;
double capital = invest;
double tmpCapital = 0;
double totalInterest = 0;
for (int i = 1; i < month + 1; i++) {
capital = capital - tmpCapital;
monthInterest = capital * monthRate;
tmpCapital = (invest * monthRate * (Math.pow((1 + monthRate), i - 1))) / (Math.pow(1 + monthRate, month) - 1);
System.out.println("第" + i + "月利息: " + monthInterest);
totalInterest = totalInterest + monthInterest;
}
return totalInterest;
}
}
第三种算法(等额本金)
public class CapitalInterest implements Interest {
@Override
public double getInterestCount(double invest, double yearRate, int month) {
double monthRate = yearRate / 12;
// 每月本息金额 = (贷款本金÷还款月数) + (贷款本金-已归还本金累计额)×月利率
// 每月本金 = 贷款本金÷还款月数
// 每月利息 = (贷款本金-已归还本金累计额)×月利率
double monthCapital = 0;
double tmpCapital = 0;
double monthInterest = 0;
double countInterest = 0;
for (int i = 1; i < month + 1; i++) {
monthCapital = (invest / month) + (invest - tmpCapital) * monthRate;
monthInterest = (invest - tmpCapital) * monthRate;
tmpCapital = tmpCapital + (invest / month);
countInterest += monthInterest;
System.out.println("第" + i + "月本息: " + monthCapital + ",本金:" + (invest / month) + ",利息:" + monthInterest);
}
return countInterest;
}
}
具体工厂
public class BidInterestFactory implements InterestFactory {
@Override
public Interest getInterest(int payMethod) {
if (payMethod == 1 || payMethod == 2) { //到期本息 先息后本
return new MaturityInterest();
}
if (payMethod == 3) { //等额本息
return new PrincipalInterest();
}
if (payMethod == 4) { //等额本金
return new CapitalInterest();
}
return new NullInterset();
}
/**
* 空算法
*/
public class NullInterset implements Interest {
@Override
public double getInterestCount(double invest, double yearRate, int month) {
return 0;
}
}
}
使用
if (interest == null) {
interest = new BidInterestFactory().getInterest(bidInfo.getPayMethod());
}
double interestCount = interest.getInterestCount(Double.valueOf(editable.toString()), rate / 100, bidInfo.getPeroidCount());
moneyTipTextView.setText("预期收入" + MoneyUtil.formatDouble(interestCount) + "元");
重构就是对软件内部结构的一种调整,目的是不改变软件可观察行为的前提下,提高其可理解性,降低其可修改成本。