我们要判断多个苹果里面的绿颜色和大于150g的苹果,或者说后面会加入更多的筛选条件,用if/else太显得臃肿,所以,java8提出了行为参数化,我们按照以下两步来进行行为参数化的说明,也是对自己的学习的一个总结。
处理这种问题首先要先建立一个公共的接口,这个接口里有一个方法,就是来接收所有的条件:
public interface FormatApple { boolean test(Apple apple); }
然后分别建立两个实现,一个是颜色,一个是重量,可以把参数作为重写方法的主要内容。
public class ColorAppleOp implements FormatApple { @Override public boolean test(Apple apple) { return "green".equals(apple.getColor()); } }
public class WeightOp implements FormatApple { @Override public boolean test(Apple apple) { return Integer.parseInt(apple.getWight())>150; } }
两个类里面都针对不同的条件进行判断,返回一个boolean类型的结果,我们在执行的时候就可以直接判断,那个实体对象符合条件,返回了true,然后将其放入list中返回。
public class MainDo { public static List<Apple> appleList(List<Apple> apples, FormatApple formatApple){ List<Apple> apples1 = new ArrayList<>(); for (Apple apple:apples){ if (formatApple.test(apple)){ apples1.add(apple); } } return apples1; } public static void main(String[] args) { Apple a = new Apple(); a.setColor("green"); a.setWight("200"); Apple a2 = new Apple(); a2.setWight("100"); a2.setColor("green"); Apple a3 = new Apple(); a3.setColor("red"); a3.setWight("250"); List alist = new ArrayList(); alist.add(a); alist.add(a2); alist.add(a3); //筛选出绿色的苹果 List<Apple> resultA1 = appleList(alist,new ColorAppleOp()); //筛选出重量大于>150g的苹果 List<Apple> resultA2 = appleList(alist,new WeightOp()); for (Apple apple: resultA1){ System.out.println(apple.getColor()+apple.getWight()); } System.out.println("--------------------"); for (Apple apple1: resultA2){ System.out.println(apple1.getColor()+apple1.getWight()); } } }
输出结果:
green200 green100 -------------------- green200 red250
这样我们就初步实现了行为参数化,但是这样写就会多建立很多的实现类,也是很臃肿,那么如何实现呢,接下来我们可以再优化一下下。
接口这里我们用泛型来接收传过来的不同实体参数:
public interface FormatApple<T> { boolean test(T t); }
然后再主要执行类中MainDo里全部修改成泛型类型:
public static <T> List<T> appleList(List<T> apples, FormatApple<T> formatApple){ List<T> apples1 = new ArrayList<T>(); for (T apple:apples){ if (formatApple.test(apple)){ apples1.add(apple); } } return apples1; }
在筛选的代码位置,我们替换成以下代码:
这是利用Lambda表达式,真正的实现行为参数化
List<Apple> resultA1 = appleList(alist,(Apple apple) -> "green".equals(apple.getColor())); List<Apple> resultA2 = appleList(alist,(Apple apple) -> Integer.parseInt(apple.getWight())>150);
当我们需要改变时,只需要传不同的实体类型就可以,不需要我们再建立多个实现类,直接一个接口,在实现类里写对应的结构方法,将接口实体作为参数传递进来,并通过泛型,扩大对不同实体的处理,将行为参数条件化:
List<Banana> resultA3 = appleList(alist,(Banana banana) -> Integer.parseInt(banana.getWeight())>150);
具体代码:
public class MainDo { public static <T> List<T> appleList(List<T> apples, FormatApple<T> formatApple){ List<T> apples1 = new ArrayList<T>(); for (T apple:apples){ if (formatApple.test(apple)){ apples1.add(apple); } } return apples1; } public static void main(String[] args) { Banana a = new Banana(); a.setColor("green"); a.setWeight("200"); Banana a2 = new Banana(); a2.setWeight("100"); a2.setColor("green"); Apple a3 = new Apple(); a3.setColor("red"); a3.setWight("250"); List alist = new ArrayList(); alist.add(a); alist.add(a2); //筛选出绿色的苹果 /*List<Apple> resultA1 = appleList(alist,new ColorAppleOp()); //筛选出重量大于>150g的苹果 List<Apple> resultA2 = appleList(alist,new WeightOp());*/ /* List<Apple> resultA1 = appleList(alist,(Apple apple) -> "green".equals(apple.getColor())); List<Apple> resultA2 = appleList(alist,(Apple apple) -> Integer.parseInt(apple.getWight())>150);*/ List<Banana> resultA3 = appleList(alist,(Banana banana) -> Integer.parseInt(banana.getWeight())>150); for (Banana banana: resultA3){ System.out.println(banana.getClass().toString()+""+banana.getColor()+banana.getWeight()); } /* System.out.println("--------------------"); for (Apple apple1: resultA2){ System.out.println(apple1.getColor()+apple1.getWight()); }*/ } }