我嘗試過把Lambda運算式融入到My Code中,下面的代碼例子是此次嘗試的結果。對於那些完全不知道Lambda運算式的Java程式員,我強烈建議在繼續閱讀之前,瀏覽一下這篇文章。
Ok,現在你已經熟悉Lambda運算式了(在閱讀過推薦的Lambda入門文章之後),那我們現在開始學習一個我認為很好的Lambda運算式的例子。
考慮一下這種情境:某些操作在執行之前需要做預先處理,執行之後需要做後期處理。待執行的操作會隨著行為的不同而變化。預先處理會提取出這個操作所需的必要參數,後期處理做一些清理的工作。
我們來看看如何利用介面與介面的匿名實作類別類比這個情境。
使用匿名內部類
一個提供了必要行為方法的介面實現:
| 代碼如下 |
複製代碼 |
interface OldPerformer {
public void performTask(String id, int status);
} |
接下來是一些預先處理和後期處理方法:
| 代碼如下 |
複製代碼 |
public class PrePostDemo {
static void performTask(String id, OldPerformer performer) {
System.out.println("Pre-Processing...");
System.out.println("Fetching the status for id: " + id);
int status = 3;
//Some status value fetched
performer.performTask(id, status);
System.out.println("Post-processing...");
}
} |
我們需要傳遞2樣東西:預先處理所需的標識,以及操作的實現。如下所示:
| 代碼如下 |
複製代碼 |
public class PrePostDemo {
public static void main(String[] args) {
//has to be declared final to be accessed within
//the anonymous inner class.
final String outsideOfImpl = "Common Value";
performTask("1234", new OldPerformer() {
@Override
public void performTask(String id, int status) {
System.out.println("Finding data based on id..."
);
System.out.println(outsideOfImpl);
System.out.println("Asserting that the status matches");
}
});
performTask("4567", new OldPerformer() {
@Override
public void performTask(String id, int status) {
System.out.println("Finding data based on id...");
System.out.println(outsideOfImpl);
System.out.println("Update status of the data found");
}
});
}
} |
從上面的代碼可以看出,匿名內部類外部的變數要想被匿名內部類訪問,需要聲明成final。例子的代碼輸出如下:
| 代碼如下 |
複製代碼 |
Pre-Processing...</pre>
Fetching the status for id: 1234
Finding data based on id...
Common Value
Asserting that the status matches
Post-processing...
PreProcessing...
Fetching the status for id: 4567
Finding data based on id...
Common Value
Update the status of the data found
Post-processing... |
使用Lambda運算式
我們來看看如何用Lambda運算式實現上述例子:
| 代碼如下 |
複製代碼 |
public class PrePostLambdaDemo {
public static void main(String[] args) {
//Need not be declared as final for use within a
//lambda expression, but has to be eventually final.
String outsideOfImpl = "Common Value";
doSomeProcessing("123", (String id, int status) -> {
System.out.println("Finding some data based on"+id);
System.out.println(outsideOfImpl);
System.out.println("Assert that the status is "+status );
});
doSomeProcessing("456", (String id, int status) -> {
System.out.print("Finding data based on id: "+id);
System.out.println(outsideOfImpl);
System.out.println("And updating the status: "+status);
});
}
static void doSomeProcessing(String id, Performer performer ){
System.out.println("Pre-Processing...");
System.out.println("Finding status for given id: "+id);
int status = 2;
performer.performTask(id, status);
System.out.println("Post-processing...");
}
}
interface Performer{
public void performTask(String id, int status);
} |
除了有趣的Lambda運算式文法之外,還有一點不同,那就是Lambda運算式外部的變數沒有聲明成final。但最終變數還是會成為常量,這意味著outsideOfImpl 在聲明及初始化之後的值不能被更改。
這個例子只是展示了如何使用清晰明了的Lambda運算式代替匿名內部類。
一個小提示:JDK8的發布時間延遲到了2014年2月,完整的發布時間表可以從這裡查閱。我每天都會更新Lambda的構建項目,如果在最新的構建中出現了任何問題,請隨時聯絡我。我會盡最大努力持續構建,並且會在這裡發表最新的例子。
另一個提示:不要讓Java 8的更新使你不知所措,大部分新特性已經存在於其他程式設計語言中。我發現學習Lambda運算式的文法和方法可以協助我以函數式的思維進行思考,對此我還應特別感謝Scala閉包。