【设计模式 - 7】之过滤器模式(Filter)
1、模式简介
过滤器模式(Filter)也叫标准模式(Criteria),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。
2、实例
这个实例的需求如下:
Person类有三个属性:姓名(Name)、性别(Gender)和婚姻情况(Marital),我们的系统中的一些功能需要对这些属性进行筛选,比如:
- 得到所有的男性;
- 得到所有的女性;
- 得到所有还单身的人;
- 得到所有已婚的人。
系统还希望能够将这些条件组合起来进行筛选,比如:
- 得到所有已婚男性;
- 得到所有单身女性;
- 得到所有已婚的人或女性;
分析:
要解决这个问题,我们可以使用过滤器模式。
代码:
Person类中的代码:
- public class Person {
- private String name; // 姓名
- private String gender; // 性别
- private String marital; // 婚姻情况
- public Person(String name, String gender, String marital) {
- this.name = name;
- this.gender = gender;
- this.marital = marital;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- public String getMarital() {
- return marital;
- }
- public void setMarital(String marital) {
- this.marital = marital;
- }
- @Override
- public String toString() {
- return "Person [name=" + name + ",gender=" + gender + ", marital=" + marital + "]";
- }
- }
Filter接口中的代码:
- public interface Filter {
- // 根据传过来的Person列表,根据一定的条件过滤,得到目标集合
- List<Person> filter(List<Person> persons);
- }
MaleFilter类中的代码:
- public class MaleFilter implements Filter {
- @Override
- public List<Person> filter(List<Person> persons) {
- List<Person> result = new ArrayList<>();
- for (Person person : persons) {
- if ("MALE".equalsIgnoreCase(person.getGender())) {
- result.add(person);
- }
- }
- return result;
- }
- }
处理“并且”逻辑的过滤器类FilterAnd类中的代码:
- public class FilterAnd implements Filter {
- private Filter filter;
- private Filter otherFilter;
- public FilterAnd(Filter filter, Filter otherFilter) {
- this.filter = filter;
- this.otherFilter = otherFilter;
- }
- @Override
- public List<Person> filter(List<Person> persons) {
- List<Person> tmpList = filter.filter(persons);
- return otherFilter.filter(tmpList);
- }
- }
处理“或者”逻辑的过滤器类FilterOr类中的代码:
- public class FilterOr implements Filter {
- private Filter filter;
- private Filter otherFilter;
- public FilterOr(Filter filter, Filter otherFilter) {
- this.filter = filter;
- this.otherFilter = otherFilter;
- }
- @Override
- public List<Person> filter(List<Person> persons) {
- List<Person> tmpList1 = filter.filter(persons);
- List<Person> tmpList2 = otherFilter.filter(persons);
- for (Person person : tmpList2) {
- if (!tmpList1.contains(person)) {
- tmpList1.add(person);
- }
- }
- return tmpList1;
- }
- }
测试类Test中的代码:
- public class Test {
- public static void main(String[] args) {
- // 初始化数据
- List<Person> persons = new ArrayList<>();
- persons.add(new Person("霍一", "FEMALE", "MARRIED"));
- persons.add(new Person("邓二", "MALE", "MARRIED"));
- persons.add(new Person("张三", "MALE", "SINGLE"));
- persons.add(new Person("李四", "FEMALE", "MARRIED"));
- persons.add(new Person("王五", "MALE", "SINGLE"));
- persons.add(new Person("赵六", "FEMALE", "SINGLE"));
- persons.add(new Person("孙七", "MALE", "SINGLE"));
- persons.add(new Person("罗八", "MALE", "MARRIED"));
- persons.add(new Person("刘九", "FEMALE", "SINGLE"));
- persons.add(new Person("史十", "FEMALE", "SINGLE"));
- // 打印出所有男性的信息
- System.out.println("---------------------所有男性---------------------");
- List<Person> maleList = new MaleFilter().filter(persons);
- printList(maleList);
- // 打印出所有单身的信息
- System.out.println("---------------------所有单身---------------------");
- List<Person> singleList = new SingleFilter().filter(persons);
- printList(singleList);
- // 打印出所有已婚女性的信息
- System.out.println("--------------------所有已婚女性-------------------");
- List<Person> marriedFemaleList = new FilterAnd(new MarriedFilter(), new FemaleFilter()).filter(persons);
- printList(marriedFemaleList);
- // 打印出所有单身或女性的信息
- System.out.println("-------------------所有单身或女性------------------");
- List<Person> singleOrFemaleList = new FilterOr(new SingleFilter(), new FemaleFilter()).filter(persons);
- printList(singleOrFemaleList);
- }
- // 打印列表中的数据信息
- private static void printList(List<Person> list) {
- for (Person person : list) {
- System.out.println(person.toString());
- }
- }
- }
测试代码如下图所示:
最后贴出过滤器模式的GitHub代码:【GitHub - Filter】。
【设计模式 - 7】之过滤器模式(Filter)的更多相关文章
- 基础设计模式-03 从过滤器(Filter)校验链学习职责链模式
1.职责链路模式 1.1UML图 1.2 职责链路模式的概念 为了避免处理对象的耦合关系,将对象连成一个链,沿着这个链进行访问,直到有一个对象处理位置: 1.3 优点 1.按照一定的顺序执行判断: 2 ...
- 设计模式系列之过滤器模式(Chriteria Pattern)
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来.这种类 ...
- 设计模式のFilterPattern(过滤器模式)----结构模式
一.产生背景 我们有一堆“人”的对象,我们应该怎么选择出其中的男性.女性或者其他类型的呢?这时候我们可以用过滤器模式 二.通常做法 我们将创建一个 Person 对象.Criteria 接口和实现了该 ...
- Java拦截过滤器模式
当我们想要对应用程序的请求或响应进行一些预处理/后处理时,使用截取过滤器设计模式. 在将请求传递到实际目标应用程序之前,在请求上定义和应用过滤器. 过滤器可以进行请求的认证/授权/日志记录或跟踪,然后 ...
- 过滤器模式(Filter Pattern)
过滤器模式 一.什么是过滤器模式 过滤器模式(Filter Pattern),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来.这种类型的设计模式属于结构型 ...
- 设计模式之过滤器模式(php实现)
/** * github地址:https://github.com/ZQCard/design_pattern * 过滤器模式(Filter Pattern)或标准模式(Criteria Patter ...
- [07]Go设计模式:过滤器模式(FilterPattern)
目录 过滤器模式 一.简介 二.代码 三.参考链接 过滤器模式 一.简介 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使 ...
- Java设计模式应用——过滤器模式
storm引擎计算出一批中间告警结果,会发送一条kafka消息给告警入库服务,告警入库服务接收到kafka消息后读取中间告警文件,经过一系列处理后把最终告警存入mysql中. 实际上,中间告警结果可能 ...
- 设计模式之过滤器模式——Java语言描述
过滤器模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来 实现 创建一个Person对象.Criteria 接口和实现了该接口的实体类,来过滤 Person 对象的列 ...
随机推荐
- JS-SDK微信支付开发攻略
一.吐槽篇 一个字——坑!两个字——很坑!三个字——非常坑!首先,微信支付接口作为微信开发接口的一部分,竟然有一本书那么厚的官方文档,共36页,更重要的是,这36页还不能把开发的流程说清楚,描述过于分 ...
- Photon开发实战(2)——开发框架、第一个Photon程序
Photon基础开发框架 Photon (v4)的基本框架.开发框架主要Photon和游戏逻辑(C#)两个部分,如下图最新的Photon v4支持的4种底层协议,游戏开发逻辑Photon目前主要划分为 ...
- preg_replace($pattern, $replacement, $content) 修饰符的奇葩作用
$str = "<span>lin</span> == <span>3615</span>";$pattern = "/& ...
- 关于webapp的一个webframe问题
最近重启ios webapp的项目,将之前的框架拿过来发现出现了错误,错误出现在写JSAlart控件的WebFrame上,xcode会报WebFrame是未定义的错误.由于之前使用的是ios5的 sd ...
- 【python】python支持中文变量,醉了
哈哈 = 1 呜呜 = -1 哈哈 + 呜呜 = 0
- 反射 DataTable拓展方法 转实体对象、实体集合、JSON
Mapper类 using System; using System.Collections.Generic; using System.Data; using System.Globalizatio ...
- iOS版 hello,world版本2
// // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. All rig ...
- 最常用的动态sql语句梳理——分享给使用Mybatis的小伙伴们!
公司项目中一直使用Mybatis作为持久层框架,自然,动态sql写得也比较多了,最常见的莫过于在查询语句中使用if标签来动态地改变过滤条件了.Mybatis的强大特性之一便是它的动态sql,免除了拼接 ...
- Netty版本升级血泪史之线程篇
1. 背景 1.1. Netty 3.X系列版本现状 根据对Netty社区部分用户的调查,结合Netty在其它开源项目中的使用情况,我们可以看出目前Netty商用的主流版本集中在3.X和4.X上,其中 ...
- Android 使用HttpClient方式提交POST请求
final String username = usernameEditText.getText().toString().trim(); final String password = passwr ...