1.过滤器模式简介
过滤器模式(Filter Pattern)又称标准模式(Criteria Pattern),用于使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。属于结构型模式,它结合多个标准来获得单一标准。
2.测试Demo
import java.util.List; import java.util.ArrayList; 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 this.name; } public String getGender() { return this.gender; } public String getMaritalStatus() { return this.maritalStatus; } } interface Criteria { public List<Person> meetCriteria(List<Person> persons); } class MaleCtiteria implements Criteria { @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> maleMeet = new ArrayList<Person>(); for (Person person : persons) { if (person.getGender().equalsIgnoreCase("MALE")) { maleMeet.add(person); } } return maleMeet; } } class FemaleCtiteria implements Criteria { @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> femaleMeet = new ArrayList<Person>(); for (Person person : persons) { if (person.getGender().equalsIgnoreCase("FEMALE")) { femaleMeet.add(person); } } return femaleMeet; } } class SingleCtiteria implements Criteria { @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> singleMeet = new ArrayList<Person>(); for (Person person : persons) { if (person.getMaritalStatus().equalsIgnoreCase("Single")) { singleMeet.add(person); } } return singleMeet; } } class AndCtiteria implements Criteria { private Criteria meetCriteria1; private Criteria meetCriteria2; public AndCtiteria(Criteria meetCriteria1, Criteria meetCriteria2) { this.meetCriteria1 = meetCriteria1; this.meetCriteria2 = meetCriteria2; } @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> andMeet = meetCriteria1.meetCriteria(persons); andMeet = meetCriteria2.meetCriteria(andMeet); return andMeet; } } class OrCtiteria implements Criteria { private Criteria selectCriteria1; private Criteria selectCriteria2; public OrCtiteria(Criteria selectCriteria1, Criteria selectCriteria2) { this.selectCriteria1 = selectCriteria1; this.selectCriteria2 = selectCriteria2; } @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> selectMeet1 = selectCriteria1.meetCriteria(persons); List<Person> selectMeet2 = selectCriteria2.meetCriteria(persons); for (Person person : selectMeet1) { if (!selectMeet2.contains(person)) { selectMeet2.add(person); } } return selectMeet2; } } public class MeteriaPatternDemo { public static void main(String args[]) { List<Person> persons = new ArrayList<Person>(); persons.add(new Person("Jane", "MALE", "Single")); persons.add(new Person("July", "FEMALE", "Married")); persons.add(new Person("Kitty", "MALE", "Married")); persons.add(new Person("James", "FEMALE", "Single")); Criteria maleCriteria = new MaleCtiteria(); Criteria singleCriteria = new SingleCtiteria(); Criteria andCriteria = new AndCtiteria(maleCriteria, singleCriteria); Criteria orCriteria = new OrCtiteria(maleCriteria, singleCriteria); System.out.println("Male:"); PrintPerson(maleCriteria.meetCriteria(persons)); System.out.println("Single:"); PrintPerson(singleCriteria.meetCriteria(persons)); System.out.println("Male and Single:"); PrintPerson(andCriteria.meetCriteria(persons)); System.out.println("Male or Single:"); PrintPerson(orCriteria.meetCriteria(persons)); } public static void PrintPerson(List<Person> persons) { for (Person person : persons) { System.out.println("name=" + person.getName() + " " + "gender=" + person.getGender() + " " + "maritalStatus=" + person.getMaritalStatus()); } } }
参考:http://www.runoob.com/design-pattern/filter-pattern.html