行为参数化是用来处理频繁变更的需求的一种软件开发模式。拿出一个代码块,把它准备好却不去执行它。这个代码块以后可以被程序的其他部分调用,也就是推迟这块代码的执行。
行为参数化:让方法接受多种行为作为参数,并在内部使用,来完成不同的行为。
传递代码,就是将新行为作为参数传递给方法。但在java 8之前实现起来很啰嗦,为借口生命许多只用一次的实体类而造成的啰嗦代码,在java 8之前可以用匿名类来减少。但java 8后,可以使用Lamda。
package lamdainaction.chap1; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class FilteringApples { public static void main(String[] args) { List<Apple> inventory = Arrays.asList(new Apple(80, "green"), new Apple(155, "green"), new Apple(120, "red")); List<Apple> result1 = filterApples1(inventory); System.out.println(result1); List<Apple> result2 = filterApples2(inventory); System.out.println(result2); List<Apple> result3 = filterApples(inventory, new AppleColorPredicate()); System.out.println(result3); List<Apple> result4 = filterApples(inventory, new AppleWeightPredicate()); System.out.println(result4); List<Apple> result5 = filterApples(inventory, new ApplePredicate(){ @Override public boolean test(Apple apple) { // TODO Auto-generated method stub return "red".equals(apple.getColor()); } }); System.out.println(result5);
List<Apple> result6 = filterApples(inventory, Apple apple -> "red".equals(apple.getColor());
System.out.println(result6);
} public static List<Apple> filterApples1(List<Apple> inventory) { List<Apple> result = new ArrayList<>(); for (Apple apple : inventory) { if ("green".equals(apple.getColor())) { result.add(apple); } } return result; } public static List<Apple> filterApples2(List<Apple> inventory) { List<Apple> result = new ArrayList<>(); for (Apple apple : inventory) { if (apple.getWeight() > 150 ) { result.add(apple); } } return result; } 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; } } interface ApplePredicate { boolean test(Apple apple); } class AppleColorPredicate implements ApplePredicate { @Override public boolean test(Apple apple) { return "green".equals(apple.getColor()); } } class AppleWeightPredicate implements ApplePredicate { @Override public boolean test(Apple apple) { return apple.getWeight() > 150; } } class Apple { private int weight; private String color; public Apple(int weight, String color) { this.setWeight(weight); this.setColor(color); } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String toString() { return "Apple{" + "color='" + color + "', weight=" + weight +"}"; } }
谓词(predicate):一个返回boolean值的函数。例如苹果,需要根据Apple的某些属性(它是绿色的吗,它的重量超过150g吗)来返回一个boolean值。
Lamda:
List<Apple> result6 = filterApples(inventory, Apple apple -> "red".equals(apple.getColor());
-> 前的 Apple apple 是ApplePredicate.test(Apple apple)中的参数
-> 后的 "red".equals(apple.getColor()) 是ApplePredicate.test(Apple apple)的方法体
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步