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

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

java 复制代码
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());
        }

    }
}

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

2.1, 匿名内部类
java 复制代码
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;
    }
});
2.2 lamble表达式
java 复制代码
List<Apple> result = filterApples(inventory, (Apple apple)->apple.getColor().equals(Color.GREEN));
result = filterApples(result, (Apple apple)->apple.getWeight()>150);

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

java 复制代码
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;
}
相关推荐
程序员南飞38 分钟前
ps aux | grep smart_webrtc这条指令代表什么意思
java·linux·ubuntu·webrtc
弥琉撒到我41 分钟前
微服务swagger解析部署使用全流程
java·微服务·架构·swagger
一颗花生米。2 小时前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
问道飞鱼2 小时前
Java基础-单例模式的实现
java·开发语言·单例模式
学习使我快乐012 小时前
JS进阶 3——深入面向对象、原型
开发语言·前端·javascript
通信仿真实验室3 小时前
(10)MATLAB莱斯(Rician)衰落信道仿真1
开发语言·matlab
勿语&3 小时前
Element-UI Plus 暗黑主题切换及自定义主题色
开发语言·javascript·ui
ok!ko5 小时前
设计模式之原型模式(通俗易懂--代码辅助理解【Java版】)
java·设计模式·原型模式
2402_857589366 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
吾爱星辰6 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin