Java学习-设计模式-过滤器模式
概述:
过滤器模式使用不同的条件过滤对象,这些标准可以通过逻辑操作链接在一起,过滤器模式是一种结构型模式。
这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来,它结合多个标准来获得单一标准。
理解:
这个是不是理解很简单啊,我感觉看到示例好的一下就理解了,示例要是只有纯代码的,贼懵逼。
简而言之:就拿高中举例,高中有男生和女生,还分大一大二大三。现假设数据高中的所有的学生的数据都是在一起的,我们要筛选出高二的女生,怎么办?说起来简单,把数据拿过来判断,看看是不是大二的女生就好了,那如果先在还需要增加需求,要判断每个年级各种性别怎么玩?定义六个类来判断?如果此时再加上学生年龄怎么解决?代码就很乱了。
按过滤器模式来说:
1. 首先我们应该有一个学生的类来记录学生的信息,
2. 然后定义一个过滤器的接口A1,里面包含了一个过滤的方法。
3. 创建对应的单条件的过滤器类去实现过滤器接口A1,比如选择高二的学生的过滤类,选择女生的过滤类
4. 如果有需要(这里测试肯定是有需要的),还可以根据需要创建多个过滤类的组合类
5. 调用过滤类来过滤出想要的数据
举例
package Practices.Mode.Filter_Pattern;
// 1. 先定义一个学生类记录学生信息
public class Student {
public String name;
public String sex;
public String grade;
public Student(String name, String sex, String grade) {
this.name = name;
this.sex = sex;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex=" + sex +
", grade='" + grade + '\'' +
'}';
}
}
package Practices.Mode.Filter_Pattern;
import java.util.List;
// 2. 定义一个过滤器的接口,包含一个过滤的方法,所有过滤器都实现这个接口,
public interface Filter {
public List<Student> filter(List<Student> students);
}
package Practices.Mode.Filter_Pattern;
import java.util.ArrayList;
import java.util.List;
// 3. 实现过滤器接口,这里获取的是高二年级
public class Grade_Filter implements Filter {
public List<Student> filter(List<Student> students) {
ArrayList<Student> result = new ArrayList<Student>();
for(Student student:students){
if(student.grade.equals("高二")){
result.add(student);
}
}
return result;
}
}
package Practices.Mode.Filter_Pattern;
import java.util.ArrayList;
import java.util.List;
// 3. 实现过滤器接口,这里获取的是女生
public class Sex_Filter implements Filter {
public List<Student> filter(List<Student> students) {
ArrayList<Student> result = new ArrayList<Student>();
for(Student student:students){
if(student.sex.equals("女")){
result.add(student);
}
}
return result;
}
}
package Practices.Mode.Filter_Pattern;
import java.util.List;
// 4. 这里创建一个组合过滤类,用来实现选定两个筛选条件 and 过滤
public class And_Filter {
private Filter filter1;
private Filter filter2;
public And_Filter(Filter filter1, Filter filter2) {
this.filter1 = filter1;
this.filter2 = filter2;
}
// 入参时,第一个过滤选高二年级,第二个过滤选女生,实现选择高二女生的过滤
public List<Student> and_filter(List<Student> students){
List<Student> tmp = filter1.filter(students);
List<Student> result = filter2.filter(tmp);
return result;
}
}
package Practices.Mode.Filter_Pattern;
import java.util.ArrayList;
import java.util.List;
// 5. 测试
public class test_main {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
students.add(new Student("小红","女","高二"));
And_Filter andFilter = new And_Filter(new Grade_Filter(), new Sex_Filter());
List<Student> studentList = andFilter.and_filter(students);
for (Student s: studentList) {
System.out.println(s.toString());
}
}
}