过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
实现
我们将创建一个 Person 对象、Criteria 接口和实现了该接口的实体类,来过滤 Person 对象的列表。CriteriaPatternDemo,我们的演示类使用 Criteria 对象,基于各种标准和它们的结合来过滤 Person 对象的列表。
步骤 1
创建一个类,在该类上应用标准
package com.DesignPatterns.Filter; 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 String getGender() { return gender; } public String getMaritalStatus() { return maritalStatus; } }
步骤 2
为标准(Criteria)创建一个接口。
package com.DesignPatterns.Filter; import java.util.List; /** * * @date 2018年11月7日 * @version 10.28版本 * @说明:规则统一接口 */ public interface Criteria { public List<Person> meetCriteria(List<Person> persons); }
步骤 3
创建实现了 Criteria 接口的实体类。
package com.DesignPatterns.Filter; import java.util.ArrayList; import java.util.List; /** * @date 2018年11月7日 * @version 10.28版本 * @说明:男生类 */ public class CriteriaMale implements Criteria { @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> malePersons = new ArrayList<Person>(); for (Person person : persons) { if(person.getGender().equalsIgnoreCase("MALE")){ malePersons.add(person); } } return malePersons; } }
package com.DesignPatterns.Filter; import java.util.ArrayList; import java.util.List; /** * * @date 2018年11月7日 * @version 10.28版本 * @说明:女生类 */ public class CriteriaFemale implements Criteria { @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> femalePersons = new ArrayList<Person>(); for (Person person : persons) { if(person.getGender().equalsIgnoreCase("FEMALE")){ femalePersons.add(person); } } return femalePersons; } }
package com.DesignPatterns.Filter; import java.util.ArrayList; import java.util.List; /** * @date 2018年11月7日 * @version 10.28版本 * @说明:单身类 */ public class CriteriaSingle implements Criteria { @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> singlePersons = new ArrayList<Person>(); for (Person person : persons) { if(person.getMaritalStatus().equalsIgnoreCase("SINGLE")){ singlePersons.add(person); } } return singlePersons; } }
package com.DesignPatterns.Filter; import java.util.List; /** * @date 2018年11月7日 * @version 10.28版本 * @说明:or单身或者女生 单身或者男生 */ 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> persons) { List<Person> firstCriteriaItems = criteria.meetCriteria(persons); List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persons); for (Person person : otherCriteriaItems) { if (!firstCriteriaItems.contains(person)) { firstCriteriaItems.add(person); } } return firstCriteriaItems; } }
package com.DesignPatterns.Filter; import java.util.List; /** * @date 2018年11月7日 * @version 10.28版本 * @说明:add 单身男生,单身女生 */ 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> persons) { List<Person> firstCriteriaPersons = criteria.meetCriteria(persons); return otherCriteria.meetCriteria(firstCriteriaPersons); } }
步骤4
使用不同的标准(Criteria)和它们的结合来过滤 Person 对象的列表。
package com.DesignPatterns.Filter; import java.util.ArrayList; import java.util.List; public class CriteriaPatternDemo { public static void main(String[] args) { List<Person> persons = new ArrayList<Person>(); persons.add(new Person("Robert","Male", "Single")); persons.add(new Person("John","Male", "Married")); persons.add(new Person("Laura","Female", "Married")); persons.add(new Person("Diana","Female", "Single")); persons.add(new Person("Mike","Male", "Single")); persons.add(new Person("Bobby","Male", "Single")); Criteria male = new CriteriaMale(); Criteria female = new CriteriaFemale(); Criteria single = new CriteriaSingle(); Criteria singleMale = new AndCriteria(single, male); Criteria singleOrFemale = new OrCriteria(single, female); System.out.println("Males男生: "); printPersons(male.meetCriteria(persons)); System.out.println("\nFemales女生: "); printPersons(female.meetCriteria(persons)); System.out.println("\nSingle Males 单身男生: "); printPersons(singleMale.meetCriteria(persons)); System.out.println("\nSingle Or Females 单身或者女生: "); printPersons(singleOrFemale.meetCriteria(persons)); } 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() +" ]"); } } }
结果:
Males男生: Person : [ Name : Robert, Gender : Male, Marital Status : Single ] Person : [ Name : John, Gender : Male, Marital Status : Married ] Person : [ Name : Mike, Gender : Male, Marital Status : Single ] Person : [ Name : Bobby, Gender : Male, Marital Status : Single ] Females女生: Person : [ Name : Laura, Gender : Female, Marital Status : Married ] Person : [ Name : Diana, Gender : Female, Marital Status : Single ] Single Males 单身男生: Person : [ Name : Robert, Gender : Male, Marital Status : Single ] Person : [ Name : Mike, Gender : Male, Marital Status : Single ] Person : [ Name : Bobby, Gender : Male, Marital Status : Single ] Single Or Females 单身或者女生: Person : [ Name : Robert, Gender : Male, Marital Status : Single ] Person : [ Name : Diana, Gender : Female, Marital Status : Single ] Person : [ Name : Mike, Gender : Male, Marital Status : Single ] Person : [ Name : Bobby, Gender : Male, Marital Status : Single ] Person : [ Name : Laura, Gender : Female, Marital Status : Married ]
过滤模式的实现在java8里面有典型的应用方法就是分组操作,可以根据指定的指标进行分组筛选。
Map<Integer, List<Person >> groupMap = persons.stream().collect(Collectors.groupingBy(Person::getGender)); groupMap.forEach((k, v) -> { System.out.println(k); v.forEach(System.out::println); });
得到的结果形式就是:
- k:是分组的指标,上面代码中的 gender
- v:是一个list的集合对象,就是 personList
原文地址:https://www.cnblogs.com/qingruihappy/p/9921833.html
时间: 2024-12-28 00:56:16