什么是过滤器模式?
过滤器模式(Filter Pattern)又称作标准模式(Criteria Pattern),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。
过滤器模式就像是一连串的过滤器,按条件或标准筛选一组对象出来。
适用场景
代码实现
示例: 找对象
假设要求:白富美 18-23岁 女性
//示例: 找对象
//假设要求:白富美 18-23岁 女性
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 人类
*/
class Person {
private String name;//姓名
private int age;//年龄
private String gender;//性别
private String skinColour;//肤色
private String riches;//财富
private String looks;//长相
public Person(String name, int age, String gender, String skinColour, String riches, String looks) {
this.name = name;
this.age = age;
this.gender = gender;
this.skinColour = skinColour;
this.riches = riches;
this.looks = looks;
}
@Override
public String toString() {
return
"姓名:" + name +
", 年龄:" + age +
", 性别:" + gender +
", 肤色:" + skinColour +
", 财富:" + riches +
", 长相:" + looks;
}
//region IDEA折叠代码
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getSkinColour() {
return skinColour;
}
public void setSkinColour(String skinColour) {
this.skinColour = skinColour;
}
public String getRiches() {
return riches;
}
public void setRiches(String riches) {
this.riches = riches;
}
public String getLooks() {
return looks;
}
public void setLooks(String looks) {
this.looks = looks;
}
//endregion
}
/**
* 过滤器
*/
interface Filter {
List<Person> filter(List<Person> persons);
}
/**
* 过滤年龄
* 要18-23岁
*/
class AgeFilter implements Filter {
@Override
public List<Person> filter(List<Person> persons) {
return persons
.stream()
.filter(person -> person.getAge() >= 18 && person.getAge() < +23)
.collect(Collectors.toList());
}
}
/**
* 过滤性别
* 要女性
*/
class GenderFilter implements Filter {
@Override
public List<Person> filter(List<Person> persons) {
return persons
.stream()
.filter(person -> person.getGender().equals("女性"))
.collect(Collectors.toList());
}
}
/**
* 过滤肤色,要白色
*/
class SkinColourFilter implements Filter {
@Override
public List<Person> filter(List<Person> persons) {
return persons
.stream()
.filter(person -> person.getSkinColour().equals("白色"))
.collect(Collectors.toList());
}
}
/**
* 过滤财富,要富裕
*/
class RichesFilter implements Filter {
@Override
public List<Person> filter(List<Person> persons) {
return persons
.stream()
.filter(person -> person.getSkinColour().equals("富裕"))
.collect(Collectors.toList());
}
}
/**
* 过滤长相,要好看
*/
class LooksFilter implements Filter {
@Override
public List<Person> filter(List<Person> persons) {
return persons
.stream()
.filter(person -> person.getSkinColour().equals("好看"))
.collect(Collectors.toList());
}
}
/**
* 过滤器模式
*/
public class FilterPattern {
public static void main(String[] args) {
//选妃大赛 找对象
//准备妃子
List<Person> persons = new ArrayList<>();
persons.add(new Person("小红", 3, "女", "白色", "富裕", "好看"));
persons.add(new Person("小蓝", 25, "女", "黄色", "贫穷", "难看"));
persons.add(new Person("小绿", 5, "男", "白色", "富裕", "好看"));
persons.add(new Person("小白", 56, "女", "白色", "贫穷", "好看"));
persons.add(new Person("小橙", 35, "女", "黄色", "富裕", "好看"));
persons.add(new Person("小黄", 12, "男", "黑色", "贫穷", "难看"));
persons.add(new Person("小青", 23, "女", "黄色", "富裕", "好看"));
persons.add(new Person("小丹", 18, "女", "白色", "贫穷", "好看"));
persons.add(new Person("小紫", 16, "男", "白色", "贫穷", "难看"));
persons.add(new Person("小粉", 17, "女", "黄色", "富裕", "好看"));
persons.add(new Person("小褐", 19, "女", "白色", "贫穷", "难看"));
persons.add(new Person("小棕", 20, "女", "黄色", "富裕", "难看"));
persons.add(new Person("小黑", 43, "男", "白色", "贫穷", "好看"));
persons.add(new Person("小灰", 91, "女", "黑色", "贫穷", "难看"));
persons.add(new Person("小暗", 32, "女", "白色", "富裕", "好看"));
persons.add(new Person("小亮", 56, "女", "黑色", "贫穷", "难看"));
//过滤器
AgeFilter ageFilter = new AgeFilter();
GenderFilter genderFilter = new GenderFilter();
SkinColourFilter skinColourFilter = new SkinColourFilter();
RichesFilter richesFilter = new RichesFilter();
LooksFilter looksFilter = new LooksFilter();
//层层选拔
System.out.println("选拨前人数:" + persons.size());
persons = ageFilter.filter(persons);
System.out.println("过滤年龄后人数:" + persons.size());
persons = genderFilter.filter(persons);
System.out.println("过滤性别后人数:" + persons.size());
persons = skinColourFilter.filter(persons);
System.out.println("过滤肤色后人数:" + persons.size());
persons = richesFilter.filter(persons);
System.out.println("过滤富裕后人数:" + persons.size());
persons = looksFilter.filter(persons);
System.out.println("过滤长相后人数:" + persons.size());
//输出:
//选拨前人数:16
//过滤年龄后人数:3
//过滤性别后人数:0
//过滤肤色后人数:0
//过滤富裕后人数:0
//过滤长相后人数:0
//打光棍吧 没找到 ……
}
}