妙用Java 8中的 Function接口 消滅if...else(非常新穎的寫法)
來源:juejin.cn/post/7011435192803917831
在開發(fā)過程中經(jīng)常會使用if...else...進行判斷拋出異常、分支處理等操作。這些if...else...充斥在代碼中嚴重影響了代碼代碼的美觀,這時我們可以利用Java 8的Function接口來消滅if...else...。
if (...){throw new RuntimeException("出現(xiàn)異常了");}if (...){doSomething();} else {doOther();}
# Function 函數(shù)式接口
使用注解@FunctionalInterface標識,并且只包含一個抽象方法的接口是函數(shù)式接口。函數(shù)式接口主要分為Supplier供給型函數(shù)、Consumer消費型函數(shù)、Runnable無參無返回型函數(shù)和Function有參有返回型函數(shù)。
Function可以看作轉(zhuǎn)換型函數(shù)
# Supplier供給型函數(shù)
Supplier的表現(xiàn)形式為不接受參數(shù)、只返回數(shù)據(jù)
# Consumer消費型函數(shù)
Consumer消費型函數(shù)和Supplier剛好相反。Consumer接收一個參數(shù),沒有返回值
# Runnable無參無返回型函數(shù)
Runnable的表現(xiàn)形式為即沒有參數(shù)也沒有返回值
Function函數(shù)的表現(xiàn)形式為接收一個參數(shù),并返回一個值。Supplier、Consumer和Runnable可以看作Function的一種特殊表現(xiàn)形式
# 使用小技巧
處理拋出異常的if
1.定義函數(shù)
定義一個拋出異常的形式的函數(shù)式接口, 這個接口只有參數(shù)沒有返回值是個消費型接口
/*** 拋異常接口**/public interface ThrowExceptionFunction {/*** 拋出異常信息** @param message 異常信息* @return void**/void throwMessage(String message);}
2.編寫判斷方法
創(chuàng)建工具類VUtils并創(chuàng)建一個isTure方法,方法的返回值為剛才定義的函數(shù)式接口-ThrowExceptionFunction。ThrowExceptionFunction的接口實現(xiàn)邏輯為當參數(shù)b為true時拋出異常
/*** 如果參數(shù)為true拋出異常** @param b* @return com.example.demo.func.ThrowExceptionFunction**/public static ThrowExceptionFunction isTure(boolean b){return (errorMessage) -> {if (b){throw new RuntimeException(errorMessage);}};}
3.使用方式
調(diào)用工具類參數(shù)參數(shù)后,調(diào)用函數(shù)式接口的throwMessage方法傳入異常信息。
當出入的參數(shù)為false時正常執(zhí)行
當出入的參數(shù)為true時拋出異常
處理if分支操作
1.定義函數(shù)式接口
創(chuàng)建一個名為BranchHandle的函數(shù)式接口,接口的參數(shù)為兩個Runnable接口。這兩個兩個Runnable接口分別代表了為true或false時要進行的操作
/*** 分支處理接口**/public interface BranchHandle {/*** 分支操作** @param trueHandle 為true時要進行的操作* @param falseHandle 為false時要進行的操作* @return void**/void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle);}
2.編寫判斷方法
創(chuàng)建一個名為isTureOrFalse的方法,方法的返回值為剛才定義的函數(shù)式接口-BranchHandle。
/*** 參數(shù)為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();}};}
3.使用方式
參數(shù)為true時,執(zhí)行trueHandle
參數(shù)為false時,執(zhí)行falseHandle
如果存在值執(zhí)行消費操作,否則執(zhí)行基于空的操作
1.定義函數(shù)
創(chuàng)建一個名為PresentOrElseHandler的函數(shù)式接口,接口的參數(shù)一個為Consumer接口。一個為Runnable,分別代表值不為空時執(zhí)行消費操作和值為空時執(zhí)行的其他操作
/*** 空值與非空值分支處理*/public interface PresentOrElseHandler<T extends Object> {/*** 值不為空時執(zhí)行消費操作* 值為空時執(zhí)行其他的操作** @param action 值不為空時,執(zhí)行的消費操作* @param emptyAction 值為空時,執(zhí)行的操作* @return void**/void presentOrElseHandle(Consumer super T> action, Runnable emptyAction);}
2.編寫判斷方法
創(chuàng)建一個名為isBlankOrNoBlank的方法,方法的返回值為剛才定義的函數(shù)式接口-PresentOrElseHandler。
/*** 參數(shù)為true或false時,分別進行不同的操作** @param b* @return com.example.demo.func.BranchHandle**/public static PresentOrElseHandler> isBlankOrNoBlank(String str){return (consumer, runnable) -> {if (str == null || str.length() == 0){runnable.run();} else {consumer.accept(str);}};}
3.使用方式
調(diào)用工具類參數(shù)參數(shù)后,調(diào)用函數(shù)式接口的presentOrElseHandle方法傳入一個Consumer和Runnable
參數(shù)不為空時,打印參數(shù)
參數(shù)不為空時
# 結(jié)尾
Function函數(shù)式接口是java 8非常重要的特性,利用好Function函數(shù)可以極大的簡化代碼。
