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

打印 上一主题 下一主题

主题 922|帖子 922|积分 2766

1, 通过定义不同的谓词接口来区分不同的苹果的重量,如果后续有更多的需求,只需要添加更多的谓词即可
  1. package org.example;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. enum Color {
  5.     RED, GREEN, YELLOW
  6. }
  7. class Apple {
  8.     private Integer weight;
  9.     private Color color;
  10.     public Apple(Integer weight, Color color) {
  11.         this.weight = weight;
  12.         this.color = color;
  13.     }
  14.     public Integer getWeight() {
  15.         return weight;
  16.     }
  17.     public void setWeight(Integer weight) {
  18.         this.weight = weight;
  19.     }
  20.     public Color getColor() {
  21.         return color;
  22.     }
  23.     public void setColor(Color color) {
  24.         this.color = color;
  25.     }
  26. }
  27. interface ApplePredicate {
  28.     boolean test(Apple apple);
  29. }
  30. class AppleGreenColorPredicate implements ApplePredicate {
  31.     // 选择绿色苹果的谓词
  32.     @Override
  33.     public boolean test(Apple apple) {
  34.         return Color.GREEN.equals(apple.getColor());
  35.     }
  36. }
  37. class AppleHeavyWeightPredicate implements ApplePredicate {
  38.     // 选择重量大于150克的谓词
  39.     @Override
  40.     public boolean test(Apple apple) {
  41.         return apple.getWeight() > 150;
  42.     }
  43. }
  44. public class Main {
  45.     public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate p) {
  46.         // 通过谓词筛选苹果
  47.         List<Apple> result = new ArrayList<>();
  48.         for (Apple apple :
  49.                 inventory) {
  50.             if (p.test(apple)) {
  51.                 result.add(apple);
  52.             }
  53.         }
  54.         return result;
  55.     }
  56.     public static void main(String[] args) {
  57.         List<Apple> inventory = new ArrayList<>();
  58.         inventory.add(new Apple(300,Color.RED));
  59.         inventory.add(new Apple(12,Color.RED));
  60.         inventory.add(new Apple(350,Color.GREEN));
  61.         inventory.add(new Apple(200,Color.YELLOW));
  62.         // 方便的筛选绿色苹果和重苹果
  63.         List<Apple> result = filterApples(inventory, new AppleGreenColorPredicate());
  64.         result = filterApples(result, new AppleHeavyWeightPredicate());
  65.         for (var apple :
  66.                 result) {
  67.             System.out.println(apple.getColor() + ":" + apple.getWeight());
  68.         }
  69.     }
  70. }
复制代码
2,上述定义接口实现的方式过于啰嗦和繁杂可以使用匿名类和lamble表达式进行简化

2.1, 匿名内部类
  1. List<Apple> result = filterApples(inventory, new ApplePredicate() {
  2.     @Override
  3.     public boolean test(Apple apple) {
  4.         return Color.GREEN.equals(apple.getColor());
  5.     }
  6. });
  7. result = filterApples(inventory, new ApplePredicate() {
  8.     @Override
  9.     public boolean test(Apple apple) {
  10.         return apple.getWeight() > 150;
  11.     }
  12. });
复制代码
2.2 lamble表达式
  1. List<Apple> result = filterApples(inventory, (Apple apple)->apple.getColor().equals(Color.GREEN));
  2. result = filterApples(result, (Apple apple)->apple.getWeight()>150);
复制代码
3,更进一步的可以将针对苹果的list类型进行抽象化,用于其他的水果
  1. interface Predicate <T>{
  2.     boolean test(T t);
  3. }
  4. public static <T> List<T> filter(List<T> inventory, Predicate<T> p) {
  5.     // 通过谓词筛选T
  6.     List<T> result = new ArrayList<>();
  7.     for (T e :
  8.             inventory) {
  9.         if (p.test(e)) {
  10.             result.add(e);
  11.         }
  12.     }
  13.     return result;
  14. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

络腮胡菲菲

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表