过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
- 创建一个类,在该类上应用标准。
/**
* 1. 创建一个类,在该类上应用标准。
* @author mazaiting
*/
public 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 name;
}
public String getGender() {
return gender;
}
public String getMaritalStatus() {
return maritalStatus;
}
}
- 为标准(Criteria)创建一个接口。
/**
* 2. 为标准(Criteria)创建一个接口
* @author mazaiting
*/
public interface Critertia {
/**男性*/
public static final String MALE = "MALE";
/**女性*/
public static final String FEMALE = "FEMALE";
/**单身*/
public static final String SINGLE = "SINGLE";
/**
* 获取指定的人群
* @param persons
*/
public List<Person> meetCriteria(List<Person> persons);
}
- 创建实现了 Criteria 接口的实体类。
/**
* 3. 创建实现了 Criteria 接口的实体类--过滤男性。
* @author mazaiting
*/
public class CriteriaMale implements Critertia{
public List<Person> meetCriteria(List<Person> persons) {
List<Person> malePersons = new ArrayList<Person>();
for (Person person : persons) {
if (person.getGender().equalsIgnoreCase(MALE)){
malePersons.add(person);
}
}
return malePersons;
}
}
/**
* 3. 创建实现了 Criteria 接口的实体类。--过滤女性
* @author mazaiting
*/
public class CriteriaFemale implements Critertia{
public List<Person> meetCriteria(List<Person> persons) {
List<Person> femalePersons = new ArrayList<Person>();
for (Person person : persons) {
if (person.getGender().equalsIgnoreCase(FEMALE)){
femalePersons.add(person);
}
}
return femalePersons;
}
}
/**
* 3. 创建实现了 Criteria 接口的实体类。--过滤单身
* @author mazaiting
*/
public class CriteriaSingle implements Critertia{
public List<Person> meetCriteria(List<Person> persons) {
List<Person> singlePersons = new ArrayList<Person>();
for (Person person : persons) {
if (person.getMaritalStatus().equalsIgnoreCase(SINGLE)){
singlePersons.add(person);
}
}
return singlePersons;
}
}
/**
* 3. 创建实现了 Criteria 接口的实体类。--双重过滤
* @author mazaiting
*/
public class AndCriteria implements Critertia{
private Critertia critertia;
private Critertia otherCritertia;
public AndCriteria(Critertia critertia, Critertia otherCriteria) {
this.critertia = critertia;
this.otherCritertia = otherCriteria;
}
public List<Person> meetCriteria(List<Person> persons) {
List<Person> firstCriteriaPersons = critertia.meetCriteria(persons);
return otherCritertia.meetCriteria(firstCriteriaPersons);
}
}
/**
* 3. 创建实现了 Criteria 接口的实体类。--或过滤
* @author mazaiting
*/
public class OrCriteria implements Critertia{
private Critertia critertia;
private Critertia otherCritertia;
public OrCriteria(Critertia critertia, Critertia otherCritertia) {
this.critertia = critertia;
this.otherCritertia = otherCritertia;
}
public List<Person> meetCriteria(List<Person> persons) {
List<Person> firstCriteriaItems = critertia.meetCriteria(persons);
List<Person> otherCriteriaItems = otherCritertia.meetCriteria(persons);
for (Person person : otherCriteriaItems) {
if (!firstCriteriaItems.contains(person)) {
firstCriteriaItems.add(person);
}
}
return firstCriteriaItems;
}
}
- 主函数验证
public class Client {
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"));
Critertia male = new CriteriaMale();
Critertia single = new CriteriaSingle();
Critertia critertia = new AndCriteria(male, single);
List<Person> meetCriteria = critertia.meetCriteria(persons);
printPersons(meetCriteria);
}
/**
* 打印数据
* @param persons
*/
public static void printPersons(List<Person> persons){
for (Person person : persons) {
System.out.println("Person : [ Name : " + person.getName()
+", Gender : " + person.getGender()
+", Marital Status : " + person.getMaritalStatus()
+" ]");
}
}
}
- 打印结果
Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]