ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
策略模式在Thread和Runnable中的应用分析 这里以实现个人所得税的计算功能为例,一步步来模拟Runnable的样子来优化代码: 模板方法 ```java /** * @program: ThreadDemo * @description: 计算税率:工资*0.1 + 奖金*0.15 * @author: hs96.cn@Gmail.com * @create: 2020-08-28 */ public class TaxCalculatorMain { public static void main(String[] args) { TaxCalculator taxCalculator = new TaxCalculator(10000d, 2000d) { @Override protected double calcTax() { return getSalary() * 0.1 + getBonus() * 0.15; } }; double tax = taxCalculator.calculate(); System.out.println(tax); } } ``` ```java /** * @program: ThreadDemo * @description: 个人所得税计算器 * @author: hs96.cn@Gmail.com * @create: 2020-08-28 */ public class TaxCalculator { // 薪水 private final double salary; // 奖金 private final double bonus; public TaxCalculator(double salary, double bonus) { this.salary = salary; this.bonus = bonus; } public double getSalary() { return salary; } public double getBonus() { return bonus; } protected double calcTax() { return 0.00; } public double calculate() { return calcTax(); } } ``` calculate可以理解为Thread的start方法,他return calcTax();可以理解为start()调用Runnable的run方法。 这样实现个人所得税计算器是没问题的,但是如果在真实的商业应用中就得去改对应界面的代码,这从设计开闭原则来说就不符合了,所以这里抽象一个计算税率的接口出来专门来做这个计算,改造代码如下: 首先我们抽象一个计算税率的接口出来专门来做计算: ```java @FunctionalInterface public interface CalculatorStrategy { /** * * @param salary 薪水 * @param bonus 奖金 * @return 税收 */ double calculate(double salary, double bonus); } ``` 接下来我们把具体实现流程放在实现类中: ```java /** * @program: ThreadDemo * @description: 计算税率:工资*0.1 + 奖金*0.15 * @author: hs96.cn@Gmail.com * @create: 2020-08-28 */ public class SimpleCalculatorStrategy implements CalculatorStrategy { private final static double SALARY_RATE = 0.1; private final static double BONUS_RATE = 0.15; @Override public double calculate(double salary, double bonus) { return salary * SALARY_RATE + bonus * BONUS_RATE; } } ``` 修改main方法如下: ```java /** * @program: ThreadDemo * @description: 计算税率:工资*0.1 + 奖金*0.15 * @author: hs96.cn@Gmail.com * @create: 2020-08-28 */ public class TaxCalculatorMain { public static void main(String[] args) { //使用摸板方法 /*TaxCalculator taxCalculator = new TaxCalculator(10000d, 2000d) { @Override protected double calcTax() { return getSalary() * 0.1 + getBonus() * 0.15; } }; double tax = taxCalculator.calculate(); System.out.println(tax);*/ //使用策略模式 CalculatorStrategy strategy = new SimpleCalculatorStrategy(); TaxCalculator calculator = new TaxCalculator(10000d, 2000d,strategy); double tex = calculator.calculate(); System.out.println(tex); } } ``` 计算调用算法的类如下: ```java /** * @program: ThreadDemo * @description: 个人所得税计算器 * @author: hs96.cn@Gmail.com * @create: 2020-08-28 */ public class TaxCalculator { // 薪水 private final double salary; // 奖金 private final double bonus; //计算策略类 private CalculatorStrategy calculatorStrategy; public TaxCalculator(double salary, double bonus, CalculatorStrategy calculatorStrategy) { this.salary = salary; this.bonus = bonus; this.calculatorStrategy = calculatorStrategy; } public double getSalary() { return salary; } public double getBonus() { return bonus; } protected double calcTax() { return calculatorStrategy.calculate(salary, bonus); } public double calculate() { return calcTax(); } } ``` 这里其实主要增加了 CalculatorStrategy对象和calcTax的实现调用了实现算法接口的实现类。 main方法的CalculatorStrategy 就相当于Thread的Runnable 接下来我们加入java8的Lambda表达式来精简一下代码: ```java public class TaxCalculatorMain { public static void main(String[] args) { //使用java8的Lambda表达式 TaxCalculator calculator = new TaxCalculator(10000d, 2000d, (s, b) -> s * 0.1 + b * 0.15); System.out.println(calculator.calculate()); } } ``` 可以看到代码精简很多。