赞
踩
允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。最常见的应用如MyBatis中。
// 实体 @Data @AllArgsConstructor public class Person { private String name; private String gender; private String maritalStatus; } // 条件接口 public interface Criteria { public List<Person> filter(List<Person> persons); } // 男性条件 public class MaleCriteria implements Criteria { @Override public List<Person> filter(List<Person> persons) { List<Person> malePersons = new ArrayList<Person>(); for (Person person : persons) { if(person.getGender().equalsIgnoreCase("MALE")){ malePersons.add(person); } } return malePersons; } } // 女性条件 public class FemaleCriteria implements Criteria { @Override public List<Person> filter(List<Person> persons) { List<Person> femalePersons = new ArrayList<Person>(); for (Person person : persons) { if(person.getGender().equalsIgnoreCase("FEMALE")){ femalePersons.add(person); } } return femalePersons; } } // 单身条件 public class SingleCriteria implements Criteria { @Override public List<Person> filter(List<Person> persons) { List<Person> singlePersons = new ArrayList<Person>(); for (Person person : persons) { if(person.getMaritalStatus().equalsIgnoreCase("SINGLE")){ singlePersons.add(person); } } return singlePersons; } } // 与条件 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> filter(List<Person> persons) { List<Person> firstCriteriaPersons = criteria.filter(persons); return otherCriteria.filter(firstCriteriaPersons); } } // 或条件 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> filter(List<Person> persons) { List<Person> firstCriteriaItems = criteria.filter(persons); List<Person> otherCriteriaItems = otherCriteria.filter(persons); for (Person person : otherCriteriaItems) { if(!firstCriteriaItems.contains(person)){ firstCriteriaItems.add(person); } } return firstCriteriaItems; } } // 使用示例 public class Demo { 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 MaleCriteria(); Criteria female = new FemaleCriteria(); Criteria single = new SingleCriteria(); Criteria singleMale = new AndCriteria(single, male); Criteria singleOrFemale = new OrCriteria(single, female); System.out.println("Males: "); printPersons(male.filter(persons)); System.out.println("Females: "); printPersons(female.filter(persons)); System.out.println("Single Males: "); printPersons(singleMale.filter(persons)); System.out.println("Single Or Females: "); printPersons(singleOrFemale.filter(persons)); } public static void printPersons(List<Person> persons){ for (Person person : persons) { System.out.println(person.toString()); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。