目录
业务代码中,若存在大量无法避免的if...else代码,可以尝试使用JDK8提供的函数式编程包。
设计一个简单的招聘业务:
招聘条件是:男18~60岁,女18~50岁。再根据男女不同年龄段,分配不同的事情任务做。
模拟代码如下
- package cn.daydayup.designmode.factorymode.demov4;
-
-
- /**
- * @author ShangHai
- * @desc
- */
- public class Demo {
-
- /**
- * 模拟招聘业务
- * @param sex 性别
- * @param age 年龄
- * @param name 名字
- */
- public void recruit(String sex, int age, String name){
- if(CommonConstants.MAN.equals(sex)){
- // 男性
- if(age > CommonConstants.SIXTY){
- throw new RuntimeException("年龄不满足");
- } else if(age > CommonConstants.FORTY_FIVE){
- this.doSomeThings001(name);
- } else if(age >= CommonConstants.THIRTY){
- this.doSomeThings002(name);
- } else if(age >= CommonConstants.EIGHTEEN){
- this.doSomeThings003(name);
- } else {
- throw new RuntimeException("年龄不满足");
- }
- } else if(CommonConstants.WOMAN.equals(sex)) {
- // 女性
- if(age > CommonConstants.FIFTY){
- throw new RuntimeException("年龄不满足");
- } else if(age >= CommonConstants.THIRTY){
- this.doSomeThings004(name);
- } else if(age >= CommonConstants.EIGHTEEN){
- this.doSomeThings005(name);
- } else {
- throw new RuntimeException("年龄不满足");
- }
- } else {
- throw new RuntimeException("性别不满足");
- }
- }
-
- private void doSomeThings001(String name){
- System.out.println(name + "[男]年龄45-60");
- }
-
- private void doSomeThings002(String name){
- System.out.println(name + "[男]年龄30-45");
- }
-
- private void doSomeThings003(String name){
- System.out.println(name + "[男]年龄18-30");
- }
-
- private void doSomeThings004(String name){
- System.out.println(name + "[女]年龄30-60");
- }
-
- private void doSomeThings005(String name){
- System.out.println(name + "[女]年龄18-30");
- }
-
- }
常量代码
- package cn.daydayup.designmode.factorymode.demov4;
-
- /**
- * @author ShangHai
- * @desc
- */
- public class CommonConstants {
-
- public final static int ZERO = 0;
-
- public final static int ONE = 1;
-
- public final static int TWO = 2;
-
- public final static int THRRE = 3;
-
- public final static int FOUR = 4;
-
- public final static int FIVE = 5;
-
- public final static int EIGHTEEN = 18;
-
- public final static int THIRTY = 30;
-
- public final static int FORTY_FIVE = 45;
-
- public final static int FIFTY = 50;
-
- public final static int SIXTY = 60;
-
- public final static String MAN = "男";
-
- public final static String WOMAN = "女";
-
- }
main方法执行【控制台:GeGeDa[男]年龄45-60】
- public static void main(String[] args) {
- // 控制台输出: 【GeGeDa[男]年龄45-60】
- new Demo().recruit(CommonConstants.MAN, 49, "GeGeDa");
- }
上述的业务方法中,存在较多的if...else条件语句。
若条件维度根据业务需求增多,if...else语句将会不断叠加,业务代码将变得不再优雅。
分析以上业务代码,可采用Function包中的Consumer
Consumer
Consumer
接口是java.util.function包的其中一个接口, 包含一个核心方法accept(T t),其意义即传入一个参数进行消费,无返回值。
代码测试如下:
package cn.daydayup.designmode.factorymode.demov4.test; import java.util.function.Consumer; /** * @author ShangHai * @desc */ public class TestConsumer { public static void main(String[] args) { Consumerconsumer = a -> { System.out.println("输出参数值:" + a); }; // 传入字符串参数 consumer.accept("aaaa"); } }以上consumer对象声明的方式,等同于
Consumerconsumer = new Consumer () { @Override public void accept(String a) { System.out.println("输出参数值:" + a); } };main方法执行【控制台:输出参数值:aaaa】
ToIntBiFunction
ToIntBiFunction
接口是java.util.function包的其中一个接口, 包含一个核心方法applyAsInt(T t,U u),其意义即传入两个参数进行消费,返回一个int值。
代码测试如下:
package cn.daydayup.designmode.factorymode.demov4.test; import cn.daydayup.designmode.CommonConstant; import cn.daydayup.designmode.factorymode.demov4.CommonConstants; import java.util.function.ToIntBiFunction; /** * @author ShangHai * @desc */ public class TestToIntBiFunction { public static void main(String[] args) { // 传入两个String参数,得到一个int值 ToIntBiFunctiontoIntBiFunction = (a, b) -> { if(a.equals(b)){ return CommonConstants.ONE; } else { return CommonConstants.TWO; } }; // 传入两个字符串参数 int result = toIntBiFunction.applyAsInt("参数1", "参数2"); System.out.println("得到的int值" + result); } }以上toIntBiFunction对象声明的方式,等同于
ToIntBiFunctiontoIntBiFunction = new ToIntBiFunction () { @Override public int applyAsInt(String a, String b) { if(a.equals(b)){ return CommonConstants.ONE; } else { return CommonConstants.TWO; } } };main方法执行【控制台:得到的int值2】
一、if条件&逻辑代码抽离
创建一个策略列表业务执行类RecruitConsumer,
用来存储if标签体内实际的执行方法。
- package cn.daydayup.designmode.factorymode.demov4.function;
-
- import cn.daydayup.designmode.factorymode.demov4.CommonConstants;
-
- import java.util.HashMap;
- import java.util.Map;
- import java.util.function.Consumer;
-
- /**
- * @author ShangHai
- * @desc
- */
- public class RecruitConsumer {
-
- // 策略列表
- public static final Map
> CONSUMER_MAP = new HashMap<>(); -
- static {
- // 根据传入不同的int值,将执行不同方法
- CONSUMER_MAP.put(CommonConstants.ONE, RecruitConsumer::doSomeThings001);
- CONSUMER_MAP.put(CommonConstants.TWO, RecruitConsumer::doSomeThings002);
- CONSUMER_MAP.put(CommonConstants.THRRE, RecruitConsumer::doSomeThings003);
- CONSUMER_MAP.put(CommonConstants.FOUR, RecruitConsumer::doSomeThings004);
- CONSUMER_MAP.put(CommonConstants.FIVE, RecruitConsumer::doSomeThings005);
- }
-
- private static void doSomeThings001(String name){
- System.out.println(name + "[男]年龄45-60");
- }
-
- private static void doSomeThings002(String name){
- System.out.println(name + "[男]年龄30-45");
- }
-
- private static void doSomeThings003(String name){
- System.out.println(name + "[男]年龄18-30");
- }
-
- private static void doSomeThings004(String name){
- System.out.println(name + "[女]年龄30-60");
- }
-
- private static void doSomeThings005(String name){
- System.out.println(name + "[女]年龄18-30");
- }
-
- }
创建一个"条件收集&策略数值"返回类RecruitToIntBiFunction,
用来收集需求下所有的if分支,且根据if满足的条件返回一个策略列表需要的策略int数值。
- package cn.daydayup.designmode.factorymode.demov4.function;
-
- import cn.daydayup.designmode.CommonConstant;
- import cn.daydayup.designmode.factorymode.demov4.CommonConstants;
-
- import java.util.function.ToIntBiFunction;
-
- /**
- * @author ShangHai
- * @desc
- */
- public class RecruitToIntBiFunction implements ToIntBiFunction{
-
- @Override
- public int applyAsInt(Object o, Object o2) {
- if(o==null || o2==null){
- // 返回0,RecruitConsumer策略列表不具备0策略,不执行任何逻辑
- return CommonConstants.ZERO;
- }
-
- String sex = o.toString();
- int age = Integer.parseInt(o2.toString());
- if(CommonConstants.MAN.equals(sex)){
- // 男性
- if(age > CommonConstants.SIXTY){
- // 返回0,RecruitConsumer策略列表不具备0策略,不执行任何逻辑
- return CommonConstants.ZERO;
- } else if(age > CommonConstants.FORTY_FIVE){
- return CommonConstants.ONE;
- } else if(age >= CommonConstants.THIRTY){
- return CommonConstants.TWO;
- } else if(age >= CommonConstants.EIGHTEEN){
- return CommonConstants.THRRE;
- } else {
- // 返回0,RecruitConsumer策略列表不具备0策略,不执行任何逻辑
- return CommonConstants.ZERO;
- }
- } else if(CommonConstants.WOMAN.equals(sex)) {
- // 女性
- if(age > CommonConstants.FIFTY){
- // 返回0,RecruitConsumer策略列表不具备0策略,不执行任何逻辑
- return CommonConstants.ZERO;
- } else if(age >= CommonConstants.THIRTY){
- return CommonConstants.FOUR;
- } else if(age >= CommonConstants.EIGHTEEN){
- return CommonConstants.FIVE;
- } else {
- // 返回0,RecruitConsumer策略列表不具备0策略,不执行任何逻辑
- return CommonConstants.ZERO;
- }
- } else {
- // 返回0,RecruitConsumer策略列表不具备0策略,不执行任何逻辑
- return CommonConstants.ZERO;
- }
- }
-
- }
二、业务逻辑代码改造(最终仅需两行)
- package cn.daydayup.designmode.factorymode.demov4;
-
-
- import cn.daydayup.designmode.factorymode.demov4.function.RecruitConsumer;
- import cn.daydayup.designmode.factorymode.demov4.function.RecruitToIntBiFunction;
-
- /**
- * @author ShangHai
- * @desc
- */
- public class Demo {
-
- /**
- * 模拟招聘业务
- * @param sex 性别
- * @param age 年龄
- * @param name 名字
- */
- public void recruit(String sex, int age, String name){
- // 将if条件所需参数传入,得到策略int数值
- int strategyInt = new RecruitToIntBiFunction().applyAsInt(sex, age);
- // 将策略int数值传入策略列表,consumer会自动执行对应的策略方法
- RecruitConsumer.CONSUMER_MAP.get(strategyInt).accept(name);
- }
-
- public static void main(String[] args) {
- // 控制台输出 ShangHai[男]年龄18-30
- new Demo().recruit(CommonConstants.MAN, 18, "ShangHai");
- }
- }
-
-
-
-
main方法执行【控制台:ShangHai[男]年龄18-30】

业务方法经过"Funtion包与策略模式"改造后,
代码变得简洁;
业务方法不再关注if如何实现,交由定义好的策略消费即可;
if...else分支语句及业务实际执行方法分类被收集,代码整体层次变得规范。