【设计模式 - 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 对象的列 ...
随机推荐
- cell的循环使用
cell的循环利用:(对cell的简单优化) 1.创建一个标示(Identifier),用于区分缓存池里的不同cell. 2.去缓存池里拿自己对应的cell,用到dequeueReusableCell ...
- CSS3—CSS3和现代Web设计
1.1 现代Web设计理念 1.1.1 可访问性第一 同样一段内容, 可以用成千上万的方法为其设计样式, 但全世界的用户应该依然可以访问它们, 不管他们用什么方式去访问Web——无论手机.键盘控制器还 ...
- ueditor富文本编辑在 asp.net MVC下使用步骤
mvc项目中用到了这个富文本编辑就试着把遇到的问题个使用步骤在这里记录一下,希望大家少走弯路. 1.首先我们先下载net版本的uediot r.
- MAC Python环境配置以及安装Pycharm 5.4.0
安装XCODE 去APP STORE下载,然后安装.免费 终端执行 xcode-select --install 安装或更新命令行开发工具 安装Pycharm 下载软件 官网:https://www. ...
- JDK源码阅读(三) Collection<T>接口,Iterable<T>接口
package java.util; public interface Collection<E> extends Iterable<E> { //返回该集合中元素的数量 in ...
- nutch 二次开发
/*深度控制*/ 深度控制:nutch是广域网的深度遍历,我们需要的是垂直采集(即只采集某一个栏目),举例,索引页总计20页,如果只有下一页,则深度为20,如果是1 2 3 4 5……20则深度为2即 ...
- ios6,ios7,ios7.1下设置UISearchbar的背景色
ios系统升级到7.1后,原来在7.0下显示正常的UISearchbar现在又出现问题了.究其原因,是由于UISearchbar的subview又做修改了. float version = [[[ U ...
- ios7新特性3-Map Kit新特性
Map Kit 框架 (MapKit.framework) 包含了大量的改进以及为基于地图的程序提供了新特性.利用地图显示位置信息的应用现在可以使用Maps这个程序用到的3D地图,包括控制程序控制视线 ...
- angularjs 资源集合
AngularJS是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方面的不足. 源码托管在Github上,其通过使用指令(directives)结构来扩展HTML词汇 ...
- 同样版本的jstl,都是jstl1.2版本,有个有问题,另一个没有问题
问题是这样的,最近部署一个项目,发现每次访问首页的时候老是报如下的错误: org.springframework.web.util.NestedServletException: Handler pr ...