设计模式-----过滤器模式
通过菜鸟教程学习的。例子也是来源于菜鸟教程的实例
举个例子:
现在在你面前有一堆人,你需要做的是就是告诉我下面几种人分别都有谁
一、筛选标准
第一种:男人
第二种:女人
第三种:单身男人
第四种:所有的女人和所有单身的
你会用什么样的方式来实现呢?
这个过滤器模式真的就是人如其名,就是过滤,我就把它理解成筛子,
二、我们的准备工作;
1、一个人(Person)
package com.design.filter.model; public class Person { private String name;//姓名 private String gender;//性别 private String maritalStatus;//婚姻状态 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 getMaritalStatus() { return maritalStatus; } public void setMaritalStatus(String maritalStatus) { this.maritalStatus = maritalStatus; } public Person(String name, String gender, String maritalStatus) { super(); this.name = name; this.gender = gender; this.maritalStatus = maritalStatus; } public Person() { super(); } }
2、筛子(Criteria)
当然了,筛子我们总不可能只有一把是吧,一把怎么筛,筛出来都长一样的,所有,我们准备的筛子得满足我们的需求呀
筛子你想怎么定义都是可以的,可以实现我们的目的就可以了
第一种:你可以选择每一个标准定义一个标准,那么定义四个标准
第二种:先定义三个基本的筛子,通过组合的方式,组装成新的筛子。总共会有五把筛子
我选择的是第二种
定义筛子接口
package com.design.filter.inter; import java.util.List; import com.design.filter.model.Person; public interface Criteria { public List<Person> meetCriteria(List<Person> persons); }
1、删选男人的筛子:CriteriaMale
1 package com.design.filter.inter.impl; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.design.filter.inter.Criteria; 7 import com.design.filter.model.Person; 8 9 /** 10 * 筛选男人的筛子 11 */ 12 public class CriteriaMale implements Criteria { 13 @Override 14 public List<Person> meetCriteria(List<Person> persons) { 15 List<Person> malePersons = new ArrayList<Person>(); 16 for (Person person : persons) { 17 if(person.getGender().equalsIgnoreCase("MALE")){ 18 malePersons.add(person); 19 } 20 } 21 return malePersons; 22 } 23 24 }
2、筛选女人的筛子:CriterFemale
1 package com.design.filter.inter.impl; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.design.filter.inter.Criteria; 7 import com.design.filter.model.Person; 8 9 /** 10 * 筛选女人的筛子 11 */ 12 public class CriteriaFemale implements Criteria { 13 @Override 14 public List<Person> meetCriteria(List<Person> persons) { 15 List<Person> femalePersons = new ArrayList<Person>(); 16 for (Person person : persons) { 17 if(person.getGender().equalsIgnoreCase("FEMALE")){ 18 femalePersons.add(person); 19 } 20 } 21 return femalePersons; 22 } 23 24 }
3、筛选单身的筛子:CriterSingle
1 package com.design.filter.inter.impl; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.design.filter.inter.Criteria; 7 import com.design.filter.model.Person; 8 9 /** 10 * 筛选单身的筛子 11 */ 12 public class CriteriaSingle implements Criteria { 13 @Override 14 public List<Person> meetCriteria(List<Person> persons) { 15 List<Person> singlePersons = new ArrayList<Person>(); 16 for (Person person : persons) { 17 if(person.getMaritalStatus().equalsIgnoreCase("SINGLE")){ 18 singlePersons.add(person); 19 } 20 } 21 return singlePersons; 22 } 23 }
4、组合单身和男人的筛子得到交集:AndCriteria
1 package com.design.filter.inter.impl; 2 3 import java.util.List; 4 5 import com.design.filter.inter.Criteria; 6 import com.design.filter.model.Person; 7 8 public class AndCriteria implements Criteria { 9 10 private Criteria criteria; 11 private Criteria otherCriteria; 12 13 public AndCriteria(Criteria criteria, Criteria otherCriteria) { 14 this.criteria = criteria; 15 this.otherCriteria = otherCriteria; 16 } 17 18 /** 19 * 第一种标准和第二种标准都满足的 20 */ 21 @Override 22 public List<Person> meetCriteria(List<Person> persons) { 23 List<Person> firstCriteriaPersons = criteria.meetCriteria(persons);// 第一种方式筛选过再使用另一种方法筛选 24 return otherCriteria.meetCriteria(firstCriteriaPersons); 25 } 26 27 }
5、组合女人和单身的筛子得到并集:OrCriteria
1 package com.design.filter.inter.impl; 2 3 import java.util.List; 4 5 import com.design.filter.inter.Criteria; 6 import com.design.filter.model.Person; 7 8 public class OrCriteria implements Criteria { 9 10 private Criteria criteria; 11 private Criteria otherCriteria; 12 13 public OrCriteria(Criteria criteria, Criteria otherCriteria) { 14 this.criteria = criteria; 15 this.otherCriteria = otherCriteria; 16 } 17 18 /** 19 * 第一种标准或者第二种标准中满足一个的 20 */ 21 @Override 22 public List<Person> meetCriteria(List<Person> persons) { 23 List<Person> firstCriteriaItems = criteria.meetCriteria(persons);//第一种标准筛选 24 List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persons);//第二种标准筛选 25 26 for (Person person : otherCriteriaItems) {//遍历第二种方式筛选的信息 27 if (!firstCriteriaItems.contains(person)) {//如果第一种标准筛选过的人中不存在的,则加入到 28 firstCriteriaItems.add(person); 29 } 30 } 31 return firstCriteriaItems; 32 } 33 }
三、筛选(CriteriaPatternDemo)
1 package com.design.filter; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.design.filter.inter.Criteria; 7 import com.design.filter.inter.impl.AndCriteria; 8 import com.design.filter.inter.impl.CriteriaFemale; 9 import com.design.filter.inter.impl.CriteriaMale; 10 import com.design.filter.inter.impl.CriteriaSingle; 11 import com.design.filter.inter.impl.OrCriteria; 12 import com.design.filter.model.Person; 13 14 public class CriteriaPatternDemo { 15 public static void main(String[] args) { 16 List<Person> persons = new ArrayList<Person>(); 17 18 //一群人 19 persons.add(new Person("Robert", "Male", "Single")); 20 persons.add(new Person("John", "Male", "Married")); 21 persons.add(new Person("Laura", "Female", "Married")); 22 persons.add(new Person("Diana", "Female", "Single")); 23 persons.add(new Person("Mike", "Male", "Single")); 24 persons.add(new Person("Bobby", "Male", "Single")); 25 26 27 Criteria male = new CriteriaMale();//筛选男人 28 Criteria female = new CriteriaFemale();//筛选女人 29 Criteria single = new CriteriaSingle();//筛选单身的 30 Criteria singleMale = new AndCriteria(single, male);//筛选单身的男人 31 Criteria singleOrFemale = new OrCriteria(single, female);//筛选所有的单身的,所有的女人 32 33 System.out.println("Males: "); 34 printPersons(male.meetCriteria(persons)); 35 36 System.out.println("\nFemales: "); 37 printPersons(female.meetCriteria(persons)); 38 39 System.out.println("\nSingle Males: "); 40 printPersons(singleMale.meetCriteria(persons)); 41 42 System.out.println("\nSingle Or Females: "); 43 printPersons(singleOrFemale.meetCriteria(persons)); 44 } 45 46 public static void printPersons(List<Person> persons) { 47 for (Person person : persons) { 48 System.out.println("Person : [ Name : " + person.getName() 49 + ", Gender : " + person.getGender() 50 + ", Marital Status : " + person.getMaritalStatus() + " ]"); 51 } 52 } 53 }