美文网首页
过滤器模式

过滤器模式

作者: Stroman | 来源:发表于2018-11-04 11:32 被阅读22次

    有啥用?

    就是从一大堆对象中筛选出符合条件的对象来,就这么简单。

    类图

    类图.png

    输出

    All objects:
    condition 2 : 466002798
    condition 2 : 1784662007
    condition 0 : 997110508
    condition 0 : 509886383
    condition 1 : 1854778591
    condition 2 : 2054798982
    condition 2 : 885951223
    condition 0 : 191382150
    condition 0 : 142666848
    condition 2 : 1060830840
    
    condition 0 : 997110508
    condition 0 : 509886383
    condition 0 : 191382150
    condition 0 : 142666848
    
    condition 1 : 1854778591
    
    condition 2 : 466002798
    condition 2 : 1784662007
    condition 2 : 2054798982
    condition 2 : 885951223
    condition 2 : 1060830840
    
    Process finished with exit code 0
    

    调用

    package com.company;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    public class Main {
    
        public static void main(String[] args) {
        // write your code here
            int objectSum = 10;
            List<TargetClass> sourceList = new ArrayList<>();
            System.out.println("All objects:");
            for (int couter = 0;couter < objectSum;couter++) {
                TargetClass tempObject = new TargetClass();
                tempObject.objectIndex = new Random().nextInt(3);
                System.out.println("condition " + tempObject.objectIndex + " : " +tempObject.hashCode());
                sourceList.add(tempObject);
            }
            System.out.println();
    
            List<TargetClass> resultList0 = new FirstConditionFilter().filterSourceObjectsList(sourceList);
            for (TargetClass iterator : resultList0) {
                System.out.println("condition 0 : " + iterator.hashCode());
            }
            System.out.println();
    
            List<TargetClass> resultList1 = new SecondConditionFilter().filterSourceObjectsList(sourceList);
            for (TargetClass iterator : resultList1) {
                System.out.println("condition 1 : " + iterator.hashCode());
            }
            System.out.println();
    
            List<TargetClass> resultList2 = new ThirdConditionFilter().filterSourceObjectsList(sourceList);
            for (TargetClass iterator : resultList2) {
                System.out.println("condition 2 : " + iterator.hashCode());
            }
        }
    }
    
    

    被依赖的对象类型

    package com.company;
    
    public class TargetClass {
        public int objectIndex;
    }
    
    

    过滤器接口

    package com.company;
    
    import java.util.List;
    
    public interface FilterInterface {
        List<TargetClass> filterSourceObjectsList(List<TargetClass> sourceList);
    }
    
    

    过滤器0

    package com.company;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class FirstConditionFilter implements FilterInterface {
        @Override
        public List<TargetClass> filterSourceObjectsList(List<TargetClass> sourceList) {
            List<TargetClass> resultList = new ArrayList<>();
            for (TargetClass iterator:sourceList) {
                if (iterator.objectIndex == 0)resultList.add(iterator);
            }
            return resultList;
        }
    }
    
    

    过滤器1

    package com.company;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class SecondConditionFilter implements FilterInterface {
        @Override
        public List<TargetClass> filterSourceObjectsList(List<TargetClass> sourceList) {
            List<TargetClass> resultList = new ArrayList<>();
            for (TargetClass iterator:sourceList) {
                if (iterator.objectIndex == 1)resultList.add(iterator);
            }
            return resultList;
        }
    }
    
    

    过滤器2

    package com.company;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ThirdConditionFilter implements FilterInterface {
        @Override
        public List<TargetClass> filterSourceObjectsList(List<TargetClass> sourceList) {
            List<TargetClass> resultList = new ArrayList<>();
            for (TargetClass iterator:sourceList) {
                if (iterator.objectIndex == 2)resultList.add(iterator);
            }
            return resultList;
        }
    }
    
    

    多谢捧场

    如果您觉得我的文章有价值,那么赏脸打赏一个,鄙人感激不尽。不过,不打赏看看也是好的,如果有不对的地方,还请您多多指正。


    相关文章

      网友评论

          本文标题:过滤器模式

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