过滤器模式
一、什么是过滤器模式
过滤器模式(Filter Pattern),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
二、具体实现
1、主要角色
过滤对象:需要过滤的对象。
过滤条件接口:抽象过滤条件的接口。 具体的过滤条件:根据需要实现过滤条件接口的具体过滤条件。2、代码实现
1、需要过滤的对象
package com.designpattern.filterPattern;/** * 过滤的对象 * * @author zhongtao on 2018/12/12 */public class Person { private String name; private String gender; //性别 private int age; 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(String name, String gender, int age) { this.name = name; this.gender = gender; this.age = age; } public Person() { } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", gender='" + gender + '\'' + ", age=" + age + '}'; }}
2、过滤条件接口
package com.designpattern.filterPattern;import java.util.List;/** * 过滤条件接口 * * @author zhongtao on 2018/12/12 */public interface FilterCriteria { ListfilterCriteria(List persons);}
3、具体过滤条件 实现过滤条件接口
package com.designpattern.filterPattern;import java.util.List;import java.util.Objects;import java.util.stream.Collectors;/** * 过滤男性 * * @author zhongtao on 2018/12/12 */public class MaleFilter implements FilterCriteria { @Override public ListfilterCriteria(List persons) { //通过流来处理 return persons.stream().filter(person -> Objects.equals(person.getGender(), "MALE")).collect(Collectors.toList()); }}package com.designpattern.filterPattern;import java.util.List;import java.util.stream.Collectors;/** * 年龄大于20 * * @author zhongtao on 2018/12/12 */public class AgeGreaterThan20Filter implements FilterCriteria { @Override public List filterCriteria(List persons) { return persons.stream().filter(person -> person.getAge() > 20).collect(Collectors.toList()); }}package com.designpattern.filterPattern;import java.util.List;import java.util.Objects;import java.util.stream.Collectors;/** * 过滤男性并且年龄大于20 * * @author zhongtao on 2018/12/12 */public class MaleAndAgeGreaterThan20Filter implements FilterCriteria { @Override public List filterCriteria(List persons) { return persons.stream().filter(person -> Objects.equals(person.getGender(), "MALE")) .filter(person -> person.getAge() > 20) .collect(Collectors.toList()); }}
4、测试过滤器模式
package com.designpattern.filterPattern;import org.junit.Test;import java.util.ArrayList;import java.util.List;/** * @author zhongtao on 2018/12/12 */public class FilterPatternTest { /** * 测试过滤器模式 */ @Test public void testFilterPattern() { Listpersons = new ArrayList<>(); persons.add(new Person("Peter", "MALE", 24)); persons.add(new Person("Mary", "FEMALE", 22)); persons.add(new Person("Bob", "MALE", 18)); persons.add(new Person("Lina", "FEMALE", 20)); persons.add(new Person("Luna", "FEMALE", 21)); FilterCriteria maleFilter = new MaleFilter(); List males = maleFilter.filterCriteria(persons); System.out.println("男的:" + males); FilterCriteria ageGreaterThan20 = new AgeGreaterThan20Filter(); List ageThan20 = ageGreaterThan20.filterCriteria(persons); System.out.println("年龄大于20的人:" + ageThan20); FilterCriteria maleAndAgeGreaterThan20 = new MaleAndAgeGreaterThan20Filter(); List personList = maleAndAgeGreaterThan20.filterCriteria(persons); System.out.println("男的且年龄大于20的人:" + personList); }}
5、测试结果
男的:[Person{name='Peter', gender='MALE', age=24}, Person{name='Bob', gender='MALE', age=18}]年龄大于20的人:[Person{name='Peter', gender='MALE', age=24}, Person{name='Mary', gender='FEMALE', age=22}, Person{name='Luna', gender='FEMALE', age=21}]男的且年龄大于20的人:[Person{name='Peter', gender='MALE', age=24}]
源码地址: