通过行为参数化传递代码:
在我们设计对象的时候,我们会分析对象的行为,例如:优惠券对象,在使用的时候会有 检查优惠券是否有效 checkValidility() 这一行为。 就是把这个checkValidility() 一段代码块 作为参数 传递给 其他的 方法。
行为 可以看做是 一段代码块/函数
演变历程java8之前的做法(简单的把书上的例子过一遍,主要是为了理解哈):
例子:老王有一堆苹果,需要筛选出绿色的苹果,然后他又想筛选出超过150g的苹果;下面代码:
List<Apple> handledApples = new ArrayList<Apple>();
for (Apple apple : apples) {
if (apple.getColor().equals("green")) {
handledApples.add(apple);
}
}
List<Apple> handledApples = new ArrayList<Apple>();
for (Apple apple : apples) {
if (apple.getHeavy() > 150) {
handledApples.add(apple);
}
}
相同的代码三行,想要把判断的那一行代码抽出来。方法来了,
使用 策略设计模式
public interface ApplePredicate {//封装选择苹果的策略
Boolean test(Apple apple);
}
public class AppleHeavyPredicate implements ApplePredicate {
public Boolean test(Apple apple) {//根据重量选择苹果
return apple.getHeavy() > 150;
}
}
public class AppleColorPredicate implements ApplePredicate{
public Boolean test(Apple apple) {//根据颜色选择苹果
return apple.getColor().equals("green");
}
}
@Service
public class ApplePredicateService {
//过滤苹果的处理逻辑
public List<Apple> handle(List<Apple> apples, ApplePredicate applePredicate) {
List<Apple> handledApples = new ArrayList<Apple>();
for (Apple apple : apples) {
if (applePredicate.test(apple)) {
handledApples.add(apple);
}
}
return handledApples;
}
}
测试:
public class ApplePredicateTest {
@Autowired
private ApplePredicateService service;
@Test
public void testApplePredicate() {
List<Apple> apples = new ArrayList();
apples.add(Apple.builder().color("green").heavy(302.5).build());
apples.add(Apple.builder().color("red").heavy(190.6).build());
apples.add(Apple.builder().color("red").heavy(66.0).build());
apples.add(Apple.builder().color("green").heavy(100.23).build());
List<Apple> greenApples = service.handle(apples, new AppleColorPredicate());
Assert.assertEquals(2, greenApples.size());//筛选出的苹果
}
}
ApplePredicate 的test行为 传进 service 的handle()方法中,只不过上面的这种方法不是直接把行为当做入参传入,而是通过 把代码包进 ApplePredicate 对象里面 使用;
使用匿名类:java中允许同时声明并实例化一个类
List<Apple> weightApples = service.handle(apples, new ApplePredicate() {
@Override
public Boolean test(Apple apple) {
return apple.getHeavy() > 150;
}
});
Assert.assertEquals(2, weightApples.size());
这个方法就是可以不用创建多个继承类,随用随建
摘录:让方法接受多种行为作为参数并在内部使用,来完成不同的行为。
第二章的内容是为了让初学者理解 行为参数化传递给代码 ,我看的时候就是理解下概念,不过我们经理给我们推荐了使用 predicate 的一些 衍生知识 :
https://www.tutorialspoint.com/commons_collections/commons_collections_filtering_objects.htm
apache 的Commons Collections - Filtering Objects