java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java行为参数化传递代码

Java函数式编程之通过行为参数化传递代码

作者:jack_xu

行为参数化就是可以帮助你处理频繁变更的需求的一种软件开发模式,这篇文章将给大家详细的介绍一下Java函数式编程之行为参数化传递代码,感兴趣的同学可以参考阅读下

不断变化的需求

应对不断变化的需求

public static List<Apple> filterGreenApples(List<Apple> apples) {
    List<Apple> arrayList = new ArrayList<>();
    for (Apple apple : apples) {
        // 筛选
        if ("green".equals(apple.getColor())) {
            arrayList.add(apple);
        }
    }
    return arrayList;
}
public static List<Apple> filterApplesByColor(List<Apple> apples, String color) {
    List<Apple> arrayList = new ArrayList<>();
    for (Apple apple : apples) {
        if (color != null && color.equals(apple.getColor())) {
            arrayList.add(apple);
        }
    }
    return arrayList;
}
public static List<Apple> filterApplesByWeight(List<Apple> apples, int Weight) {
    List<Apple> arrayList = new ArrayList<>();
    for (Apple apple : apples) {
        if (apple.getWeight() > Weight) {
            arrayList.add(apple);
        }
    }
    return arrayList;
}
public static List<Apple> filterApples(List<Apple> apples, int weight, String color, boolean flag) {
    List<Apple> arrayList = new ArrayList<>();
    for (Apple apple : apples) {
        if ((flag && apple.getWeight() > weight) || (!flag && color != null && color.equals(apple.getColor()))) {
            arrayList.add(apple);
        }
    }
    return arrayList;
}

行为参数化

public interface ApplePredicate {
    boolean test(Apple apple);
}
class AppleHeavyWeightPredicate implements ApplePredicate {
    @Override
    public boolean test(Apple apple) {
        return apple.getWeight() > 150;
    }
}
class AppleRedPredicate implements ApplePredicate {
    @Override
    public boolean test(Apple apple) {
        return "red".equals(apple.getColor());
    }
}
public class Test5 {
    public static void main(String[] args) {
        System.out.println(filterApples(AppleClient.getApples(), new AppleHeavyWeightPredicate()));
        System.out.println(filterApples(AppleClient.getApples(), new AppleRedPredicate()));
    }
    public static List<Apple> filterApples(List<Apple> apples, ApplePredicate applePredicate) {
        List<Apple> arrayList = new ArrayList<>();
        for (Apple apple : apples) {
            if (applePredicate.test(apple)) {
                arrayList.add(apple);
            }
        }
        return arrayList;
    }
}
public class Test5 {
    public static void main(String[] args) {
        System.out.println(filterApples(AppleClient.getApples(), new ApplePredicate() {
            @Override
            public boolean test(Apple apple) {
                return apple.getWeight() > 150;
            }
        }));
        System.out.println(filterApples(AppleClient.getApples(), new ApplePredicate() {
            @Override
            public boolean test(Apple apple) {
                return "red".equals(apple.getColor());
            }
        }));
    }
    public static List<Apple> filterApples(List<Apple> apples, ApplePredicate applePredicate) {
        List<Apple> arrayList = new ArrayList<>();
        for (Apple apple : apples) {
            if (applePredicate.test(apple)) {
                arrayList.add(apple);
            }
        }
        return arrayList;
    }
}
public class Test5 {
    public static void main(String[] args) {
        System.out.println(filterApples(AppleClient.getApples(), apple -> apple.getWeight() > 150));
        System.out.println(filterApples(AppleClient.getApples(), apple -> "red".equals(apple.getColor())));
    }
    public static List<Apple> filterApples(List<Apple> apples, ApplePredicate applePredicate) {
        List<Apple> arrayList = new ArrayList<>();
        for (Apple apple : apples) {
            if (applePredicate.test(apple)) {
                arrayList.add(apple);
            }
        }
        return arrayList;
    }
}
public static void prettyPrintApple(List<Apple> apples,???){
    for (Apple apple : apples) {
        String output = ???.???(apple);
        System.out.println(output);
    }
}
public interface AppleFormat {
    String accept(Apple apple);
}
public static void prettyPrintApple(List<Apple> apples,AppleFormat appleFormat){
    for (Apple apple : apples) {
        String output = appleFormat.accept(apple);
        System.out.println(output);
    }
}
public class Test6 {
    public static void main(String[] args) {
        prettyPrintApple(AppleClient.getApples(), apple -> {
            return "颜色:" + apple.getColor() + ",重量:" + apple.getWeight();
        });
        prettyPrintApple(AppleClient.getApples(), apple -> {
            return "颜色:" + apple.getColor();
        });
    }
    public static void prettyPrintApple(List<Apple> apples, AppleFormat appleFormat) {
        for (Apple apple : apples) {
            String output = appleFormat.accept(apple);
            System.out.println(output);
        }
    }
}
public interface Predicate<T> {
    boolean test(T t);
}
public static <T> List<T> filter(List<T> list, Predicate<T> predicate) {
    List<T> result = new ArrayList<>();
    for (T e : list) {
        if (predicate.test(e)){
            result.add(e);
        }
    }
    return result;
}
public interface Comparator<T>{
    int compare(T o1, T o2);
}
public class Test8 {
    public static void main(String[] args) {
        AppleClient.getApples().sort(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getWeight() - o2.getWeight();
            }
        });
    }
}
public class Test8 {
    public static void main(String[] args) {
        AppleClient.getApples().sort((o1, o2) -> o1.getWeight() - o2.getWeight());
    }
}

小结

以上就是Java函数式编程之通过行为参数化传递代码的详细内容,更多关于Java行为参数化传递代码的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文