编程语言的整个目的就在于操作值,要是按照历史上编程语言的传统,这些值被成为一等值,而编程语言中的其他结构也许有助于表示值的结构,但在程序执行期间不能传递,因此为二等值,比如方法和类等则是二等值,类可以实例化来产生值,但方法和类本身都不是值,java8的设计者则决定允许方法作为值,让编程更轻松.

举个栗子

就农场库存程序而言,如果要实现一个从列表中塞选出绿色苹果的功能,可能会这样写(1.0)

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

假如这个时候要赛选出不仅是绿色的苹果,可能会加入一个颜色作为参数,就象这样(2.0)

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

但这个时候别人可能还想区分出重苹果和轻苹果,你可能会这样来赛选重苹果

public static List<Apple> filterApplesByWeight(List<Apple> inventory, int weight){
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory){
if (apple.getWeight() > weight) {
result.add(apple);
}
}
return result;
}

这样解决确实不错,但是却复制了大量的代码来实现功能,它打破了DRY(Don't Repeat Yourself)的软件工程原则,会许这时候你可能会将两种条件结合起来,并用一个标识表示赛选那种类型的苹果,像这样(3.0)

public static List<Apple> filterGreenApples(List<Apple> inventory, String color, Integer weight, boolean flag){
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory){
if ((flag && apple.getColor().equals(color)) || (!flag && apple.getWeight() > weight)) {
result.add(apple);
}
}
return result;
}

但是,这并不是一个可行的解决方案,首先别人阅读你的代码的时候,true|false表示什么?如果这个时候还有更加复杂的塞选情况又该怎么办?java8的行为化参数可以解决这个问题

行为参数化

让方法接受多种行为(或战略)作为参数,并在内部使用,来完成不同的行为.

好处: 多种行为,一个参数,可以把迭代要塞选的集合的逻辑和对集合中每个元素应用的行为区分开来,这样可以重复使用同一个方法,给他不同的行为达到不同的目的.

定义一个接口来对选择标准建模

public interface ApplePredicate {
boolean test (Apple apple);
}

现在就可以为它进行多个实现了,比如

public class AppleGreenColorPredicate implements ApplePredicate {
@Override
public boolean test(Apple apple) {
//仅选出绿色的苹果
return "green".equals(apple.getColor());
}
} public class AppleHeavyWeightPredicate implements ApplePredicate {
@Override
public boolean test(Apple apple) {
//仅选出重的苹果
return apple.getWeight() > 150;
}
}

现在经过java8的改良,代码可能会是这个样子(4.0)

public static List<Apple> filterApples(List<Apple> apples, ApplePredicate predicate){
List<Apple> result = new ArrayList<>();
for (Apple apple : apples){
if (predicate.test(apple)){
result.add(apple);
}
}
return result;
}

每次调用的时候,只需要传递相应的行为即可,比如

List<Apple> greenApples = filterApples(inventory,new AppleGreenColorPredicate());

这样一来,每次塞选的是什么苹果完全取决于通过ApplePredicate对象传递的代码,换句话说,把filterApple的方法行为参数化了

当然,到了这里你可能还觉得不甘心,因为每次都要去创建一个新的类,有点啰嗦,所以,你可以使用匿名类来完成这一工作(5.0)

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

但是,匿名类还是不够还,因为它占用了很多空间,所以,在java8中可以直接使用Lambda表达式来完成(6.0)

List<Apple> weightApples = filterApples(inventory, (Apple apple) -> apple.getWeight() > 150);

到目前为止,总算是可以解决啰嗦麻烦的问题了,因为它更像在描述问题本身.现在你可以将List抽象画,让他应用面更加广泛

public interface Predicate<T>{
boolean test(T t);
} public static <T> List<T> filter(List<T> list, Predicate<T> p){
List<T> result = new ArrayList<>();
for(T e : list){
if(p.test(e)){
result.add(e);
}
}
return result;
}

现在在灵活性和简洁性之间找到了平衡点,这是java8带来的便利.

最新文章

  1. js和jquery如何获取图片真实的宽度和高度
  2. reset 单个文件 回退
  3. LinQ实战学习笔记(三) 序列,查询操作符,查询表达式,表达式树
  4. UWP开发-HTTP详解
  5. Maven解决Missing artifact com.sun:tools:jar:1.5.0错误
  6. zookeeper使用和原理探究(一)(转)
  7. AQuery简介:jQuery for Android
  8. IdeasToComeTrue
  9. Dynamics CRM 提示&ldquo;操作无效&rdquo;
  10. 工具:Entity Framework Profiler
  11. C语言博客作业--函数
  12. 安装Adobe时出现Adobe Application Manager丢失或损坏解决方法
  13. 【Unity】第5章 3D坐标系和天空盒
  14. hdu-1176(动态规划)
  15. “最大子序列和”算法 java
  16. 《JAVA与模式》之桥接模式
  17. js实现window.open不被拦截的解决方法汇总
  18. 从零开始用 Flask 搭建一个网站(四)
  19. [ES6] 10. Array Comprehensions
  20. node搭环境

热门文章

  1. HDU 6098 Inversion
  2. AtomicLong与LongAdder的区别
  3. SSH Secure Shell Client--- the host may be dow
  4. Lunar New Year and Red Envelopes CodeForces - 1106E (dp)
  5. AND Graph CodeForces - 987F (状压)
  6. ansible-playbook快速入门填坑
  7. matplotlib.pyplot 绘制图形
  8. OAF Sample Code(转)
  9. JAVA计算文件的crc32校验码
  10. django url 使用总结