Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Writing in the front

Design pattern, in fact, to a certain extent, represents the optimal solution, the best practice, the solution summarized by some developers based on experience, and the optimal solution for different scenarios.

In the current environment, design patterns are also highly respected by developers, and use them to solve many problems, so it is imperative to learn design patterns.

Let’s take a look at filter mode.

The filter pattern is also one of structural design patterns. Its core principle is to filter a group of objects by using different rules and standards, and to associate them together through certain logic.

Usage scenarios

Filter mode usage scenarios are really difficult to summarize, if you have a good scenario to discuss ha, not here.

Code implementation

First, let’s create a class on which we will apply our rules.

public class Person {

    private String name;
    private String gender;
    private String maritalStatus;

    public Person(String name,String gender,String maritalStatus){
        this.name = name;
        this.gender = gender;
        this.maritalStatus = maritalStatus;
    }

    public String getName(a) {
        return name;
    }
    public String getGender(a) {
        return gender;
    }
    public String getMaritalStatus(a) {
        returnmaritalStatus; }}Copy the code

Create standard rule interfaces.

public interface Criteria {
    public List<Person> meetCriteria(List<Person> persons);
}
Copy the code

Create the interface implementation class for the standard rule as follows:

public class CriteriaMale implements Criteria {

    @Override
    public List<Person> meetCriteria(List<Person> persons) {
        List<Person> malePersons = new ArrayList<Person>();
        for (Person person : persons) {
            if(person.getGender().equalsIgnoreCase("MALE")){ malePersons.add(person); }}returnmalePersons; }}Copy the code
public class CriteriaFemale implements Criteria {

    @Override
    public List<Person> meetCriteria(List<Person> persons) {
        List<Person> femalePersons = new ArrayList<Person>();
        for (Person person : persons) {
            if(person.getGender().equalsIgnoreCase("FEMALE")){ femalePersons.add(person); }}returnfemalePersons; }}Copy the code
public class CriteriaSingle implements Criteria {

    @Override
    public List<Person> meetCriteria(List<Person> persons) {
        List<Person> singlePersons = new ArrayList<Person>();
        for (Person person : persons) {
            if(person.getMaritalStatus().equalsIgnoreCase("SINGLE")){ singlePersons.add(person); }}returnsinglePersons; }}Copy the code
public class AndCriteria implements Criteria {

    private Criteria criteria;
    private Criteria otherCriteria;

    public AndCriteria(Criteria criteria, Criteria otherCriteria) {
        this.criteria = criteria;
        this.otherCriteria = otherCriteria;
    }

    @Override
    public List<Person> meetCriteria(List<Person> persons) {
        List<Person> firstCriteriaPersons = criteria.meetCriteria(persons);
        returnotherCriteria.meetCriteria(firstCriteriaPersons); }}Copy the code
public class OrCriteria implements Criteria {

    private Criteria criteria;
    private Criteria otherCriteria;

    public OrCriteria(Criteria criteria, Criteria otherCriteria) {
        this.criteria = criteria;
        this.otherCriteria = otherCriteria;
    }

    @Override
    public List<Person> meetCriteria(List<Person> persons) {
        List<Person> firstCriteriaItems = criteria.meetCriteria(persons);
        List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persons);

        for (Person person : otherCriteriaItems) {
            if(!firstCriteriaItems.contains(person)){
                firstCriteriaItems.add(person);
            }
        }
        returnfirstCriteriaItems; }}Copy the code

Finally we filter the initial objects through different standard rules.

public class CriteriaPatternDemo {
    public static void main(String[] args) {
        List<Person> persons = new ArrayList<Person>();

        persons.add(new Person("Robert"."Male"."Single"));
        persons.add(new Person("John"."Male"."Married"));
        persons.add(new Person("Laura"."Female"."Married"));
        persons.add(new Person("Diana"."Female"."Single"));
        persons.add(new Person("Mike"."Male"."Single"));
        persons.add(new Person("Bobby"."Male"."Single"));

        Criteria male = new CriteriaMale();
        Criteria female = new CriteriaFemale();
        Criteria single = new CriteriaSingle();
        Criteria singleMale = new AndCriteria(single, male);
        Criteria singleOrFemale = new OrCriteria(single, female);

        System.out.println("Males: ");
        printPersons(male.meetCriteria(persons));

        System.out.println("\nFemales: ");
        printPersons(female.meetCriteria(persons));

        System.out.println("\nSingle Males: ");
        printPersons(singleMale.meetCriteria(persons));

        System.out.println("\nSingle Or Females: ");
        printPersons(singleOrFemale.meetCriteria(persons));
    }

    public static void printPersons(List<Person> persons){
        for (Person person : persons) {
            System.out.println("Person : [ Name : " + person.getName()
                    +", Gender : " + person.getGender()
                    +", Marital Status : " + person.getMaritalStatus()
                    +"]"); }}}Copy the code