Java8學(xué)習(xí)筆記之行為參數(shù)化

用一個(gè)例子說(shuō)明行為參數(shù)化帶來(lái)的變化 - 從蘋果倉(cāng)庫(kù)中篩選蘋果

版本1

從一個(gè)蘋果集合中選出綠的蘋果

public static List<Apple> filterGreenApples(List<Apple> inventory) {
        List<Apple> result = new ArrayList<Apple>();
        for (Apple apple : inventory) {
            if ("green".equals(apple.getColor()) {
                result.add(apple);
            }
        }
        return result;
    }

版本2

這時(shí),如果需求變了,要從集合中選出紅蘋果,我們會(huì)這樣

public static List<Apple> filterApplesByColor(List<Apple> inventory, String color) {
        List<Apple> result = new ArrayList<Apple>();
        for (Apple apple : inventory) {
            if (apple.getColor().equals(color)) {
                result.add(apple);
            }
        }
        return result;
    }

然后傳入顏色參數(shù)來(lái)篩選

List<Apple> apples = filterApplesByColor(inventory, "red");

版本3

但是,如果現(xiàn)在要選出重量超過(guò)150g的蘋果呢?在方法參數(shù)列表中多加一個(gè)weight么?
你會(huì)發(fā)現(xiàn)我們所有的代碼,只有if判斷中的條件發(fā)生了變化,這違反了DRY原則(Don't Repeat Yourself)。

所以,我們把整個(gè)具體行為作為參數(shù)來(lái)傳遞,這樣,方法體本身的代碼就可以復(fù)用了。

    // 定義一個(gè)接口
    public interface ApplePredicate {
        boolean test(Apple apple);
    }

    public class AppleHeavyWeightPredicate implements ApplePredicate {
        public boolean test(Apple apple) {
            return apple.getWeight() > 150;
        }
    }

    public class AppleGreenColorPredicate implements ApplePredicate {
        public boolean test(Apple apple) {
            return "green".equals(apple.getColor());
        }
    }
    public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate p) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (p.test(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

現(xiàn)在,我們可以很靈活的調(diào)用了

List<Apple> redAndHeavyApples = filterApples(inventory, new AppleHeavyWeightPredicate());

版本4

其實(shí),接口的具體實(shí)現(xiàn),我們只會(huì)用到一次。所以,我們可以改成匿名類:

List<Apple> redApples = filterApples(inventory, new ApplePredicate() {
        public boolean test(Apple apple) {
            return "red".equals(apple.getColor());
        }
    });

現(xiàn)在,代碼已經(jīng)變得非常簡(jiǎn)潔和靈活了。

版本5

從Java8開(kāi)始,我們可以利用Lambda表達(dá)式,進(jìn)一步改進(jìn)代碼:

List<Apple> result = filterApples(inventory, (Apple apple) -> "red".equals(apple.getColor()));

現(xiàn)在,調(diào)用方法,我們只要一行代碼,而且代碼的可讀性非常好。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,065評(píng)論 25 708
  • 第一章 為什么要關(guān)心Java 8 使用Stream庫(kù)來(lái)選擇最佳低級(jí)執(zhí)行機(jī)制可以避免使用Synchronized(同...
    謝隨安閱讀 1,517評(píng)論 0 4
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,868評(píng)論 18 139
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,198評(píng)論 4 61
  • 自從聽(tīng)了劉同,張嘉佳…那種寫東西的沖動(dòng)就再抑制不住…喜歡簡(jiǎn)書這個(gè)原創(chuàng)平臺(tái)。僅希望留下些什么記錄生活點(diǎn)滴,勾...
    晴箜?xiàng)髂?/span>閱讀 111評(píng)論 0 0