一、过滤器模式简介
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
二、示例演示
1、业务需求
假设Person类有三个属性:姓名(Name)、性别(Gender)和婚姻情况(Marital),我们的系统中的一些功能需要对这些属性进行过滤筛选,比如:
1)得到所有的男性;
2)得到所有的女性;
3)得到所有还单身的人;
4)得到所有已婚的人。
系统还希望能够将这些条件组合起来进行过滤筛选,比如:
1)得到所有已婚男性;
2)得到所有单身女性;
3)得到所有已婚的人或女性;
2、定义Person类
public class Person {
private String name; // 姓名
private String gender; // 性别
private String marital; // 婚姻情况
public Person(String name, String gender, String marital) {
this.name = name;
this.gender = gender;
this.marital = marital;
}
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 getMarital() {
return marital;
}
public void setMarital(String marital) {
this.marital = marital;
}
@Override
public String toString() {
return "Person [name=" + name + ",gender=" + gender + ", marital=" + marital + "]";
}
}
3、定义过滤器接口以及具体的实现
过滤器接口:
public interface Filter {
// 根据传过来的Person列表,根据一定的条件过滤,得到目标集合
List<Person> filter(List<Person> persons);
}
查找所有男性的实现类:
public class MaleFilter implements Filter {
@Override
public List<Person> filter(List<Person> persons) {
List<Person> result = new ArrayList<>();
for (Person person : persons) {
if ("MALE".equalsIgnoreCase(person.getGender())) {
result.add(person);
}
}
return result;
}
}
查找所有女性的实现类FemaleFilter
查找所有单身人士的实现类SingleFilter
查找所有已婚人士的实现类MarriedFilter
上述三个类的实现原理与MaleFilter实现原理相同,这里不做表述。
处理“and”逻辑的过滤器类FilterAnd类中的代码:
public class FilterAnd implements Filter {
private Filter filter;
private Filter otherFilter;
public FilterAnd(Filter filter, Filter otherFilter) {
this.filter = filter;
this.otherFilter = otherFilter;
}
@Override
public List<Person> filter(List<Person> persons) {
List<Person> tmpList = filter.filter(persons);
return otherFilter.filter(tmpList); //经过两层过滤
}
}
处理“or”逻辑的过滤器类FilterOr类中的代码:
public class FilterOr implements Filter {
private Filter filter;
private Filter otherFilter;
public FilterOr(Filter filter, Filter otherFilter) {
this.filter = filter;
this.otherFilter = otherFilter;
}
@Override
public List<Person> filter(List<Person> persons) {
List<Person> tmpList1 = filter.filter(persons);
List<Person> tmpList2 = otherFilter.filter(persons);
for (Person person : tmpList2) {
if (!tmpList1.contains(person)) { //将tmpList1和tmpList2合并,即完成or运算
tmpList1.add(person);
}
}
return tmpList1;
}
}
4、编写测试代码:
public class Test {
public static void main(String[] args) {
// 初始化数据
List<Person> persons = new ArrayList<>();
persons.add(new Person("霍一", "FEMALE", "MARRIED"));
persons.add(new Person("邓二", "MALE", "MARRIED"));
persons.add(new Person("张三", "MALE", "SINGLE"));
persons.add(new Person("李四", "FEMALE", "MARRIED"));
persons.add(new Person("王五", "MALE", "SINGLE"));
persons.add(new Person("赵六", "FEMALE", "SINGLE"));
persons.add(new Person("孙七", "MALE", "SINGLE"));
persons.add(new Person("罗八", "MALE", "MARRIED"));
persons.add(new Person("刘九", "FEMALE", "SINGLE"));
persons.add(new Person("史十", "FEMALE", "SINGLE"));
// 打印出所有男性的信息
System.out.println("---------------------所有男性---------------------");
List<Person> maleList = new MaleFilter().filter(persons);
printList(maleList);
// 打印出所有单身的信息
System.out.println("---------------------所有单身---------------------");
List<Person> singleList = new SingleFilter().filter(persons);
printList(singleList);
// 打印出所有已婚女性的信息
System.out.println("--------------------所有已婚女性-------------------");
List<Person> marriedFemaleList = new FilterAnd(new MarriedFilter(), new FemaleFilter()).filter(persons);
printList(marriedFemaleList);
// 打印出所有单身或女性的信息
System.out.println("-------------------所有单身或女性------------------");
List<Person> singleOrFemaleList = new FilterOr(new SingleFilter(), new FemaleFilter()).filter(persons);
printList(singleOrFemaleList);
}
// 打印列表中的数据信息
private static void printList(List<Person> list) {
for (Person person : list) {
System.out.println(person.toString());
}
}
}