用一個(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)用方法,我們只要一行代碼,而且代碼的可讀性非常好。