zoukankan      html  css  js  c++  java
  • Java8新特性系列一:行为参数化

    1. 什么是行为参数化

     行为参数化主要是为了应对需求的变化,减少代码的变动冗余、变动,应对行为标准建模,方式可以为定义接口,下面以一个例子说明行为参数化的必要性

    2. 需求一

     有一个Apple类,属性包括颜色、重量等,现在用户需求为:筛选出苹果重量>100的苹果,这个需求很简单,很容易想到的实现如下:

    package lambdasinaction.chap1;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * @Author oxygen
     * @Date 2020/7/20 0020
     * @Version V1.0
     **/
    public class MyFilterApple {
    
        public static void main(String[] args) {
            List<Apple> inventory = Arrays.asList(new Apple(100, "red"), new Apple(180, "green"));
            List<Apple> apple = findWeightGH100Apple(inventory);
        }
    
        private static List<Apple> findWeightGH100Apple(List<Apple> inventory) {
            List<Apple> result = new ArrayList<>();
            for (Apple apple : inventory) {
                if (apple.getWeight() > 100) {
                    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 void setMethod&getMethod(...)
        }
    }
    

    3. 需求二

     用户改变增加需求:

     筛选出重量>100而且颜色为red的苹果,很容易想到的实现为增加一个实现方法,但是随着需求的增加,会增加N多筛选的方法,同时冗余代码

     private static List<Apple> findWeightGH100Apple(List<Apple> inventory) {
            List<Apple> result = new ArrayList<>();
            for (Apple apple : inventory) {
                if (apple.getWeight()>100 && "green".equals(apple.getColor())){
                    result.add(apple);
                }
            }
        }
    

    4. 改进一

    4.1 定义接口传递行为

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

    4.2 多个类实现具体的逻辑

       /**
         *过滤重量大于100而且颜色是红色的苹果
         */
        public class AppleColorPredicate implements ApplePredicate {
            @Override
            public boolean test(Apple apple) {
                return "red".equals(apple.getColor()) && apple.getWeight() > 100;
            }
        }
    
        /**
         * 过滤重量>150的苹果
         */
        public class AppleHeavyWeightPredicate implements ApplePredicate {
            @Override
            public boolean test(Apple apple) {
                return apple.getWeight() > 150;
            }
        }
    

    4.3 定义实现方法

    List<Apple> inventory = Arrays.asList(new Apple(100, "red"), new Apple(180, "green"));
    MyFilterApple.AppleColorPredicate p = new MyFilterApple().new AppleColorPredicate();
    List<Apple> apples = filterApples(inventory, p);
    
    MyFilterApple.AppleHeavyWeightPredicate p2 = new MyFilterApple().new AppleHeavyWeightPredicate();
    List<Apple> apples1 = filterApples(inventory, p2);
    
    private 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;
    }

    5. 改进二

     filterApples方法只能接受对象,所以必须把代码包裹在ApplePredicate对象里,因为通过一个实现了test方法的对象来传递布尔表达式

     可用匿名内部类和Lambd改进

    List<Apple> apples3 = filterApples(inventory, new ApplePredicate() {//匿名内部类优化
      @Override
      public boolean test(Apple apple) {
        return apple.getWeight() > 150;
           }
        });
    List<Apple> apples3 = filterApples(inventory, apple -> apple.getWeight() > 100);//Lambda表达式优化
    

      

  • 相关阅读:
    使用 udev 高效、动态地管理 Linux 设备文件
    【技术贴】visual stdio 2005下载地址,vs2005下载地址 vs2005正版破解 v
    【转】无法登陆SQL server 服务器的解决办法
    【技术贴】visual stdio 2005下载地址,vs2005下载地址 vs2005正版破解 v
    【技术贴】远程桌面连接 时“由于帐户限制 您无法登陆”的解决办法
    【转】桌面快捷方式打不开的解决办法
    【技术贴】从51下载的网站代码asp源码怎么运行?怎么打开?
    【转】Visual Studio 2005不能调试的错误
    【技术贴】asms文件,安装windows xp原版时,需要“asms”文件,H:\I386\asm
    【技术贴】asms文件,安装windows xp原版时,需要“asms”文件,H:\I386\asm
  • 原文地址:https://www.cnblogs.com/oxygenG/p/13352513.html
Copyright © 2011-2022 走看看