美文网首页
Java8 in Action chap2

Java8 in Action chap2

作者: grace_fang | 来源:发表于2019-12-30 15:25 被阅读0次

通过行为参数化传递代码

1.行为参数化定义:

可以帮助你处理频繁变更的需求的一种软件开发模式。
一个方法可以接收不同行为作为参数,然后去执行。

2.学习这个能做什么?

对你的现有代码进行改进:
从而更灵活适应不断变化的需求。

package lambdasinaction.chap2;

import java.util.*;

/**
 * @author jincheng
 * @date 2019/12/30
 * java in action chap2
 * 思路:通过行为参数化传递代码 + 简单的策略模式
 */
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"));   

        // [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
        List<Apple> greenApples = filterApplesByColor(inventory, "green");
        System.out.println(greenApples);

        // [Apple{color='red', weight=120}]
        List<Apple> redApples = filterApplesByColor(inventory, "red");
        System.out.println(redApples);

        // [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
        List<Apple> greenApples2 = filter(inventory, new AppleColorPredicate());
        System.out.println(greenApples2);

        // [Apple{color='green', weight=155}]
        List<Apple> heavyApples = filter(inventory, new AppleWeightPredicate());
        System.out.println(heavyApples);

        // []
        List<Apple> redAndHeavyApples = filter(inventory, new AppleRedAndHeavyPredicate());
        System.out.println(redAndHeavyApples);

        // [Apple{color='red', weight=120}]
        List<Apple> redApples2 = filter(inventory, new ApplePredicate() {
            public boolean test(Apple a){
                return a.getColor().equals("red"); 
            }
        });
        System.out.println(redApples2);

    }

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

    public static List<Apple> filterApplesByColor(List<Apple> inventory, String color){
        List<Apple> result = new ArrayList<>();
        for(Apple apple: inventory){
            if(apple.getColor().equals(color)){
                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;
    }


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

    public static class Apple {
        private int weight = 0;
        private String color = "";

        public Apple(int weight, String color){
            this.weight = weight;
            this.color = color;
        }

        public Integer getWeight() {
            return weight;
        }

        public void setWeight(Integer weight) {
            this.weight = weight;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public String toString() {
            return "Apple{" +
                    "color='" + color + '\'' +
                    ", weight=" + weight +
                    '}';
        }
    }

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

    static class AppleWeightPredicate implements ApplePredicate{
        public boolean test(Apple apple){
            return apple.getWeight() > 150; 
        }
    }
    static class AppleColorPredicate implements ApplePredicate{
        public boolean test(Apple apple){
            return "green".equals(apple.getColor());
        }
    }

    static class AppleRedAndHeavyPredicate implements ApplePredicate{
        public boolean test(Apple apple){
            return "red".equals(apple.getColor()) 
                    && apple.getWeight() > 150; 
        }
    }
}

相关文章

  • Java8 in Action chap2

    通过行为参数化传递代码 1.行为参数化定义: 可以帮助你处理频繁变更的需求的一种软件开发模式。一个方法可以接收不同...

  • 新年第一本书

    java8实战下周写总结

  • Java8 学习笔记

    @(in action系列)[java8, lambda, stream] Java8 学习 java8 能高效的...

  • java8 in action中提供的源码导入idea后出现编译

    最近看著名的《java8 in action》,发现在github上有提供书中源码,于是兴冲冲的将源代码导入到了i...

  • java8 in action行为参数化

    行为参数化:拿出一个代码块,把它准备好却不去执行它。这个代码块以后可以被你程序的其他部分调用,这意味着你可以推迟这...

  • 行为参数化------Java8 in action

    行为参数化是一种类似于策略设计模式的模式,可以轻松地适应不断变化的需求。 具体而言:行为参数化就是让一个方法接受多...

  • Java8

    Java8 in action 没有共享的可变数据,将方法和函数即代码传递给其他方法的能力就是我们平常所说的函数式...

  • Java Array

    学习材料 https://joshhug.gitbooks.io/hug61b/content/chap2/cha...

  • chap2

    1. 大多数计算机使用块(8位) 或者一个字节作为最小的可寻址的内存单位。 2. 32位程序 or 64位程序,区...

  • Action Action Action

    纽约时间比加州早3个小时, New York is 3 hours ahead of California, 但加...

网友评论

      本文标题:Java8 in Action chap2

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