本篇內(nèi)容主要講解“Java8怎么實(shí)現(xiàn)通過行為參數(shù)傳遞代碼”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Java8怎么實(shí)現(xiàn)通過行為參數(shù)傳遞代碼”吧!

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供龍井網(wǎng)站建設(shè)、龍井做網(wǎng)站、龍井網(wǎng)站設(shè)計(jì)、龍井網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、龍井企業(yè)網(wǎng)站模板建站服務(wù),10余年龍井做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
通過篩選蘋果闡述通過行為參數(shù)傳遞代碼
public static List<Apple> filterGreenApples(List<Apple> inventory){
List<Apple> result = new ArrayList<>();
for(Apple apple: inventory){
if("green".equals(apple.getColor())){
result.add(apple);
}
}
return result;
}要是農(nóng)民想要篩選多種顏色:淺綠色、暗紅色、黃色等,這種方法就應(yīng)付不了了。一個(gè)良好的原則是在編寫類似的代碼之后,嘗試將其抽象化。
public static List<Apple> filterApplesByColor(List<Apple> inventory, String color){
List<Apple> result = new ArrayList<>();
for(Apple apple: inventory){
if(apple.getColor().equals(color)){
result.add(apple);
}
}
return result;
}運(yùn)用
List<Apple> greenApples = filterApplesByColor(inventory, "green"); List<Apple> redApples = filterApplesByColor(inventory, "red");
“要是能區(qū)分輕的蘋果和重的蘋果就太好了。重的蘋果一般是重量大于150克。”
public static List<Apple> filterApplesByWeight(List<Apple> inventory, int weight){
List<Apple> result = new ArrayList<>();
for(Apple apple: inventory){
if(apple.getWeight() > weight){
result.add(apple);
}
}
return result;
}發(fā)現(xiàn)有重復(fù)代碼,打破DRY(Don't Repeat Yourself)原則
public static List<Apple> filterApples(List<Apple> inventory, int weight, String color, boolean flag){
List<Apple> result = new ArrayList<Apple>();
for (Apple apple: inventory){
if ( (flag && apple.getColor().equals(color)) || (!flag && apple.getWeight() > weight) ){
result.add(apple);
}
}
return result;
}你可以這么用(但真的很笨拙)
List<Apple> greenApples = filterApples(inventory, "green", 0, true); List<Apple> heavyApples = filterApples(inventory, "", 150, false);
定義一個(gè)接口來對(duì)選擇標(biāo)準(zhǔn)建模:
interface ApplePredicate{
public boolean test(Apple a);
}現(xiàn)在你就可以用ApplePredicate的多個(gè)實(shí)現(xiàn)代表不同的選擇標(biāo)準(zhǔn)了
public class AppleWeightPredicate implements ApplePredicate{
public boolean test(Apple apple){
return apple.getWeight() > 150;
}
}
public class AppleColorPredicate implements ApplePredicate{
public boolean test(Apple apple){
return "green".equals(apple.getColor());
}
}這里有 策略模式的影子
策略模式:定義一系列的算法,把它們一個(gè)個(gè)封裝起來, 并且使它們可相互替換。
該怎么利用ApplePredicate的不同實(shí)現(xiàn)呢?你需要filterApples方法接受ApplePredicate對(duì)象,對(duì)Apple做條件測試。這就是行為參數(shù)化:讓方法接受多種行為(或戰(zhàn)略)作為參數(shù),并在內(nèi)部使用,來完成不同的行為。
public static List<Apple> filter(List<Apple> inventory, ApplePredicate p){
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory){
if(p.test(apple)){
result.add(apple);
}
}
return result;
}public class AppleRedAndHeavyPredicate implements ApplePredicate{
public boolean test(Apple apple){
return "red".equals(apple.getColor())
&& apple.getWeight() > 150;
}
}
List<Apple> redAndHeavyApples = filter(inventory, new AppleRedAndHeavyPredicate());filterApples方法的行為取決于你通過ApplePredicate對(duì)象傳遞的代碼。換句話說,你把filterApples方法的行為參數(shù)化了!
行為參數(shù)化的好處在于你可以把迭代要篩選的集合的邏輯與對(duì)集合中每個(gè)元素應(yīng)用的行為區(qū)分開來。這樣你可以重復(fù)使用同一個(gè)方法,給它不同的行為來達(dá)到不同的目的。
編寫靈活的prettyPrintApple方法
編寫一個(gè)prettyPrintApple方法,它接受一個(gè)Apple的List,并可以對(duì)它參數(shù)化,以多種方式根據(jù)蘋果生成一個(gè)String輸出
public static void prettyPrintApple(List<Apple> inventory, ???){
for(Apple apple: inventory) {
String output = ???.???(apple);
System.out.println(output);
}
}首先
public interface AppleFormatter{
String accept(Apple a);
}然后
public class AppleFancyFormatter implements AppleFormatter{
public String accept(Apple apple){
String characteristic = apple.getWeight() > 150 ? "heavy" : "light";
return "A " + characteristic + " " + apple.getColor() +" apple";
}
}
public class AppleSimpleFormatter implements AppleFormatter{
public String accept(Apple apple){
return "An apple of " + apple.getWeight() + "g";
}
}最后
public static void prettyPrintApple(List<Apple> inventory, AppleFormatter formatter){
for(Apple apple: inventory){
String output = formatter.accept(apple);
System.out.println(output);
}
}運(yùn)用
prettyPrintApple(inventory, new AppleFancyFormatter()); prettyPrintApple(inventory, new AppleSimpleFormatter());
輸出
A light green apple A heavy red apple …
An apple of 80g An apple of 155g …
人們都不愿意用那些很麻煩的功能或概念。目前,當(dāng)要把新的行為傳遞給filterApples方法的時(shí)候,你不得不聲明好幾個(gè)實(shí)現(xiàn)ApplePredicate接口的類,然后實(shí)例化好幾個(gè)只會(huì)提到一次的ApplePredicate對(duì)象。這真是很啰嗦,很費(fèi)時(shí)間!
匿名類和你熟悉的Java局部類(塊中定義的類)差不多,但匿名類沒有名字。它允許你同時(shí)聲明并實(shí)例化一個(gè)類。換句話說,它允許你隨用隨建。
List<Apple> redApples = filterApples(inventory, new ApplePredicate() {
public boolean test(Apple apple){
return "red".equals(apple.getColor());
}
});但匿名類還是不夠好。第一,它往往很笨重,因?yàn)樗加昧撕芏嗫臻g。第二,很多程序員覺得它用起來很讓人費(fèi)解。
鼓勵(lì)程序員使用行為參數(shù)化模式,通過引入Lambda表達(dá)式——一種更簡潔的傳遞代碼的方式。
List<Apple> result = filterApples(inventory, (Apple apple) -> "red".equals(apple.getColor()));
目前,filterApples方法還只適用于Apple。還可以將List類型抽象化,從而超越你眼前要處理的問題。
public interface Predicate<T>{
boolean test(T t);
}
public static <T> List<T> filter(List<T> list, Predicate<T> p){
List<T> result = new ArrayList<>();
for(T e: list){
if(p.test(e)){
result.add(e);
}
}
return result;
}List<Apple> redApples = filter(inventory, (Apple apple) -> "red".equals(apple.getColor())); List<Integer> evenNumbers = filter(numbers, (Integer i) -> i % 2 == 0);
// java.util.Comparator
public interface Comparator<T> {
public int compare(T o1, T o2);
}
inventory.sort(new Comparator<Apple>() {
@Override
public int compare(Apple o1, Apple o2) {
return o1.getWeight().compareTo(o2.getWeight());
}
});
inventory.sort((Apple a1, Apple a2)
-> a1.getWeight().compareTo(a2.getWeight()));//行為參數(shù)化
Thread t = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello, World!");
}
});
t = new Thread(()->System.out.println("Hello, World!")) ;Button button = new Button("Send");
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
label.setText("Sent!!");
}
});
//行為參數(shù)化
button.setOnAction((ActionEvent event) -> label.setText("Sent!!"));到此,相信大家對(duì)“Java8怎么實(shí)現(xiàn)通過行為參數(shù)傳遞代碼”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
名稱欄目:Java8怎么實(shí)現(xiàn)通過行為參數(shù)傳遞代碼
本文來源:http://www.yijiale78.com/article14/pdsgde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、搜索引擎優(yōu)化、網(wǎng)站排名、品牌網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)