过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种行为型设计模式。这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。
一、UML类图
二、代码示例
过滤器接口,和一些逻辑组合的接口实现类
package filter;
public interface Criteria<T> {
boolean matches(T candidate);
// 只有default方法才可以有方法体
default Criteria<T> and(Criteria<T> other) {
return new AndCriteria<T>(this, other);
}
// 只有default方法才可以有方法体
default Criteria<T> or(Criteria<T> other) {
return new OrCriteria<T>(this, other);
}
class AndCriteria<T> implements Criteria<T> {
private Criteria<T> thisCriteria, thatCriteria;
public AndCriteria(Criteria<T> thisCriteria, Criteria<T> thatCriteria) {
this.thisCriteria = thisCriteria;
this.thatCriteria = thatCriteria;
}
@Override
public boolean matches(T candidate) {
return thisCriteria.matches(candidate) && thatCriteria.matches(candidate);
}
}
class OrCriteria<T> implements Criteria<T> {
private Criteria<T> thisCriteria, thatCriteria;
public OrCriteria(Criteria<T> thisCriteria, Criteria<T> thatCriteria) {
this.thisCriteria = thisCriteria;
this.thatCriteria = thatCriteria;
}
@Override
public boolean matches(T candidate) {
return thisCriteria.matches(candidate) || thatCriteria.matches(candidate);
}
}
class NotCriteria<T> implements Criteria<T> {
private Criteria<T> thisCriteria;
public NotCriteria(Criteria<T> thisCriteria) {
this.thisCriteria = thisCriteria;
}
@Override
public boolean matches(T candidate) {
return !thisCriteria.matches(candidate);
}
}
}
具体的过滤器实现类
public class MaleCriteria implements Criteria<Person> {
@Override
public boolean matches(Person candidate) {
return candidate.getGender().equalsIgnoreCase("male");
}
}
public class FemaleCriteria implements Criteria<Person> {
@Override
public boolean matches(Person candidate) {
return candidate.getGender().equalsIgnoreCase("female");
}
}
public class AdultCriteria implements Criteria<Person> {
@Override
public boolean matches(Person candidate) {
return candidate.getAge() >= 18;
}
}
被过滤对象
package filter;
public class Person {
private String name, gender;
private int age;
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = 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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
}
客户类
package filter;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Client {
public static void main(String[] args) {
List<Person> persons = Arrays.asList(
new Person("Zhang", "female", 28),
new Person("Wang", "male", 17),
new Person("Li", "female", 19),
new Person("Sun", "male", 22),
new Person("Han", "female", 16)
);
System.out.println("******************** old school ********************\n");
System.out.println("all male people --->>>");
System.out.println(persons.stream().filter(p -> new MaleCriteria().matches(p)).collect(Collectors.toList()) + "\n");
System.out.println("all male and adult female people --->>>");
System.out.println(persons.stream().filter(p -> new MaleCriteria().or(new AdultCriteria().and(new FemaleCriteria())).matches(p)).collect(Collectors.toList()) + "\n");
System.out.println("******************** new style with Predicate ********************\n");
// 完全不需要写那么多鸡肋的Criteria,在Java 8中完全可用Predicate来替代
Predicate<Person> isMale = p -> p.getGender().equalsIgnoreCase("male");
Predicate<Person> isFemale = p -> p.getGender().equalsIgnoreCase("female");
Predicate<Person> isAdult = p -> p.getAge() >= 18;
System.out.println(persons.stream().filter(isMale).collect(Collectors.toList()));
System.out.println(persons.stream().filter(isMale.or(isFemale.and(isAdult))).collect(Collectors.toList()));
}
}
测试结果
******************** old school ********************
all male people --->>>
[Person{name='Wang', gender='male', age=17}, Person{name='Sun', gender='male', age=22}]
all male and adult female people --->>>
[Person{name='Zhang', gender='female', age=28}, Person{name='Wang', gender='male', age=17}, Person{name='Li', gender='female', age=19}, Person{name='Sun', gender='male', age=22}]
******************** new style with Predicate ********************
[Person{name='Wang', gender='male', age=17}, Person{name='Sun', gender='male', age=22}]
[Person{name='Zhang', gender='female', age=28}, Person{name='Wang', gender='male', age=17}, Person{name='Li', gender='female', age=19}, Person{name='Sun', gender='male', age=22}]
Process finished with exit code 0
【参考】
- https://www.geeksforgeeks.org/filter-pattern-in-java/
- https://www.linkedin.com/pulse/think-functional-rethinking-criteria-pattern-lambdas-sujit-kamthe/
- https://www.runoob.com/design-pattern/filter-pattern.html
设计模式系列博文导航
一、创建型 - 5种
原型模式(Prototype Pattern)
抽象工厂模式(Abstract Factory Pattern)
建造者模式(Builder Pattern)
工厂模式(Factory Pattern)
单例模式(Singleton Pattern)
助记语:原抽建工单
二、结构型 - 8种
享元模式(Flyweight Pattern)
代理模式(Proxy Pattern)
适配器模式(Adapter Pattern)
外观模式(Facade Pattern)
过滤器模式(Filter/Criteria Pattern)
桥接模式(Bridge Pattern)
组合模式(Composite Pattern)
装饰器模式(Decorator Pattern)
助记语:想呆室外,过桥组装
三、行为型 - 11种
责任链模式(Chain of Responsibility Pattern)
命令模式(Command Pattern)
解释器模式(Interpreter Pattern)
中介者模式(Mediator Pattern)
迭代器模式(Iterator Pattern)
观察者模式(Observer Pattern)
策略模式(Strategy Pattern)
状态模式(State Pattern)
备忘录模式(Memento Pattern)
模板方法模式(Template Pattern)
访问者模式(Visitor Pattern)
助记语:责令解中谍,观测状被模仿