美文网首页
行为参数化——使用lambda传递代码

行为参数化——使用lambda传递代码

作者: Blue_hr | 来源:发表于2019-05-11 15:10 被阅读0次

行为参数化传递代码

行为参数化是为了帮助我们应对不断变化的需求实现快速开发

比如在List<Apple> 苹果集合中我们需要实现:

  • 找出绿色苹果
  • 找出大苹果(重量大于500g)
  • 找出绿色大苹果

对于这样的场景,我们可以将对集合的需求就可以抽象为一个行为。

假设场景:

​ 1. 需要筛选绿苹果

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;
}
  1. 需要筛选各种颜色(将颜色抽象为参数)
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;
}
  1. 需要大于500g的苹果或者绿苹果

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

    这样确实比较蠢,不写注释的话,你能根据方法名判断flag是干啥的不?

这个时候我们就可以运用“ 行为参数化 ” 来帮我们解决问题。

首先我们来抽象需求:我们考虑的
是苹果,需要根据Apple的某些属性(比如它是绿色的吗?重量超过xxx克吗?)来返回一个
boolean值。我们把它称为谓词(即一个返回boolean值的函数)。让我们定义一个接口来对选
JDK中为我们提供了一个接口

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

现在我们可以根据不同需求实现不同的行为

public class AppleHeavyWeightPredicate implements ApplePredicate{
    public boolean test(Apple apple){
        return apple.getWeight() > 150; //筛选大于150g的
    }
}
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;
}

最终实现了行为参数化的fliterAppler方法看起来是不错的

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 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> heavyApples =
    filterApples(inventory, new AppleHeavyWeightPredicate());
    List<Apple> greenApples =
    filterApples(inventory, new AppleGreenColorPredicate());
    }
    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;
    }
}

可是每次都还需要定义一个实现类再实例化,看起来好像十分的啰嗦。Java有一个机制称为匿名类,它可以让你同时
声明和实例化一个类。它可以帮助你进一步改善代码,让它变得更简洁。

下面的代码展示了如何通过创建一个用匿名类实现ApplePredicate的对象,重写筛选的例子:

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

但是不得不说匿名类真的很笨重,很多冗余的模版代码,而且还会有this指针的问题

引入Lambda我们可以做到这样子,避免了笨重了匿名类。

List<Apple> result =
filterApples(inventory, (Apple apple) -> "red".equals(apple.getColor()));

相关文章

  • [代码简化] 1 Lambda表达式

    1、lambda表达式 1.1 lambda 利用lambda表达式代替匿名参数类,将行为进行参数化传递到代码中,...

  • 行为参数化——使用lambda传递代码

    行为参数化传递代码 行为参数化是为了帮助我们应对不断变化的需求实现快速开发 比如在List 苹果集合中我们需要实...

  • JAVA8(二)

    行为参数化 什么是行为参数化 个人理解:把行为抽象出来进行封装,让代码适应需求的变化,并把行为或代码作为参数传递,...

  • Lambda 深入浅出

    在学习 Lambda之前我们得学习下通过行为参数化传递代码,如何理解?举个例子就是,在我们进行音乐程序开发的时候,...

  • 侠说java8-行为参数化(开山篇)

    啥是行为参数化 行为参数化的本质是不执行复杂的代码块,让逻辑清晰可用。 相信使用过js的你肯定知道,js是可以传递...

  • 第三节:lambda表达式初识

    上一节中,通过行为参数化的方式来传递代码,代码量比较多,不简洁,也不是十分灵活,这一张,我们就先初识lambda表...

  • java 8 新特性-lambda表达式

    lambda表达式 理解(行为参数化)lambda表达式是一个函数式接口的实现,可以将函数作为方法参数,或者将代码...

  • 2018-08-13通过行为参数化传递代码

    行为参数化 就是一个方法接收多个不同的行为作为参数,并在内部使用它们,完成不同行为的能力。 使用Lambda表达式

  • 通过行为参数化传递代码

    基础概念 在软件工程中,一个众所周知的问题是,不管你做什么,用户的需求肯定会变。行为参数化就是可以帮助你处理频繁变...

  • 通过行为参数化传递代码

    行为参数化是可以帮助你处理频繁变更的需求的一种软件开发模式 引言 1.首先我们看下实现从苹果列表中选出所有的绿色的...

网友评论

      本文标题:行为参数化——使用lambda传递代码

      本文链接:https://www.haomeiwen.com/subject/iqvxaqtx.html