过滤器模式
过滤器模式可以运行通过某些单个标准或组合标准过滤出符合需求的对象。
定义过滤接口
import java.util.List; public interface Criteria { public List<Person> meetCriteria(List<Person> persons);//过滤出符合某种标准的结果集 }
筛选出性别为男的Person
import java.util.ArrayList; import java.util.List; public class ManCriteria implements Criteria{ @Override public List<Person> meetCriteria(List<Person> persons) { // TODO Auto-generated method stub List<Person> manList = new ArrayList<Person>(); for(Person person:persons) { if("男".equals(person.getSex())) { manList.add(person); } } return manList; } }
筛选出性别为女的Person对象
import java.util.ArrayList; import java.util.List; public class WomanCriteria implements Criteria{ @Override public List<Person> meetCriteria(List<Person> persons) { // TODO Auto-generated method stub List<Person> womanList = new ArrayList<Person>(); for(Person person:persons) { if("女".equals(person.getSex())) { womanList.add(person); } } return womanList; } }
筛选出已婚的Person对象
import java.util.ArrayList; import java.util.List; public class MaritalCriteria implements Criteria{ @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> maritalList = new ArrayList<Person>(); for(Person person:persons) { if("Married".equals(person.getMaritalStatus())){ maritalList.add(person); } } return maritalList; } }
AndCirter, 代表过滤器的与逻辑。AndCirter(A,B) ,筛选出满足A且满足B的Person。
import java.util.List; public class AndCriteria implements Criteria{ private Criteria criteria; private Criteria othercriteria; public AndCriteria(Criteria criteria, Criteria othercriteria) { super(); this.criteria = criteria; this.othercriteria = othercriteria; } @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> firstCriteria = criteria.meetCriteria(persons); return othercriteria.meetCriteria(firstCriteria); } }
OrCirter, 代表过滤器的或逻辑。OrCirter(A,B) ,筛选出满足A或满足B的Person。
import java.util.List; public class OrCriteria implements Criteria{ private Criteria criteria; private Criteria otherCriteria; public OrCriteria(Criteria criteria, Criteria otherCriteria) { super(); this.criteria = criteria; this.otherCriteria = otherCriteria; } @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> firstCriteriaItem = criteria.meetCriteria(persons); List<Person> otherCriteriaItem = otherCriteria.meetCriteria(persons); for(Person person:firstCriteriaItem) { if(!otherCriteriaItem.contains(person)) {//去掉两个结果集中重复部分,即将不包含的内容添加。 otherCriteriaItem.add(person); } } return otherCriteriaItem; } }
测试
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Person> persons = new ArrayList<Person>(); persons.add(new Person("Robert","男", "Single")); persons.add(new Person("John","女", "Married")); persons.add(new Person("Laura","女", "Married")); persons.add(new Person("Diana","女", "Single")); persons.add(new Person("Mike","男", "Single")); persons.add(new Person("Bobby","男", "Single")); Criteria manCriteria = new ManCriteria(); out(manCriteria.meetCriteria(persons));//筛选出性别为男的对象 Criteria womanCriteria = new WomanCriteria(); out(womanCriteria.meetCriteria(persons));//筛选出性别为女的对象 Criteria maritalCriteria = new MaritalCriteria(); out(maritalCriteria.meetCriteria(persons));//筛选出已婚对象 Criteria manAndMarital = new AndCriteria(manCriteria, maritalCriteria);//筛选出男性且已婚的对象 out(manAndMarital.meetCriteria(persons)); } public static void out(List<Person> persons) { for(Person person:persons) { System.out.println(person); } System.out.println(); } }
参考资料: