• java 通过行为参数化传递代码,来解决不断增长的需求


    1, 通过定义不同的谓词接口来区分不同的苹果的重量,如果后续有更多的需求,只需要添加更多的谓词即可

    
    package org.example;
    
    
    import java.util.ArrayList;
    import java.util.List;
    
    enum Color {
        RED, GREEN, YELLOW
    }
    
    class Apple {
        private Integer weight;
        private Color color;
    
        public Apple(Integer weight, Color color) {
            this.weight = weight;
            this.color = color;
        }
    
        public Integer getWeight() {
            return weight;
        }
    
        public void setWeight(Integer weight) {
            this.weight = weight;
        }
    
        public Color getColor() {
            return color;
        }
    
        public void setColor(Color color) {
            this.color = color;
        }
    }
    
    interface ApplePredicate {
        boolean test(Apple apple);
    }
    
    class AppleGreenColorPredicate implements ApplePredicate {
        // 选择绿色苹果的谓词
        @Override
        public boolean test(Apple apple) {
            return Color.GREEN.equals(apple.getColor());
        }
    }
    
    class AppleHeavyWeightPredicate implements ApplePredicate {
        // 选择重量大于150克的谓词
        @Override
        public boolean test(Apple apple) {
            return apple.getWeight() > 150;
        }
    }
    
    
    public class Main {
    
        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;
        }
    
    
        public static void main(String[] args) {
            List<Apple> inventory = new ArrayList<>();
            inventory.add(new Apple(300,Color.RED));
            inventory.add(new Apple(12,Color.RED));
            inventory.add(new Apple(350,Color.GREEN));
            inventory.add(new Apple(200,Color.YELLOW));
    
            // 方便的筛选绿色苹果和重苹果
            List<Apple> result = filterApples(inventory, new AppleGreenColorPredicate());
            result = filterApples(result, new AppleHeavyWeightPredicate());
            for (var apple :
                    result) {
                System.out.println(apple.getColor() + ":" + apple.getWeight());
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    2,上述定义接口实现的方式过于啰嗦和繁杂可以使用匿名类和lamble表达式进行简化

    2.1, 匿名内部类
    List<Apple> result = filterApples(inventory, new ApplePredicate() {
        @Override
        public boolean test(Apple apple) {
            return Color.GREEN.equals(apple.getColor());
        }
    });
    result = filterApples(inventory, new ApplePredicate() {
        @Override
        public boolean test(Apple apple) {
            return apple.getWeight() > 150;
        }
    });
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    2.2 lamble表达式
    List<Apple> result = filterApples(inventory, (Apple apple)->apple.getColor().equals(Color.GREEN));
    result = filterApples(result, (Apple apple)->apple.getWeight()>150);
    
    • 1
    • 2

    3,更进一步的可以将针对苹果的list类型进行抽象化,用于其他的水果

    interface Predicate <T>{
        boolean test(T t);
    }
    
    public static <T> List<T> filter(List<T> inventory, Predicate<T> p) {
        // 通过谓词筛选T
        List<T> result = new ArrayList<>();
        for (T e :
                inventory) {
            if (p.test(e)) {
                result.add(e);
            }
        }
        return result;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    深度学习(3)---PyTorch中的张量
    (StateFlow & ShareFlow) VS (Flow & LiveData)
    moment.js 实现获取近一月、近三月、近一年、一月后、一年后等
    java基于Springboot+vue的个人口腔牙齿卫生保护产品销售购物网站 elementui
    ApplicationContext接口解读
    【springboot】9、Rest风格请求及视图解析
    以太网_底层
    Java编码规范总结
    工业光网助力工业企业数字化转型发展
    第一章 基础算法
  • 原文地址:https://blog.csdn.net/qq_38130747/article/details/133514189