首页 > 其他分享 >通过行为参数传递代码

通过行为参数传递代码

时间:2023-01-03 10:38:14浏览次数:50  
标签:apple weight 代码 List 参数传递 test 行为 public Apple


一、概述


1.1、行为参数化的含义:让方法接受多种行为作为参数,并在内部使用,来完成不同行为。


1.2、案例:我们要得到重量大于150克的苹果和所有的绿苹果。


二、代码实现


2.1 行为参数化


package com.jdk.test;

public class Apple {
private String color;
private int weight;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Apple(String color, int weight) {
super();
this.color = color;
this.weight = weight;
}
@Override
public String toString() {
return "Apple [color=" + color + ", weight=" + weight + "]";
}


}



package com.jdk.test;
/**
*
* @author Administrator
*策略模式的应用
*行为参数化:让方法接受多种行为作为参数,并在内部使用,来完成不同的行为。
*/
public interface ApplePredicate {

boolean test(Apple apple);
}


package com.jdk.test;

public class AppleGreenColorPredicate implements ApplePredicate {

@Override
public boolean test(Apple apple) {
//选出绿色的苹果
return "green".equals(apple.getColor());
}

}
package com.jdk.test;

public class AppleHeavyWeightPredicate implements ApplePredicate {

@Override
public boolean test(Apple apple) {
//选出大于150克的苹果
return apple.getWeight() > 150;
}

}
package com.jdk.test;

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("green",80),
new Apple("red",190),
new Apple("green",100));

List<Apple> heavryApples = filterApples(inventory,new AppleHeavyWeightPredicate());
heavryApples.forEach(Apple -> System.out.println("输出重量大于150克"+Apple.toString()));
List<Apple> greenApples = filterApples(inventory,new AppleGreenColorPredicate());
greenApples.forEach(Apple -> System.out.println("输出绿色的Apple"+Apple.toString()));
}

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;
}
}

以上是我们使用行为参数化来实现的需求,下面我们来看下使用匿名内部类的实现方式:






List<Apple> redApples = filterApples(inventory,new ApplePredicate() {
@Override
public boolean test(Apple apple) {
return "red".equals(apple.getColor());
}
});
redApples.forEach(Apple ->System.out.println("输出红色Apple"+Apple.toString()));






使用了匿名内部类并没有让我们感到代码的简洁和易读,我们再对上面的代码进行改造。






2.2使用Lambda表达式



List<Apple> greenApples = filterApples(inventory, (Apple apple) -> "green".equals(apple.getColor()));
greenApples.forEach(Apple -> System.out.println("使用Lambda表达式"+Apple.toString()));



//按照大小升序排序



greenApples.sort((Apple a1,Apple a2) -> a1.getMax().compareTo(a2.getMax()));




这段代码看起来是不是清晰多了,这就是JDK8中的

Lambda表达式的写法。


三、总结



通过上面的案例我们已经熟悉了JDK8中的Lambda表达式的写法,我们可以将filter方法进行提取让它变成一个公共



的代码块,而不仅仅是过滤Apple了。


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

标签:apple,weight,代码,List,参数传递,test,行为,public,Apple
From: https://blog.51cto.com/u_15928064/5984644

相关文章