设计模式之过滤器模式FilterPattern(十)

news/2024/9/22 18:31:16/

一、过滤器模式

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

二、组件介绍

1、过滤器接口(Filter)
定义了过滤器的基本方法,具体的实现还要具体过滤器角色去参与,在实际应用中可以扩展该接口以适应不同的过滤条件
2 具体命过滤器(ConcreteFilter)
实现了过滤器接口,负责执行具体的过滤操作。对数据进行过滤
3 过滤链(FilterChain)
将多个过滤器按照一定的顺序组合起来,形成一个过滤器链,依次对数据进行过滤

三、代码实战

1、Person

package com.xu.demo.filterPattern;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** 数据实体类*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {private String name;//姓名private String sex;  // M表示男性,F表示女性private Integer age;//年龄}

 2、IFilter

package com.xu.demo.filterPattern;import java.util.List;/*** 过滤器接口*/
public interface IFilter {/*** 过滤操作* @param personList* @return*/List<Person> filterOperate(List<Person> personList);}

 3、NameFilter

package com.xu.demo.filterPattern;import java.util.List;/*** 过滤出性别为男的同学*/
public class NameFilter implements IFilter {@Overridepublic List<Person> filterOperate(List<Person> personList) {return personList.stream().filter(person ->person.getName().startsWith("J")).toList();}
}

 4、AgeFilter

package com.xu.demo.filterPattern;import java.util.List;/*** 过滤已满足18岁的同学*/
public class AgeFilter implements IFilter {@Overridepublic List<Person> filterOperate(List<Person> personList) {return personList.stream().filter(person ->person.getAge() > 25).toList();}
}

 5、SexFilter

 

package com.xu.demo.filterPattern;import java.util.List;/*** 过滤已满足18岁的同学*/
public class SexFilter implements IFilter {@Overridepublic List<Person> filterOperate(List<Person> personList) {return personList.stream().filter(person ->"M".equals(person.getSex())).toList();}
}

6、FilterChain

package com.xu.demo.filterPattern;import org.apache.commons.collections.CollectionUtils;
import java.util.ArrayList;
import java.util.List;public class FilterChain {private List<IFilter> filters;public FilterChain(List<IFilter> filters) {this.filters = filters;}public List<Person> doFilter(List<Person> personList) {if (CollectionUtils.isEmpty(filters) || CollectionUtils.isEmpty(personList)) {return new ArrayList<>();}List<Person> afterFilterShapes = new ArrayList<>(personList);// 执行过滤for (IFilter filter : filters) {afterFilterShapes = filter.filterOperate(afterFilterShapes);}return afterFilterShapes;}
}

7、FilterPattern

package com.xu.demo.filterPattern;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class FilterPattern {public static void main(String[] args) {//待过滤的数据List<Person> peoples = new ArrayList<>();peoples.add(new Person("Jenny", "F", 20));peoples.add(new Person("John", "M", 30));peoples.add(new Person("Jack", "M", 35));peoples.add(new Person("Mark", "M", 30));peoples.add(new Person("Ronnie", "M", 25));peoples.add(new Person("Ding", "M", 25));peoples.add(new Person("Janet", "F", 35));peoples.add(new Person("Jessie", "F", 25));//过滤器IFilter nameFilter = new NameFilter();IFilter ageFilter = new AgeFilter();IFilter sexFilter = new SexFilter();//过滤链FilterChain filter1 = new FilterChain(Arrays.asList(nameFilter, ageFilter));FilterChain filter2 = new FilterChain(Arrays.asList(ageFilter, sexFilter));FilterChain filter3 = new FilterChain(Arrays.asList(nameFilter, sexFilter));//过滤链过滤出数据List<Person> filteredPeople1 = filter1.doFilter(peoples);List<Person> filteredPeople2 = filter2.doFilter(peoples);List<Person> filteredPeople3 = filter3.doFilter(peoples);System.out.println("================名字以J开头和年龄大于25岁==================");filteredPeople1.forEach(System.out::println);System.out.println("================年龄大于25岁和性别是M男性==================");filteredPeople2.forEach(System.out::println);System.out.println("================名字以J开头和性别是M男性===================");filteredPeople3.forEach(System.out::println);}}

 运行结果:

 至此,整个过滤器模式就基本实现了,注意体会代码之间的关系,我们下回再见。

 

 


http://www.ppmy.cn/news/1467354.html

相关文章

通过LabVIEW提升生产设备自动化水平

现代制造业对生产设备的自动化水平提出了越来越高的要求。使用LabVIEW这一强大的图形化编程环境&#xff0c;可以显著提升生产设备的自动化程度&#xff0c;改善生产效率和产品质量。本文将详细分析如何通过LabVIEW改善生产设备的自动化水平&#xff0c;并提供具体的实施策略与…

Spring系统学习 - Spring入门

什么是Spring&#xff1f; Spring翻译过来就是春天的意思&#xff0c;字面意思&#xff0c;冠以Spring的意思就是想表示使用这个框架&#xff0c;代表程序员的春天来了&#xff0c;实际上就是让开发更加简单方便&#xff0c;实际上Spring确实做到了。 官网地址&#xff1a;ht…

Matplotlib | 绘制柱状图

简介 安装 Matplotlib 开始绘制 简单柱状图 改变颜色 改变纹理 改变边框样式 改变透明度 改变柱子宽度 改变图表标题 ​编辑 并列柱状图 横向柱状图 堆叠柱状图 更多函数 简介 柱状图&#xff08;Bar chart&#xff09;&#xff0c;是一种以长方形的长度为变量的…

054、Python 函数的概念以及定义

编程大师Martin Fowler曾说过&#xff1a;“代码有很多种坏味道&#xff0c;重复是最坏的一种。” 那么遇到重复的代码&#xff0c;如何做&#xff1f;答案就是&#xff1a;函数。 函数就是把重复的代码封装在一起&#xff0c;然后通过调用该函数从而实现在不同地方运行同样的…

回溯之分割回文串

题目&#xff1a; 给你一个字符串 s&#xff0c;请你将 s 分割成一些子串&#xff0c;使每个子串都是 回文串 。返回 s 所有可能的分割方案。 示例 1&#xff1a; 输入&#xff1a;s "aab" 输出&#xff1a;[["a","a","b"],[&quo…

图解Mysql索引原理

概述 是什么 索引像是一本书的目录列表&#xff0c;能根据目录快速的找到具体的书本内容&#xff0c;也就是加快了数据库的查询速度索引本质是一个数据结构索引是在存储引擎层&#xff0c;而不是服务器层实现的&#xff0c;所以&#xff0c;并没有统一的索引标准&#xff0c;…

阻性负载和感性负载的区别

1.阻性负载&#xff1a; 阻性负载是指电路中只包含电阻元件的情况。这种负载会使得电流与电压之间呈现出线性关系&#xff0c;即满足欧姆定律。 当负载电流负载电压没有相位差时负载为阻性(如负载为白炽灯、电炉)。 2.感性负载&#xff1a; 感性负载指的是电路中含有电感元…

CMakeLists.txt和Package.xml

CMakeLists.txt和Package.xml CMakeLists.txt 总览 CMakeLists.txt 是用于定义如何构建 ROS (Robot Operating System) 包的 CMake 脚本文件。CMake 是一个跨平台的构建系统&#xff0c;用于自动化编译过程。在 ROS 中&#xff0c;CMakeLists.txt 文件指定了如何编译代码和链…