深入浅出过滤器模式

简介: 深入浅出过滤器模式

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

过滤器模式中几个常见的角色:

  1. AbstractFilter(抽象过滤器角色):在客户端可以调用它的方法,在抽象过滤器角色中可以知道相关的(一个或者多个)子系统的功能和责任;在正常情况下,它将所有从客户端发来的请求委派到相应的实现类去,传递给相应的实现类对象处理。
  2. ConcreteFilter(具体滤器角色):在客户端可以调用它的方法,在具体滤器角色会对目标对象集合进行逻辑过滤,最后再返回一个过滤后的集合。
  3. 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'}]
复制代码


参考:

https://blog.csdn.net/weixx3/article/details/80233991

https://www.runoob.com/design-pattern/filter-pattern.html


相关文章
|
人工智能 安全 机器人
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
OpenClaw(原Clawdbot)是一款开源本地AI助手,支持钉钉、飞书等多平台接入。本教程手把手指导Linux下部署与钉钉机器人对接,涵盖环境配置、模型选择(如Qwen)、权限设置及调试,助你快速打造私有、安全、高权限的专属AI助理。(239字)
40919 184
|
存储 缓存 NoSQL
阿里云 Tair 联手 SGLang 共建 HiCache,构建面向“智能体式推理”的缓存新范式
本文系统剖析面向智能体推理的 KVCache 技术演进,针对传统机制在长上下文、多轮决策与多智能体协同中的状态膨胀、持久化缺失和缓存孤立三大瓶颈,介绍阿里云 Tair KVCache 团队联合 SGLang 社区推出的 HiCache 分层缓存体系。该方案通过显存-内存-3FS 多级卸载与全局共享,实现缓存命中率提升至80%,TTFT 降低56%,推理 QPS 翻倍,支撑智能体时代的大模型高效推理。
|
存储 人工智能 运维
阿里云 Tair 基于 3FS 工程化落地 KVCache:企业级部署、高可用运维与性能调优实践
阿里云 Tair KVCache 团队联合硬件团队对 3FS 进行深度优化,通过 RDMA 流量均衡、小 I/O 调优及全用户态落盘引擎,提升 4K 随机读 IOPS 150%;增强 GDR 零拷贝、多租户隔离与云原生运维能力,构建高性能、高可用、易管理的 KVCache 存储底座,助力 AI 大模型推理降本增效。
|
安全 Linux 数据安全/隐私保护
阿里云镜像仓库:拉取和推送Docker镜像
阿里云镜像仓库:拉取和推送Docker镜像
44419 2
阿里云镜像仓库:拉取和推送Docker镜像
|
人工智能 自然语言处理 计算机视觉
推荐收藏!2024年新版GPT详细论文润色指南【更新至2024年4月30日】
推荐收藏!2024年新版GPT详细论文润色指南【更新至2024年4月30日】
1772 0
|
存储 分布式计算 Kubernetes
PVC、Ceph、HDFS、CPFS、NFS和OSS
PVC、Ceph、HDFS、CPFS、NFS和OSS都是分布式存储系统,下面是它们的详细介绍: 1. PVC(Persistent Volume Claim):PVC是Kubernetes中的一个资源请求,用于声明在集群中存储持久化数据的需求。PVC可以被用于存储应用程序的数据,例如数据库、日志文件等。PVC由Kubernetes集群管理员创建,并在Kubernetes集群中进行分配。
1723 0
|
机器学习/深度学习 智能网卡 数据中心
详解灵骏智能算力之可预期高性能网络
从灵骏解析阿里云可预期高性能网络
详解灵骏智能算力之可预期高性能网络

热门文章

最新文章