类别:
Structual Pattern
问题:
方案:
示例:
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class CriteriaPatternDemo { public static void main(String[] args) { List<Person> persons = new ArrayList<Person>(); persons.add(new Person("小强", "男", "未婚")); persons.add(new Person("小明", "男", "未婚")); persons.add(new Person("小李", "男", "已婚")); persons.add(new Person("小红", "女", "已婚")); persons.add(new Person("小月", "女", "已婚")); persons.add(new Person("小兰", "女", "未婚")); Criteria male = new Male(); Criteria female = new Female(); Criteria single = new Single(); Criteria singleMale = new And(single, male); Criteria singleOrFemale = new Or(single, female); System.out.println("男: "); printPersons(male.filter(persons)); System.out.println("\n女: "); printPersons(female.filter(persons)); System.out.println("\n未婚男: "); printPersons(singleMale.filter(persons)); System.out.println("\n未婚或者女: "); printPersons(singleOrFemale.filter(persons)); } public static void printPersons(List<Person> persons) { for (Person person : persons) { System.out.println(person); } } } class Person { private String name; private String sex; private String marriage; public Person(String name, String sex, String marriage) { this.name = name; this.sex = sex; this.marriage = marriage; } public String getName() { return name; } public String getSex() { return sex; } public String getMarriage() { return marriage; } @Override public String toString() { return "Person [name=" + name + ", sex=" + sex + ", marriage=" + marriage + "]"; } } interface Criteria { public List<Person> filter(List<Person> list); } class Male implements Criteria { @Override public List<Person> filter(List<Person> list) { List<Person> malePersonList = new ArrayList<Person>(); for (Person person : list) { if (person.getSex().equalsIgnoreCase("男")) { malePersonList.add(person); } } return malePersonList; } } class Female implements Criteria { @Override public List<Person> filter(List<Person> list) { List<Person> femalePersonList = new ArrayList<Person>(); for (Person person : list) { if (person.getSex().equalsIgnoreCase("女")) { femalePersonList.add(person); } } return femalePersonList; } } class Single implements Criteria { @Override public List<Person> filter(List<Person> list) { List<Person> singlePersonList = new ArrayList<Person>(); for (Person person : list) { if (person.getMarriage().equalsIgnoreCase("未婚")) { singlePersonList.add(person); } } return singlePersonList; } } class And implements Criteria { private Criteria a; private Criteria b; public And(Criteria a, Criteria b) { this.a = a; this.b = b; } @Override public List<Person> filter(List<Person> list) { List<Person> aList = a.filter(list); return b.filter(aList); } } class Or implements Criteria { private Criteria a; private Criteria b; public Or(Criteria a, Criteria b) { this.a = a; this.b = b; } @Override public List<Person> filter(List<Person> list) { List<Person> aList = a.filter(list); List<Person> bList = b.filter(list); Set<Person> set = new HashSet<Person>(); set.addAll(aList); set.addAll(bList); return new ArrayList<Person>(set); } }
男: Person [name=小强, sex=男, marriage=未婚] Person [name=小明, sex=男, marriage=未婚] Person [name=小李, sex=男, marriage=已婚] 女: Person [name=小红, sex=女, marriage=已婚] Person [name=小月, sex=女, marriage=已婚] Person [name=小兰, sex=女, marriage=未婚] 未婚男: Person [name=小强, sex=男, marriage=未婚] Person [name=小明, sex=男, marriage=未婚] 未婚或者女: Person [name=小兰, sex=女, marriage=未婚] Person [name=小月, sex=女, marriage=已婚] Person [name=小强, sex=男, marriage=未婚] Person [name=小明, sex=男, marriage=未婚] Person [name=小红, sex=女, marriage=已婚]
应用:
不足:(
优化:)
标签:name,Pattern,List,sex,Filter,Person,marriage,Criteria From: https://www.cnblogs.com/zno2/p/6694826.html