标签:Collector new als public 处理 过滤器 interface href auth
??过滤器模式(Filter Pattern),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
过滤对象:需要过滤的对象。
过滤条件接口:抽象过滤条件的接口。
具体的过滤条件:根据需要实现过滤条件接口的具体过滤条件。
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 {
List<Person> filterCriteria(List<Person> 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 List<Person> filterCriteria(List<Person> 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<Person> filterCriteria(List<Person> 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<Person> filterCriteria(List<Person> 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() {
List<Person> persons = 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<Person> males = maleFilter.filterCriteria(persons);
System.out.println("男的:" + males);
FilterCriteria ageGreaterThan20 = new AgeGreaterThan20Filter();
List<Person> ageThan20 = ageGreaterThan20.filterCriteria(persons);
System.out.println("年龄大于20的人:" + ageThan20);
FilterCriteria maleAndAgeGreaterThan20 = new MaleAndAgeGreaterThan20Filter();
List<Person> 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}]
源码地址:https://github.com/zt19994/designPattern
标签:Collector new als public 处理 过滤器 interface href auth
原文地址:https://www.cnblogs.com/zt19994/p/10112062.html