Java使用函数式接口书写if-else

前言

        在工作开发中,会遇到很多如业务类型、状态、渠道等会很多if-else的情况。可能一个类里面写了很多if-else,代码阅读可能增加很多难度,同样也不雅观。

方法

        我们可以采用多种方法比如switch方法、函数式接口、策略模式来优化if-else方法,今天主要分享函数式接口的方式。

函数式接口

当我们遇到复杂的处理逻辑,可以将这些逻辑抽出来处理,增加整体的可读性和解耦性,利用函数式接口来处理if-else的模式,也可以用策略模式,他们两个的实现逻辑差不多,策略模式自行百度这里就不做分享了。

函数式接口选择

函数式接口方法说明调用方法
Function有参数,有返回值apply
Consumer有参数,无返回值accept
Supplier无参数,有返回值get
Runnable无参数,无返回值run

@PostConstruct注解

        Java自带的注解,在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法。

示例

@Component
public class TestFunctionInterfaceStrategy {
    /**
     * Map-key:方法标识
     *
     * Function-T 方法参数
     *
     * Function-R 方法返回值
     *
     */
    private Map<String, Function<String,String>> methodMap;

    @PostConstruct
    private void init(){
        methodMap = new HashMap<>(4);
        methodMap.put("1",this::businessMethod1);
        methodMap.put("2",this::businessMethod2);
        methodMap.put("3",this::businessMethod3);
        methodMap.put("4",this::businessMethod4);
    }

    /**
     * 通过这个方法调用
     * @param businessParams  Map-key
     * @param params 方法参数
     * @return 返回值
     */
    public String doMethod(String businessParams,String params){
        return methodMap.get(businessParams).apply(params);
    }

    private String businessMethod1(String parameter){
        return "业务方法一"+parameter;
    }
    private String businessMethod2(String parameter){
        return "业务方法二"+parameter;
    }
    private String businessMethod3(String parameter){
        return "业务方法三"+parameter;
    }
    private String businessMethod4(String parameter){
        return "业务方法四"+parameter;
    }

}

方法测试:

@SpringBootTest
@RunWith(SpringRunner.class)
public class FunctionTest {
    @Resource
    private TestFunctionInterfaceStrategy testFunctionInterfaceStrategy;
    @Test
    public void Test() {
        String s = testFunctionInterfaceStrategy.doMethod("1", "====调用成功!====");
        System.out.println(s);
    }

}

函数式接口可以帮助解决if-else的问题。在Java中,函数式接口是指只包含一个抽象方法的接口。通过使用lambda表达式或方法引用,可以将代码逻辑作为参数传递给函数式接口的方法,从而避免使用if-else语句。 以下是一个简单的示例,演示如何使用函数式接口来处理if-else逻辑: ```java @FunctionalInterface interface Action { void perform(String input); } class Processor { public static void process(String input, Action action) { action.perform(input); } } public class Main { public static void main(String[] args) { String input = "example"; Processor.process(input, (str) -> { if (str.equals("example")) { System.out.println("Do something for example"); } else if (str.equals("another example")) { System.out.println("Do something for another example"); } else { System.out.println("Do something else"); } }); } } ``` 在上面的示例中,我们定义了一个函数式接口`Action`,它有一个抽象方法`perform`,该方法接受一个String类型的参数。`Processor`类中的`process`方法接受一个`Action`对象作为参数,并调用其`perform`方法。 在`main`方法中,我们使用lambda表达式作为参数传递给`process`方法。根据输入的不同,我们可以定义不同的代码逻辑来执行相应的操作。这样,我们就可以避免使用冗长的if-else语句。 通过使用函数式接口,我们可以将代码逻辑作为参数传递,实现更灵活和可扩展的解决方案。同时,它还提高了代码的可读性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值