简介
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
优点:简单,解耦,使用方便。
使用场景:需要进行筛选时。
角色
过滤器模式包含如下角色:
(1) AbstractFilter(抽象过滤器角色):定义过滤的方法。
(2) ConcreteFilter(具体滤器角色):实现过滤的方法,对目标对象集合进行逻辑过滤,最后再返回一个过滤后的集合。
(3) Subject(被过滤角色):目标角色,源数据,具体过滤器会对其进行逻辑处理。
简单示例
1.创建Subject(被过滤角色)
public class Student {
private String name;
private String gender;
private String grade;
//getter setter .....
}
2.创建 AbstractFilter(抽象过滤器角色)
public interface Filter {
public List<Student> doFilt(List<Student> students);
}
3.创建ConcreteFilter(具体滤器角色)
public class Boy implements Filter {
@Override
public List<Student> doFilt(List<Student> students) {
List<Student> students1 = new ArrayList<>();
for (Student student: students) {
if (student.getGender().equals("boy")){
students1.add(student);
}
}
return students1;
}
}
public class Girl implements Filter{
@Override
public List<Student> doFilt(List<Student> students) {
List<Student> students1 = new ArrayList<>();
for (Student student: students) {
if (student.getGender().equals("girl")){
students1.add(student);
}
}
return students1;
}
}
public class GradeOne implements Filter {
@Override
public List<Student> doFilt(List<Student> students) {
List<Student> students1 = new ArrayList<>();
for (Student student: students) {
if (student.getGrade().equals("one")){
students1.add(student);
}
}
return students1;
}
}
public class GradeTwo implements Filter {
@Override
public List<Student> doFilt(List<Student> students) {
List<Student> students1 = new ArrayList<>();
for (Student student: students) {
if (student.getGrade().equals("two")){
students1.add(student);
}
}
return students1;
}
}
public class And implements Filter{
private Filter filter1;
private Filter filter2;
public And(Filter filter1, Filter filter2) {
this.filter1 = filter1;
this.filter2 = filter2;
}
@Override
public List<Student> doFilt(List<Student> students) {
List<Student> students2 = filter1.doFilt(students);
return filter2.doFilt(students2);
}
}
public class Or implements Filter {
private Filter filter1;
private Filter filter2;
public Or(Filter filter1,Filter filter2) {
this.filter1 = filter1;
this.filter2 = filter2;
}
@Override
public List<Student> doFilt(List<Student> students) {
List<Student> students1 = filter1.doFilt(students);
List<Student> students2 = filter2.doFilt(students);
for(Student student : students2) {
if(!students1.contains(student)) {
students1.add(student);
}
}
return students1;
}
}
4.Main验证
public class FilterMain extends Boy {
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student("hello1","boy","one"));
students.add(new Student("hello2","girl","two"));
students.add(new Student("hello3","boy","two"));
students.add(new Student("hello4","girl","one"));
students.add(new Student("hello5","girl","one"));
students.add(new Student("hello6","boy","two"));
Filter boy = new Boy();
System.out.println("boy list is : " + boy.doFilt(students));
Filter girl = new Girl();
System.out.println("girl list is : " + girl.doFilt(students));
Filter one = new GradeOne();
System.out.println("GradeOne list is : " + one.doFilt(students));
Filter two = new GradeTwo();
System.out.println("GradeTwo list is : " + two.doFilt(students));
Filter oneBoy = new And(one,boy);
System.out.println("GradeOne Boy list is : " + oneBoy.doFilt(students));
Filter twoGirl = new And(two,girl);
System.out.println("GradeTwo Girl list is : " + twoGirl.doFilt(students));
Filter grilOrOne = new Or(one,girl);
System.out.println("GradeOne or girl list is :" + grilOrOne.doFilt(students));
}
}
boy list is : [Student{name='hello1', gender='boy', grade='one'}, Student{name='hello3', gender='boy', grade='two'}, Student{name='hello6', gender='boy', grade='two'}]
girl list is : [Student{name='hello2', gender='girl', grade='two'}, Student{name='hello4', gender='girl', grade='one'}, Student{name='hello5', gender='girl', grade='one'}]
GradeOne list is : [Student{name='hello1', gender='boy', grade='one'}, Student{name='hello4', gender='girl', grade='one'}, Student{name='hello5', gender='girl', grade='one'}]
GradeTwo list is : [Student{name='hello2', gender='girl', grade='two'}, Student{name='hello3', gender='boy', grade='two'}, Student{name='hello6', gender='boy', grade='two'}]
GradeOne Boy list is : [Student{name='hello1', gender='boy', grade='one'}]
GradeTwo Girl list is : [Student{name='hello2', gender='girl', grade='two'}]
GradeOne or girl list is :[Student{name='hello1', gender='boy', grade='one'}, Student{name='hello4', gender='girl', grade='one'}, Student{name='hello5', gender='girl', grade='one'}, Student{name='hello2', gender='girl', grade='two'}]