妙用Java 8中的 Function接口,消灭if…else(非常新颖的写法)

Java学习者社区 276 0

一、理论知识

  • Function 函数式接口
    • Supplier供给型函数
    • Consumer消费型函数
    • Runnable无参无返回型函数
    • Function函数的表现形式为接收一个参数,并返回一个值。SupplierConsumerRunnable可以看作Function的一种特殊表现形式
  • 使用小技巧
    • 处理抛出异常的if
    • 处理if分支操作
    • 如果存在值执行消费操作,否则执行基于空的操作

在开发过程中经常会使用if...else...进行判断抛出异常、分支处理等操作。这些if...else...充斥在代码中严重影响了代码代码的美观,这时我们可以利用Java 8Function接口来消灭if...else...

Function 函数式接口

使用注解@FunctionalInterface标识,并且只包含一个抽象方法的接口是函数式接口函数式接口主要分为Supplier供给型函数、Consumer消费型函数、Runnable无参无返回型函数和Function有参有返回型函数。

Function可以看作转换型函数

Supplier供给型函数

Supplier的表现形式为不接受参数、只返回数据

Consumer消费型函数

Consumer消费型函数和Supplier刚好相反。Consumer接收一个参数,没有返回值

Runnable无参无返回型函数

Runnable的表现形式为即没有参数也没有返回值

Function函数的表现形式为接收一个参数,并返回一个值。SupplierConsumerRunnable可以看作Function的一种特殊表现形式

二、代码实操

1.异常的if

处理抛出异常的if

  1. 定义函数

定义一个抛出异常的形式的函数式接口, 这个接口只有参数没有返回值是个消费型接口

/**
 * 抛异常接口
 **/
@FunctionalInterface
public interface ThrowExceptionFunction {

    /**
     * 抛出异常信息
     *
     * @param message 异常信息
     * @return void
     **/
    void throwMessage(String message);
}
  1. 编写判断方法

创建工具类VUtils并创建一个isTure方法,方法的返回值为刚才定义的函数式接口-ThrowExceptionFunctionThrowExceptionFunction的接口实现逻辑为当参数btrue时抛出异常

/**
 * @author DBC
 * @date 2023/11/6 14:28
 * @network dbc655.top
 */
public class VUtils {
    /**
     *  如果参数为true抛出异常
     *
     * @param b
     * @return com.example.demo.func.ThrowExceptionFunction
     **/
    public static ThrowExceptionFunction isTure(boolean b){

        return (errorMessage) -> {
            if (!b){
                throw new RuntimeException(errorMessage);
            }
        };
    }
  1. 使用方式

调用工具类参数参数后,调用函数式接口throwMessage方法传入异常信息。当出入的参数为true时正常执行

    public static void main(String[] args) {
        VUtils.isTure(true).throwMessage("我不会被执行到");

        VUtils.isTure(false).throwMessage("我会被执行到!");
    }

妙用Java 8中的 Function接口,消灭if…else(非常新颖的写法)插图

2.if分支操作

处理if分支操作

  1. 定义函数式接口

创建一个名为BranchHandle的函数式接口,接口的参数为两个Runnable接口。这两个两个Runnable接口分别代表了为truefalse时要进行的操作

/**
 * @author DBC
 * @date 2023/11/6 15:11
 * @network dbc655.top
 */
/**
 * 分支处理接口
 **/
@FunctionalInterface
public interface BranchHandle {

    /**
     * 分支操作
     *
     * @param trueHandle 为true时要进行的操作
     * @param falseHandle 为false时要进行的操作
     * @return void
     **/
    void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);

}

方法使用相关

   /**
     * 参数为true或false时,分别进行不同的操作
     *
     * @param b
     * @return com.example.demo.func.BranchHandle
     **/
    public static BranchHandle isTureOrFalse(boolean b){

        return (trueHandle, falseHandle) -> {
            if (b){
                trueHandle.run();
            } else {
                falseHandle.run();
            }
        };
    }

    public static void test(){
        System.out.println("test");
    }

    public static void main(String[] args) {

        VUtils.isTureOrFalse(true)
                .trueOrFalseHandle(VUtils::test,
                        () -> System.out.println("false"));
    }
温馨提示

虽然有点脱裤子放屁,但是看起来有点意思[aru_1]

发表评论 取消回复
表情 图片 链接 代码

分享