过滤器模式 -- 设计模式
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式
思考: 其实是将组合型的过滤器和具体实现类型的过滤器进行抽象, 将不同类型的过滤器抽象出一个方法, 然后使用组合型的过滤器来组合, 最后达到应用多个过滤器过滤数据源的作用
package day0318.FilterPattern; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; 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")); CriteriaSingle criteriaSingle = new CriteriaSingle(); CriterialFemale criterialFemale = new CriterialFemale(); CriterialMale criterialMale = new CriterialMale(); List<Person> singleAndFemalePersons = new AndCriterial(criteriaSingle, criterialFemale).meetCriteria(persons); System.out.println("single female: "); System.out.println(singleAndFemalePersons); System.out.println("single male: "); System.out.println(new AndCriterial(criterialMale, criteriaSingle).meetCriteria(persons)); System.out.println("female and male"); System.out.println(new AndCriterial(criterialFemale, criterialMale).meetCriteria(persons)); } } // 约束接口 interface Criteria { List<Person> meetCriteria(List<Person> persons); } class AndCriterial implements Criteria { Criteria criteria; Criteria otherCriteria; public AndCriterial(Criteria criteria, Criteria otherCriteria){ this.criteria = criteria; this.otherCriteria = otherCriteria; } @Override public List<Person> meetCriteria(List<Person> persons){ return this.criteria.meetCriteria(this.otherCriteria.meetCriteria(persons)); } } class OrCriterial implements Criteria { Criteria criteria; Criteria otherCriteria; public OrCriterial(Criteria criteria, Criteria otherCriteria){ this.criteria = criteria; this.otherCriteria = otherCriteria; } @Override public List<Person> meetCriteria(List<Person> persons){ List<Person> people = this.criteria.meetCriteria(persons); people.addAll(this.otherCriteria.meetCriteria(persons)); return people; } } class CriterialFemale implements Criteria { @Override public List<Person> meetCriteria(List<Person> persons){ return persons.stream() .filter((p) -> p.getGender() .equalsIgnoreCase("FEMALE")).collect(Collectors.toList()); } } class CriterialMale implements Criteria { @Override public List<Person> meetCriteria(List<Person> persons){ List<Person> ret = new ArrayList<>(); persons.stream() .filter((p) -> p.getGender() .equalsIgnoreCase("MALE")) .forEach(p -> ret.add(p)); return ret; } } class CriteriaSingle implements Criteria { @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> singlePersons = new ArrayList<Person>(); for (Person person : persons) { if(person.getMaterialStatus().equalsIgnoreCase("SINGLE")){ singlePersons.add(person); } } return singlePersons; } } class Person { String name; String gender; String materialStatus; public Person(String name, String gender, String materialStatus){ this.name = name; this.gender = gender; this.materialStatus = materialStatus; } @Override public String toString(){ return "Person{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", materialStatus='" + materialStatus + '\'' + '}'; } 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 getMaterialStatus(){ return materialStatus; } public void setMaterialStatus(String materialStatus){ this.materialStatus = materialStatus; } }