赞
踩
目录
过滤器模式(Filter Pattern)又称标准模式(Criteria Pattern)是一种设计模式,属于结构型模式。这种模式允许开发人员使用不同的标准来过滤一组对象,通过运算逻辑以解耦的方式将它们连接起来。过滤器模式可结合多个标准来获得单一标准。说白了,就是按条件筛选一组对象出来。
场景: 一群人当中有按性别划分为男人、女人;按婚姻状态划分为未婚、已婚、离异、丧偶。当需要根据按指定性别,婚姻状态寻找特定人群,则此时就很适用过滤器模式。
抽象过滤器角色
- package com.common.demo.pattern.filter;
-
- import java.util.List;
-
- /**
- * @author Evan Walker 昂焱数据: https://www.ayshuju.com
- * @version 1.0
- * @desc 过滤器接口
- * @date 2023/07/11 20:40:59
- */
- public interface Filter {
- List<People> doFilter(List<People> peopleList);
- }
具体过滤器角色
- package com.common.demo.pattern.filter;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @author Evan Walker 昂焱数据: https://www.ayshuju.com
- * @version 1.0
- * @desc 婚姻状态过滤器
- * @date 2023/07/11 21:24:27
- */
- public class MaritalTypeFilter implements Filter{
-
- private MaritalTypeEnum maritalTypeEnum;
-
- public MaritalTypeFilter(MaritalTypeEnum maritalTypeEnum) {
- // 在构造方法时指定婚姻状态
- this.maritalTypeEnum = maritalTypeEnum;
- }
-
- @Override
- public List<People> doFilter(List<People> peopleList) {
- List<People> result = new ArrayList<>();
- for (People people : peopleList) {
- if (this.maritalTypeEnum.equals(people.getMaritalType())) {
- result.add(people);
- }
- }
- return result;
- }
- }
- package com.common.demo.pattern.filter;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @author Evan Walker 昂焱数据: https://www.ayshuju.com
- * @version 1.0
- * @desc 性别过滤器
- * @date 2023/07/11 21:21:36
- */
- public class GenderFilter implements Filter {
-
- private GenderEnum genderEnum;
-
- public GenderFilter(GenderEnum genderEnum) {
- // 在构造方法时指定性别
- this.genderEnum = genderEnum;
- }
-
- @Override
- public List<People> doFilter(List<People> peopleList) {
- List<People> result = new ArrayList<>();
- for (People people : peopleList) {
- if (this.genderEnum.equals(people.getGender())) {
- result.add(people);
- }
- }
- return result;
- }
- }
被过滤的主体角色
- package com.common.demo.pattern.filter;
-
- /**
- * @author Evan Walker 昂焱数据: https://www.ayshuju.com
- * @version 1.0
- * @desc 人实体类
- * @date 2023/07/11 20:41:11
- */
- public class People {
-
- private String name;
- /**
- * 1.男-MALE 2.女-FEMALE 3.人妖-HERMAPHRODITE
- */
- private GenderEnum gender;
-
- /**
- * 1.未婚-UNMARRIED 2.已婚-MARRIED 3.离婚-DIVORCED 4.丧偶-WIDOWED
- */
- private MaritalTypeEnum maritalType;
-
-
- public People() {
- }
-
- public People(String name,GenderEnum gender, MaritalTypeEnum maritalType) {
- this.name = name;
- this.gender = gender;
- this.maritalType = maritalType;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public GenderEnum getGender() {
- return gender;
- }
-
- public void setGender(GenderEnum gender) {
- this.gender = gender;
- }
-
- public MaritalTypeEnum getMaritalType() {
- return maritalType;
- }
-
- public void setMaritalType(MaritalTypeEnum maritalType) {
- this.maritalType = maritalType;
-
- }
-
- @Override
- public String toString() {
- return "People{" +
- "name='" + name + '\'' +
- ", gender=" + gender +
- ", maritalType=" + maritalType +
- '}';
- }
- }
辅助枚举类
- package com.common.demo.pattern.filter;
-
- /**
- * @author Evan Walker 昂焱数据: https://www.ayshuju.com
- * @version 1.0
- * @desc 婚姻状态枚举
- * @date 2023/07/11 20:49:30
- */
- public enum MaritalTypeEnum {
-
- UNMARRIED(1,"未婚"),
- MARRIED(2,"已婚"),
- DIVORCED(3,"离婚"),
- WIDOWED(4,"丧偶"),
- ;
-
- private int code;
- private String msg;
-
- MaritalTypeEnum(int code, String msg) {
- this.code = code;
- this.msg = msg;
- }
- }
- package com.common.demo.pattern.filter;
-
- /**
- * @author Evan Walker 昂焱数据: https://www.ayshuju.com
- * @version 1.0
- * @desc 性别枚举
- * @date 2023/07/11 20:48:02
- */
- public enum GenderEnum {
-
- MALE(1,"男"),
- FEMALE(2,"女"),
- HERMAPHRODITE(3,"人妖"),
- ;
-
- private int code;
- private String msg;
-
- GenderEnum(int code, String msg) {
- this.code = code;
- this.msg = msg;
- }
- }
测试类
- package com.common.demo.pattern.filter;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @author Evan Walker 昂焱数据: https://www.ayshuju.com
- * @version 1.0
- * @desc 测试类
- * @date 2023/07/09 14:21:58
- */
- public class Test {
-
- public static void main(String[] args) {
- // 数据初始化
- List<People> personList = new ArrayList<People>();
- personList.add(new People("张美丽", GenderEnum.FEMALE, MaritalTypeEnum.DIVORCED));
- personList.add(new People("老张", GenderEnum.MALE, MaritalTypeEnum.MARRIED));
- personList.add(new People("小明", GenderEnum.MALE, MaritalTypeEnum.UNMARRIED));
- personList.add(new People("李花花", GenderEnum.FEMALE, MaritalTypeEnum.MARRIED));
- personList.add(new People("王五", GenderEnum.MALE, MaritalTypeEnum.MARRIED));
- personList.add(new People("王麻子", GenderEnum.MALE, MaritalTypeEnum.WIDOWED));
-
- // 女性
- GenderFilter genderFilter = new GenderFilter(GenderEnum.FEMALE);
- List<People> list = genderFilter.doFilter(personList);
- System.out.println("女性为:" + list);
-
- // 男性
- genderFilter = new GenderFilter(GenderEnum.MALE);
- List<People> list1 = genderFilter.doFilter(personList);
- System.out.println("男性为:" + list1);
-
- // 男性未婚
- genderFilter = new GenderFilter(GenderEnum.MALE);
- MaritalTypeFilter maritalTypeFilter = new MaritalTypeFilter(MaritalTypeEnum.UNMARRIED);
- List<People> list2 = maritalTypeFilter.doFilter(genderFilter.doFilter(personList));
- System.out.println("男性未婚:" + list2);
-
- }
- }
测试截图
更多消息资讯,请访问昂焱数据(https://www.ayshuju.com)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。