前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设计模式-过滤器模式

设计模式-过滤器模式

作者头像
逍遥壮士
发布2020-09-18 11:24:20
3840
发布2020-09-18 11:24:20
举报
文章被收录于专栏:技术趋势

背景

今年的高考推迟了,每年的高考完后有喜有悲,当然每个人考得的分数不同,所以结果就不用,而一般高校都是有严格的分数界限的,比如211和985、一本、二本、三本、还有普通专科...

过滤器模式是什么?

过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。

模式可以干嘛?

其实现模式也就是用来做过滤和筛选用的,比如你考700分那肯定是一本或重点大学,当然你也可以选择读不读或选个sb的普通专科;反过来高校也是一样的,你投过来的志愿,正常情况下如果低于分数线直接不录取。

优点:

简单、方便、解耦;

缺点:

目前没有发现...

过滤器模式类图

实现代码

代码语言:javascript
复制
/**
 * @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 +
                '}';
    }
}
代码语言:javascript
复制
/**
 * @Auther: csh
 * @Date: 2020/5/25 14:16
 * @Description:分数拦截器
 */
public interface IGradeFilter {
    /** 分数拦截器*/
    List<Student> conditionGradeFilter(List<Student> studentList);
}
代码语言:javascript
复制
/**
 * @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;
    }
}
代码语言:javascript
复制
/**
 * @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;
    }
}
代码语言:javascript
复制
/**
 * @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;
    }
}
代码语言:javascript
复制
/**
 * @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;
    }
}
代码语言:javascript
复制
/**
 * @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;
    }
}
代码语言:javascript
复制
/**
 * @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,反正很常用,建议读者在日常开发过程中可以加以实践。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-06-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 技术趋势 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档