美文网首页
Java实现——策略模式

Java实现——策略模式

作者: 李小二的倔强 | 来源:发表于2021-05-20 17:43 被阅读0次

    现有一个苹果类,有两个基本属性一个是颜色-color ,一个是重量-weight

    public class Apple {
    
        private String color;
        private long weight;
        
        //省略get/set 方法
    }
    
    一、单个参数的需求

    现在有一个需求需要获取绿色的苹果

        public static List<Apple> findGreenApple(List<Apple> apples) {
    
            List<Apple> list = new ArrayList<>();
    
            for (Apple apple : apples) {
                if ("green".equals(apple.getColor())) {
                    list.add(apple);
                }
            }
            return list;
        }
    

    测试方法如下:

    
    
        public static void main(String[] args) throws InterruptedException {
            List<Apple> list = Arrays.asList(new Apple("green", 150), new Apple("yellow", 120), new Apple("green", 170));
            List<Apple> greenApples = findGreenApple(list);
            System.out.println(greenApples);
        }
    
    二、两个参数需求

    现在不光要获取绿色的苹果,还有可能获取红色的苹果

        public static List<Apple> findApple(List<Apple> apples, String color) {
            List<Apple> list = new ArrayList<>();
    
            for (Apple apple : apples) {
                if (color.equals(apple.getColor())) {
                    list.add(apple);
                }
            }
            return list;
        }
    

    测试方法如下:

        public static void main(String[] args) throws InterruptedException {
            List<Apple> list = Arrays.asList(new Apple("green", 150), new Apple("yellow", 120), new Apple("green", 170));
    //        List<Apple> greenApples = findGreenApple(list);
    
           List<Apple> greenApples = findApple(list, "green");
            System.out.println(greenApples);
        }
    

    这样就可以实现,无论什么颜色的苹果都可以这个方法获取,但是缺点也暴露出来了,需求改变的同时参数在一直改变,如果即根据颜色又根据重量得到苹果呢?那就得增加重量参数

    如何满足各种条件又不用更改很多的程序呢?

    三、策略模式

    定义一个策略接口

        public interface AppleFilter {
            boolean filter(Apple apple);
        }
    

    编写获取苹果的方法,并且传入策略

        public static List<Apple> findApple(List<Apple> apples, AppleFilter appleFilter) {
            List<Apple> list = new ArrayList<>();
    
            for (Apple apple : apples) {
                if (appleFilter.filter(apple))
                    list.add(apple);
            }
            return list;
        }
    

    需要哪种需求,可以对策略接口进行实现

        //重量大于160g的绿色苹果
        public static class GreenAnd160WeightFilter implements AppleFilter {
            @Override
            public boolean filter(Apple apple) {
                return (apple.getColor().equals("green") && apple.getWeight() >= 160);
            }
        }
    
        //重量小于150g的黄色苹果
        public static class YellowLess150WeightFilter implements AppleFilter {
            @Override
            public boolean filter(Apple apple) {
                return (apple.getColor().equals("yellow") && apple.getWeight() < 150);
            }
        }
    

    测试方法如下:

        public static void main(String[] args) throws InterruptedException {
            List<Apple> list = Arrays.asList(new Apple("green", 150), new Apple("yellow", 120), new Apple("green", 170));
    //        List<Apple> greenApples = findGreenApple(list);
    //        assert greenApples.size() == 2;
    
           /* List<Apple> greenApples = findApple(list, "green");
            System.out.println(greenApples);
    
            List<Apple> redApples = findApple(list, "red");
            System.out.println(redApples);*/
            List<Apple> result = findApple(list, new GreenAnd160WeightFilter());
            System.out.println(result);
    

    方法二、内部类方式

    测试方法如下:

        public static void main(String[] args) throws InterruptedException {
            List<Apple> list = Arrays.asList(new Apple("green", 150), new Apple("yellow", 120), new Apple("green", 170));
    //        List<Apple> greenApples = findGreenApple(list);
    //        assert greenApples.size() == 2;
    
           /* List<Apple> greenApples = findApple(list, "green");
            System.out.println(greenApples);
    
            List<Apple> redApples = findApple(list, "red");
            System.out.println(redApples);*/
    /*        List<Apple> result = findApple(list, new GreenAnd160WeightFilter());
            System.out.println(result);*/
    
            List<Apple> yellowList = findApple(list, new AppleFilter() {
                @Override
                public boolean filter(Apple apple) {
                    return "yellow".equals(apple.getColor());
                }
            });
        }
    

    end

    相关文章

      网友评论

          本文标题:Java实现——策略模式

          本文链接:https://www.haomeiwen.com/subject/uxlbjltx.html