zoukankan      html  css  js  c++  java
  • 《Java 8 实战》(一)——通过行为参数化传递代码

    行为参数化是用来处理频繁变更的需求的一种软件开发模式。拿出一个代码块,把它准备好却不去执行它。这个代码块以后可以被程序的其他部分调用,也就是推迟这块代码的执行。

    行为参数化:让方法接受多种行为作为参数,并在内部使用,来完成不同的行为。

    传递代码,就是将新行为作为参数传递给方法。但在java 8之前实现起来很啰嗦,为借口生命许多只用一次的实体类而造成的啰嗦代码,在java 8之前可以用匿名类来减少。但java 8后,可以使用Lamda。

    package lamdainaction.chap1;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    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"));
            
            List<Apple> result1 = filterApples1(inventory);
            System.out.println(result1);
            
            List<Apple> result2 = filterApples2(inventory);
            System.out.println(result2);
            
            
            List<Apple> result3 = filterApples(inventory, new AppleColorPredicate());
            System.out.println(result3);
            
            List<Apple> result4 = filterApples(inventory, new AppleWeightPredicate());
            System.out.println(result4);
            
            List<Apple> result5 = filterApples(inventory, new ApplePredicate(){
    
                @Override
                public boolean test(Apple apple) {
                    // TODO Auto-generated method stub
                    return "red".equals(apple.getColor());
                }
                
            });
            System.out.println(result5);


         
    List<Apple> result6 = filterApples(inventory, Apple apple -> "red".equals(apple.getColor());

    System.out.println(result6);
    
        }
        
        public static List<Apple> filterApples1(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> filterApples2(List<Apple> inventory) {
            List<Apple> result = new ArrayList<>();
            for (Apple apple : inventory) {
                if (apple.getWeight() > 150 ) {
                    result.add(apple);
                }
            }
            return result;
        }
        
        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;
        }
    
    }
    
    interface ApplePredicate {
        boolean test(Apple apple);
    }
    
    class AppleColorPredicate implements ApplePredicate {
    
        @Override
        public boolean test(Apple apple) {
            
            return "green".equals(apple.getColor());
        }
        
    }
    
    class AppleWeightPredicate implements ApplePredicate {
    
        @Override
        public boolean test(Apple apple) {
            
            return apple.getWeight() > 150;
        }
        
    }
    class Apple {
        private int weight;
        private String color;
        
        public Apple(int weight, String color) {
            this.setWeight(weight);
            this.setColor(color);
        }
    
        public int getWeight() {
            return weight;
        }
    
        public void setWeight(int 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 +"}";
        }
        
    }

    谓词(predicate):一个返回boolean值的函数。例如苹果,需要根据Apple的某些属性(它是绿色的吗,它的重量超过150g吗)来返回一个boolean值。

    Lamda:

    List<Apple> result6 = filterApples(inventory, Apple apple -> "red".equals(apple.getColor());

    -> 前的 Apple apple 是ApplePredicate.test(Apple apple)中的参数
    -> 后的 "red".equals(apple.getColor()) 是ApplePredicate.test(Apple apple)的方法体
  • 相关阅读:
    F. 蚂蚁装修
    D. 蚂蚁平面
    B. 蚂蚁觅食(二)
    A 蚂蚁觅食
    落谷 P1734 最大约数和
    F
    D
    Http头 Range、Content-Range(http断点续传原理)
    Http头 Range、Content-Range
    XCODE 4.5 IOS多语言设置 及NSLocalizedString和NSLocalizedStringFromTable的用法。
  • 原文地址:https://www.cnblogs.com/IvySue/p/6734961.html
Copyright © 2011-2022 走看看