背景
今年的高考推迟了,每年的高考完后有喜有悲,当然每个人考得的分数不同,所以结果就不用,而一般高校都是有严格的分数界限的,比如211和985、一本、二本、三本、还有普通专科...
过滤器模式是什么?
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
模式可以干嘛?
其实现模式也就是用来做过滤和筛选用的,比如你考700分那肯定是一本或重点大学,当然你也可以选择读不读或选个sb的普通专科;反过来高校也是一样的,你投过来的志愿,正常情况下如果低于分数线直接不录取。
优点:
简单、方便、解耦;
缺点:
目前没有发现...
过滤器模式类图
实现代码
/**
* @Auther: csh
* @Date: 2020/5/25 14:10
* @Description:学生
*/
public class Student implements Serializable {
/** 人名*/
private String realName;
/** 分数*/
private int grade;
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public Student(String realName, int grade) {
this.realName = realName;
this.grade = grade;
}
@Override
public String toString() {
return "Student{" +
"realName='" + realName + '\'' +
", grade=" + grade +
'}';
}
}
/**
* @Auther: csh
* @Date: 2020/5/25 14:16
* @Description:分数拦截器
*/
public interface IGradeFilter {
/** 分数拦截器*/
List<Student> conditionGradeFilter(List<Student> studentList);
}
/**
* @Auther: csh
* @Date: 2020/5/25 14:26
* @Description:分数枚举
*/
public enum GradeEnum {
ONE_COLLEGE(700,"1本"),
TWO_COLLEGE(600,"2本"),
THREE_COLLEGE(400,"3本"),
COMMON_COLLEGE(200,"普通专科")
;
private int grade;
private String name;
GradeEnum(int grade, String name) {
this.grade = grade;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
/**
* @Auther: csh
* @Date: 2020/5/25 14:30
* @Description:最好的大学如 清华、北大等
*/
public class BestUniversityFilter implements IGradeFilter{
@Override
public List<Student> conditionGradeFilter(List <Student> studentList) {
List<Student> list = new ArrayList <Student>();
for (Student student : studentList) {
if(student.getGrade()>=GradeEnum.ONE_COLLEGE.getGrade()){
list.add(student);
}
}
return list;
}
}
/**
* @Auther: csh
* @Date: 2020/5/25 14:30
* @Description:二本大学
*/
public class SecondUniversityFilter implements IGradeFilter{
@Override
public List<Student> conditionGradeFilter(List <Student> studentList) {
List<Student> list = new ArrayList <Student>();
for (Student student : studentList) {
if(student.getGrade()>=GradeEnum.TWO_COLLEGE.getGrade() && student.getGrade()<GradeEnum.ONE_COLLEGE.getGrade()){
list.add(student);
}
}
return list;
}
}
/**
* @Auther: csh
* @Date: 2020/5/25 14:30
* @Description:三本大学
*/
public class ThreeUniversityFilter implements IGradeFilter{
@Override
public List<Student> conditionGradeFilter(List <Student> studentList) {
List<Student> list = new ArrayList <Student>();
for (Student student : studentList) {
if(student.getGrade()>=GradeEnum.THREE_COLLEGE.getGrade() && student.getGrade()<GradeEnum.TWO_COLLEGE.getGrade()){
list.add(student);
}
}
return list;
}
}
/**
* @Auther: csh
* @Date: 2020/5/25 14:30
* @Description:普通专科
*/
public class CommonUniversityFilter implements IGradeFilter{
@Override
public List<Student> conditionGradeFilter(List <Student> studentList) {
List<Student> list = new ArrayList <Student>();
for (Student student : studentList) {
if(student.getGrade()>=GradeEnum.COMMON_COLLEGE.getGrade() && student.getGrade()<GradeEnum.THREE_COLLEGE.getGrade()){
list.add(student);
}
}
return list;
}
}
/**
* @Auther: csh
* @Date: 2020/5/25 14:34
* @Description:演示
*/
public class Client {
public static void main(String[] args) {
List<Student> students = new ArrayList <Student>();
for(int i =0;i<20;i++){
students.add(new Student(i+"学生",getRandom(200,750)));
}
for (Student student : students) {
System.out.println(student);
}
System.out.println(students.size()+"个学生!");
System.out.println("----------------------------------------");
BestUniversityFilter best = new BestUniversityFilter();
List <Student> bestList = best.conditionGradeFilter(students);
for (Student student : bestList) {
System.out.println(student);
}
System.out.println(bestList.size()+"个"+GradeEnum.ONE_COLLEGE.getName());
System.out.println("-------------------------------------------");
SecondUniversityFilter second = new SecondUniversityFilter();
List <Student> secondList = second.conditionGradeFilter(students);
for (Student student : secondList) {
System.out.println(student);
}
System.out.println(secondList.size()+"个"+GradeEnum.TWO_COLLEGE.getName());
System.out.println("-------------------------------------------");
ThreeUniversityFilter threeFilter = new ThreeUniversityFilter();
List <Student> threeeList = threeFilter.conditionGradeFilter(students);
for (Student student : threeeList) {
System.out.println(student);
}
System.out.println(threeeList.size()+"个"+GradeEnum.THREE_COLLEGE.getName());
System.out.println("-------------------------------------------");
ThreeUniversityFilter commonFilter = new ThreeUniversityFilter();
List <Student> commonList = commonFilter.conditionGradeFilter(students);
for (Student student : commonList) {
System.out.println(student);
}
System.out.println(threeeList.size()+"个"+GradeEnum.COMMON_COLLEGE.getName());
}
//随机数
public static int getRandom(int start,int end) {
int num=(int) (Math.random()*(end-start+1)+start);
return num;
}
}
最后
该模式非常简单,也很好实现,并且很普遍,在各大框架中都有影子,比如spring mvc中的Filter,还有请求转发中的dispatcher,反正很常用,建议读者在日常开发过程中可以加以实践。