过滤器模式(Filter Pattern)
是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
过滤器模式中几个常见的角色:
- AbstractFilter(抽象过滤器角色):在客户端可以调用它的方法,在抽象过滤器角色中可以知道相关的(一个或者多个)子系统的功能和责任;在正常情况下,它将所有从客户端发来的请求委派到相应的实现类去,传递给相应的实现类对象处理。
- ConcreteFilter(具体滤器角色):在客户端可以调用它的方法,在具体滤器角色会对目标对象集合进行逻辑过滤,最后再返回一个过滤后的集合。
- Subject(被过滤角色):在软件系统中可以有一个或者多个目标角色,在具体过滤器中会对目标角色进行逻辑处理以查看是否是我想要的。
代码实现
Student.java
/** * @desc: Student实体类(被过滤器角色) * @author: YanMingXin * @create: 2021/8/11-17:53 **/ public class Student { private Integer id; private String name; private Integer age; private String degree; private Integer score; private String scoreString; ......(构造方法、Getter方法、Setter方法、toString方法) }
StudentFilter.java
/** * @desc: Filter接口(抽象过滤器角色) * @author: YanMingXin * @create: 2021/8/11-17:56 **/ public interface StudentFilter { /** * 拦截Student实体 * * @param student * @return */ Student doFilter(Student student); /** * 拦截Student集合 * * @param students * @return */ List<Student> doListFilter(List<Student> students); }
ScoreFilter.java
/** * @desc: 分数过滤器类(具体过滤器角色) * @author: YanMingXin * @create: 2021/8/11-17:58 **/ public class ScoreFilter implements StudentFilter { private final String SCORE_A = "A"; private final String SCORE_B = "B"; private final String SCORE_C = "C"; private final int SCORE_A_VALUE = 100; private final int SCORE_B_VALUE = 80; private final int SCORE_C_VALUE = 60; @Override public Student doFilter(Student student) { int score = student.getScore(); if (score >= SCORE_B_VALUE && score <= SCORE_A_VALUE) { student.setScoreString(SCORE_A); } else if (score < SCORE_B_VALUE && score >= SCORE_C_VALUE) { student.setScoreString(SCORE_B); } else if (score < SCORE_C_VALUE) { student.setScoreString(SCORE_C); } return student; } @Override public List<Student> doListFilter(List<Student> students) { ArrayList<Student> list = new ArrayList<>(); for (Student student : students) { int score = student.getScore(); if (score >= SCORE_B_VALUE && score <= SCORE_A_VALUE) { student.setScoreString(SCORE_A); } else if (score < SCORE_B_VALUE && score >= SCORE_C_VALUE) { student.setScoreString(SCORE_B); } else if (score < SCORE_C_VALUE) { student.setScoreString(SCORE_C); } list.add(student); } return list; } }
DegreeFilter.java
/** * @desc: 年级过滤器类(具体过滤器角色) * @author: YanMingXin * @create: 2021/8/11-17:57 **/ public class DegreeFilter implements StudentFilter { private final String HIGH_DEGREE = "高年级"; private final String LOW_DEGREE = "低年级"; private final Integer AVG_AGE = 14; /** * 实现单个学生的过滤方法 * * @param student * @return */ @Override public Student doFilter(Student student) { if (student.getAge() >= AVG_AGE) { student.setDegree(HIGH_DEGREE); } else { student.setDegree(LOW_DEGREE); } return student; } /** * 实现学生集合的过滤方法 * * @param students * @return */ @Override public List<Student> doListFilter(List<Student> students) { ArrayList<Student> list = new ArrayList<>(); for (Student student : students) { if (student.getAge() >= AVG_AGE) { student.setDegree(HIGH_DEGREE); } else { student.setDegree(LOW_DEGREE); } list.add(student); } return list; } }
测试:
/** * @desc: 过滤器模式 * @author: YanMingXin * @create: 2021/8/11-17:51 **/ public class MyFilterTest { public static void main(String[] args) { List<Student> students = Arrays.asList( new Student(1, "zs", 13, null, 78, null), new Student(2, "ls", 18, null, 97, null), new Student(3, "ww", 13, null, 51, null), new Student(4, "zl", 12, null, 45, null), new Student(5, "mq", 10, null, 84, null) ); DegreeFilter degreeFilter = new DegreeFilter(); ScoreFilter scoreFilter = new ScoreFilter(); List<Student> students1 = degreeFilter.doListFilter(students); System.out.println("第一次过滤:"); System.out.println(students1); List<Student> students2 = scoreFilter.doListFilter(students); System.out.println("第二次过滤:"); System.out.println(students2); } }
输出:
第一次过滤: [Student{id=1, name='zs', age=13, degree='低年级', score='78', scoreString='null'}, Student{id=2, name='ls', age=18, degree='高年级', score='97', scoreString='null'}, Student{id=3, name='ww', age=13, degree='低年级', score='51', scoreString='null'}, Student{id=4, name='zl', age=12, degree='低年级', score='45', scoreString='null'}, Student{id=5, name='mq', age=10, degree='低年级', score='84', scoreString='null'}] 第二次过滤: [Student{id=1, name='zs', age=13, degree='低年级', score='78', scoreString='B'}, Student{id=2, name='ls', age=18, degree='高年级', score='97', scoreString='A'}, Student{id=3, name='ww', age=13, degree='低年级', score='51', scoreString='C'}, Student{id=4, name='zl', age=12, degree='低年级', score='45', scoreString='C'}, Student{id=5, name='mq', age=10, degree='低年级', score='84', scoreString='A'}]