1,背景

过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。

二,实现

被过滤的对象

/**
 * 被过滤的对象
 */
public class Person {

    /**
     * 姓名
     */
    private String name;

    /**
     * 性别 MALE FEMALE
     */
    private String gender;

    /**
     * 婚姻状况: SINGLE MARRIED
     */
    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;
    }
}

过滤器接口

import java.util.List;

/**
 * 过滤器接口
 */
public interface IFilter {

    List<Person> filter(List<Person> persons);
}

过滤器

/**
 * 男生过滤器
 */
public class MaleFilter implements IFilter {

    @Override
    public List<Person> filter(List<Person> persons) {
        return persons.stream().filter(person -> person.getGender().equals("MALE")).collect(Collectors.toList());
    }

}

/**
 * 女生过滤器
 */
public class MarriedFilter implements IFilter {

    @Override
    public List<Person> filter(List<Person> persons) {
        return persons.stream().filter(person -> person.getGender().equals("MARRIED")).collect(Collectors.toList());
    }

}

/**
 * 已婚过滤器
 *
 * @author xiaowen
 */
public class MarryFilter implements IFilter {

    @Override
    public List<Person> filter(List<Person> persons) {
        return persons.stream().filter(person -> person.getMarital().equals("MARRIED")).collect(Collectors.toList());
    }

}

/**
 * 未婚过滤器
 *
 * @author xiaowen
 */
public class SingleFilter implements IFilter {

    @Override
    public List<Person> filter(List<Person> persons) {
        return persons.stream().filter(person -> person.getMarital().equals("SINGLE")).collect(Collectors.toList());
    }
}

处理并且逻辑的组合条件过滤器

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 处理并且逻辑的组合条件过滤器
 */
public class FilterAnd implements IFilter {

    private List<IFilter> filters;

    public FilterAnd(IFilter... filter) {
        filters = Arrays.asList(filter);
    }

    @Override
    public List<Person> filter(List<Person> persons) {
        List<Person> filter = new ArrayList<>(persons);
        for (IFilter x : filters) {
            filter = x.filter(filter);
        }
        return filter;
    }

}

测试

public class TestMain {

    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);
        maleList.forEach(male -> {
            System.out.println(male.getName() + "-" + male.getGender() + "-" + male.getMarital());
        });
        System.out.println("---------------获取所有单身------------------");
        List<Person> singleList = new SingleFilter().filter(persons);
        singleList.forEach(single -> {
            System.out.println(single.getName() + "-" + single.getGender() + "-" + single.getMarital());
        });
        System.out.println("--------------------所有单身的男性-------------------");
        List<Person> singleAndMale = new FilterAnd(new MaleFilter(), new SingleFilter()).filter(persons);
        singleAndMale.forEach(result -> {
            System.out.println(result.getName() + "-" + result.getGender() + "-" + result.getMarital());
        });
        System.out.println("--------------------所有已婚的男性-------------------");
        List<Person> marryList = new FilterAnd(new MaleFilter(), new MarryFilter()).filter(persons);
        marryList.forEach(marrayAndMale -> {
            System.out.println(marrayAndMale.getName() + "-" + marrayAndMale.getGender() + "-" + marrayAndMale.getMarital());
        });
    }
}

---------------获取所有的男性信息------------------
邓二-MALE-MARRIED
张三-MALE-SINGLE
王五-MALE-SINGLE
孙七-MALE-SINGLE
罗八-MALE-MARRIED
---------------获取所有单身------------------
张三-MALE-SINGLE
王五-MALE-SINGLE
赵六-FEMALE-SINGLE
孙七-MALE-SINGLE
刘九-FEMALE-SINGLE
史十-FEMALE-SINGLE
--------------------所有单身的男性-------------------
张三-MALE-SINGLE
王五-MALE-SINGLE
孙七-MALE-SINGLE
--------------------所有已婚的男性-------------------
邓二-MALE-MARRIED
罗八-MALE-MARRIED