当前位置:   article > 正文

什么是过滤器模式(Filter)?_filter模式

filter模式

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

  1. 意图
    以不同的标准过滤一组对象,通过逻辑运算解耦组合。
  2. 类图
    在这里插入图片描述
  3. 实例
static class Person {
        private String name;
        private String gender;
        private String marry;

        public String getName() {
            return name;
        }

        public Person setName(String name) {
            this.name = name;
            return this;
        }

        public String getGender() {
            return gender;
        }

        public Person setGender(String gender) {
            this.gender = gender;
            return this;
        }

        public String getMarry() {
            return marry;
        }

        public Person setMarry(String marry) {
            this.marry = marry;
            return this;
        }

        public static Person create () {
            return new Person();
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", gender='" + gender + '\'' +
                    ", marry='" + marry + '\'' +
                    '}';
        }
    }

    static class PersonBuilder {
        List<Person> people;
        private PersonBuilder () {
            people = new ArrayList<>(16);
        }

        public PersonBuilder addPerson (Person person) {
            people.add(person);
            return this;
        }

        public static PersonBuilder create () {
            return new PersonBuilder();
        }

        public List<Person> getPeople () {
            return people;
        }
    }

    interface Filter {
        /**
         * 过滤
         * @param people
         * @return
         */
        List<Person> filter (List<Person> people);
    }

    static class MaleFilter implements Filter {
        @Override
        public List<Person> filter(List<Person> people) {
            return people.stream().filter(person -> "male".equals(person.getGender())).collect(Collectors.toList());
        }
    }

    static class FemaleFilter implements Filter {
        @Override
        public List<Person> filter(List<Person> people) {
            return people.stream().filter(person -> "female".equals(person.getGender())).collect(Collectors.toList());
        }
    }

    static class MarryFilter implements Filter {
        @Override
        public List<Person> filter(List<Person> people) {
            return people.stream().filter(person -> "married".equals(person.getMarry())).collect(Collectors.toList());
        }
    }

    static class AndFilter implements Filter {
        private Filter filter1;
        private Filter filter2;

        public AndFilter (Filter filter1, Filter filter2) {
            this.filter1 = filter1;
            this.filter2 = filter2;
        }

        @Override
        public List<Person> filter(List<Person> people) {
            return filter2.filter(filter1.filter(people));
        }
    }

    static class OrFilter implements Filter {
        private Filter filter1;
        private Filter filter2;

        public OrFilter (Filter filter1, Filter filter2) {
            this.filter1 = filter1;
            this.filter2 = filter2;
        }

        @Override
        public List<Person> filter(List<Person> people) {
            List<Person> f1 = filter1.filter(people);
            filter2.filter(people).forEach(person -> {if (!f1.contains(person)) {f1.add(person);}});
            return f1;
        }
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  1. 测试
public static void main(String[] args) {
        PersonBuilder builder = PersonBuilder.create().addPerson(Person.create().setName("hello").setGender("male").setMarry("married"))
                .addPerson(Person.create().setName("good").setGender("female").setMarry("married"))
                .addPerson(Person.create().setName("jjiji").setGender("female").setMarry("nomarried"))
                .addPerson(Person.create().setName("kkkk").setGender("male").setMarry("married"));
        List<Person> people = builder.getPeople();

        AndFilter andFilter = new AndFilter(new MaleFilter(), new MarryFilter());

        System.out.println("male and married:");
        andFilter.filter(people).forEach(System.out::println);
        System.out.println();

        OrFilter orFilter = new OrFilter(new FemaleFilter(), new MarryFilter());

        System.out.println("female or married:");
        orFilter.filter(people).forEach(System.out::println);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

运行结果:

male and married:
Person{name='hello', gender='male', marry='married'}
Person{name='kkkk', gender='male', marry='married'}

female or married:
Person{name='good', gender='female', marry='married'}
Person{name='jjiji', gender='female', marry='nomarried'}
Person{name='hello', gender='male', marry='married'}
Person{name='kkkk', gender='male', marry='married'}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

想看更多吗?请访问:设计模式

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/482020
推荐阅读
相关标签
  

闽ICP备14008679号