• 设计模式-过滤模式


    过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。

    下面创建了CriteriaMale、CriteriaFemale、CriteriaSingle、AndCriteria、OrCriteria都实现了Criteria接口并重写了meetCriteria方法,在Client中可以根据需要过滤出所需要的数据

    1.创建Criteria接口和Person实体类

    package com.design.demo.filter;
    
    /**
     * @author: GuanBin
     * @date: Created in 下午11:11 2019/8/18
     */
    public class Person {
    
        private String name;
        private String gender;
        private String maritalStatus;
    
        public Person(String name, String gender, String maritalStatus) {
            this.name = name;
            this.gender = gender;
            this.maritalStatus = maritalStatus;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getGender() {
            return gender;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public String getMaritalStatus() {
            return maritalStatus;
        }
    
        public void setMaritalStatus(String maritalStatus) {
            this.maritalStatus = maritalStatus;
        }
    }
    View Code
    package com.design.demo.filter;
    
    import java.util.List;
    
    /**
     * @author: GuanBin
     * @date: Created in 下午11:14 2019/8/18
     */
    public interface Criteria {
    
        public List<Person> meetCriteria(List<Person> personList);
    }

    2.创建CriteriaMale

    package com.design.demo.filter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author: GuanBin
     * @date: Created in 下午11:15 2019/8/18
     */
    public class CriteriaMale implements Criteria {
    
        @Override
        public List<Person> meetCriteria(List<Person> personList) {
            ArrayList<Person> malePersons = new ArrayList<>();
            for (Person person : personList) {
                if (person.getGender().equalsIgnoreCase("MALE")) {
                    malePersons.add(person);
                }
            }
            return malePersons;
        }
    }

    3.创建CriteriaFemale

    package com.design.demo.filter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author: GuanBin
     * @date: Created in 下午11:15 2019/8/18
     */
    public class CriteriaFemale implements Criteria {
    
        @Override
        public List<Person> meetCriteria(List<Person> personList) {
            ArrayList<Person> femalePersons = new ArrayList<>();
            for (Person person : personList) {
                if (person.getGender().equalsIgnoreCase("FEMALE")) {
                    femalePersons.add(person);
                }
            }
            return femalePersons;
        }
    }

    4.创建CriteriaSingle

    package com.design.demo.filter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author: GuanBin
     * @date: Created in 下午11:15 2019/8/18
     */
    public class CriteriaSingle implements Criteria {
    
        @Override
        public List<Person> meetCriteria(List<Person> personList) {
            ArrayList<Person> singlePersons = new ArrayList<>();
            for (Person person : personList) {
                if (person.getMaritalStatus().equalsIgnoreCase("SINGLE")) {
                    singlePersons.add(person);
                }
            }
            return singlePersons;
        }
    }

    5.创建AndCriteria

    package com.design.demo.filter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author: GuanBin
     * @date: Created in 下午11:15 2019/8/18
     */
    public class AndCriteria implements Criteria {
    
       private  Criteria criteria;
       private  Criteria otherCriteria;
    
        public AndCriteria(Criteria criteria, Criteria otherCriteria) {
            this.criteria = criteria;
            this.otherCriteria = otherCriteria;
        }
    
        @Override
        public List<Person> meetCriteria(List<Person> personList) {
            List<Person> people = criteria.meetCriteria(personList);
            return otherCriteria.meetCriteria(people);
        }
    }

    6.创建OrCriteria

    package com.design.demo.filter;
    
    import java.util.List;
    
    /**
     * @author: GuanBin
     * @date: Created in 下午11:15 2019/8/18
     */
    public class OrCriteria implements Criteria {
    
       private  Criteria criteria;
       private  Criteria otherCriteria;
    
        public OrCriteria(Criteria criteria, Criteria otherCriteria) {
            this.criteria = criteria;
            this.otherCriteria = otherCriteria;
        }
    
        @Override
        public List<Person> meetCriteria(List<Person> personList) {
            List<Person> people = criteria.meetCriteria(personList);
            List<Person> otherPeople = otherCriteria.meetCriteria(people);
    
            otherPeople.stream().forEach(person -> {
                if(!people.contains(person)){
                    people.add(person);
                }
            });
    
            return people;
    
        }
    }

    7.创建Client类

    package com.design.demo.filter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author: GuanBin
     * @date: Created in 下午11:27 2019/8/18
     */
    public class Client {
    
        public static void main(String[] args) {
            List<Person> people = new ArrayList<>();
    
            people.add(new Person("lisi","Male","Single"));
            people.add(new Person("zhangsan","Male","Single"));
            people.add(new Person("wangwu","Female","Married"));
            people.add(new Person("zhangliu","Male","Married"));
            people.add(new Person("wangsi","Female","Single"));
    
            CriteriaMale criteriaMale = new CriteriaMale();
            CriteriaFemale criteriaFemale = new CriteriaFemale();
            CriteriaSingle criteriaSingle = new CriteriaSingle();
    
            //即为女士又是单身的人
            AndCriteria andCriteria = new AndCriteria(criteriaMale,criteriaSingle);
            //男士和单身的人
            OrCriteria orCriteria = new OrCriteria(criteriaFemale,criteriaSingle);
    
            System.out.println("-----Male");
            printPersons(criteriaMale.meetCriteria(people));
            System.out.println("-----Female");
            printPersons(criteriaFemale.meetCriteria(people));
            System.out.println("-----Single");
            printPersons(criteriaSingle.meetCriteria(people));
            System.out.println("-----Male and Single");
            printPersons(andCriteria.meetCriteria(people));
            System.out.println("-----Femal or Single");
            printPersons(orCriteria.meetCriteria(people));
        }
    
        public static void printPersons(List<Person> persons){
            for (Person person : persons) {
                System.out.println("Person : [ Name : " + person.getName()
                        +", Gender : " + person.getGender()
                        +", Marital Status : " + person.getMaritalStatus()
                        +" ]");
            }
        }
    }
  • 相关阅读:
    高性能计算发展简史
    软件里有“slave”关键字算不算种族歧视
    LAXCUS集群的松耦合和紧耦合性能对比
    《操作系统》课程笔记(Ch12-大容量存储结构)
    《剑指Offer》部分简单题题解
    ASP.Net Core 5.0 MVC中AOP思想的体现(五种过滤器)并结合项目案例说明过滤器的用法
    扒一扒 剪视频-致力打造最好用的视频创作导航
    C#设计模式02——原型模式的写法
    C#设计模式03——简单工厂的写法
    C#设计模式04——工厂方法的写法
  • 原文地址:https://www.cnblogs.com/guanbin-529/p/11380147.html
Copyright © 2020-2023  润新知