Java filter pattern
introduce
What is filtering mode?
For example, in cement manufacturing, sand is used to mix cement. If there are too many large stones in the sand, the mixed cement will not crack easily. Therefore, before mixing water and mud, we need to use a filter screen to filter out the large stones in the sand, and the filter we learn today is like this filter screen for filtering sand
For example, if there are 10000 information about women's blind dates, then a man comes for a blind date. He said that I need the information of women with xxx conditions, then we can filter out the unnecessary information and display it to the man's qualified women's information through filtering
case
The following case is mainly about filtering (gender, single). For other filtering, please refer to this case
**Person * * entity class
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() { return name; } public String getGender() { return gender; } public String getMaritalStatus() { return maritalStatus; } }
filtraCriteria filter interface
//Filter interface public interface filtraCriteria { //Incoming content to be filtered public List<Person> meetCriteria(List<Person> persons); }
The implementation class CriteriaFemale that implements the filter interface
//Filter class public class CriteriaFemale implements filtraCriteria { //Female sex @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> femalePersons = new ArrayList<Person>(); persons.stream() .filter(gender -> gender.getGender().equalsIgnoreCase("female")) .forEach(femalePersons::add); return femalePersons; } }
The implementation class CriteriaMale that implements the filter interface
//Filter class public class CriteriaMale implements filtraCriteria { //Male sex @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> malePersons = new ArrayList<Person>(); persons.stream() .filter(gender->gender.getGender().equalsIgnoreCase("male")) .forEach(malePersons::add); return malePersons; } }
The implementation class CriteriaSingle that implements the filter interface
//Filter class public class CriteriaSingle implements filtraCriteria { //I'm single @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> singlePersons = new ArrayList<Person>(); persons.stream() .filter(maritalStatus->maritalStatus.getMaritalStatus().equalsIgnoreCase("single")) .forEach(singlePersons::add); return singlePersons; } }
AndCriteria, a composite implementation class that implements the filter interface
//Combine filter classes and public class AndCriteria implements filtraCriteria { private filtraCriteria criteria; private filtraCriteria otherCriteria; public AndCriteria(filtraCriteria criteria, filtraCriteria otherCriteria) { this.criteria = criteria; this.otherCriteria = otherCriteria; } @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> firstCriteriaPersons = criteria.meetCriteria(persons); List<Person> people = otherCriteria.meetCriteria(firstCriteriaPersons); return people; } }
We have implemented the combination of two filter classes above. Of course, three or four are OK. It depends on your own design
OrCriteri, a composite implementation class that implements the filter interface
//Combination filter class or public class OrCriteria implements filtraCriteria { private filtraCriteria criteria; private filtraCriteria otherCriteria; public OrCriteria(filtraCriteria criteria, filtraCriteria otherCriteria) { this.criteria = criteria; this.otherCriteria = otherCriteria; } @Override public List<Person> meetCriteria(List<Person> persons) { List<Person> firstORother=new ArrayList<>(); List<Person> firstCriteriaItems = criteria.meetCriteria(persons); firstCriteriaItems.stream().forEach(firstORother::add); List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persons); otherCriteriaItems.stream().forEach(firstORother::add); return firstORother; } }
Test CriteriaPatternDemo
public class CriteriaPatternDemo { //Traverse the information in the collection 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() +" ]"); } } public static void main(String[] args) { //-------------------Information to be filtered---------------------------------------- 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")); // ------------------Create filter condition class-------------------- filtraCriteria male = new CriteriaMale(); //Release gender male filtraCriteria female = new CriteriaFemale(); //Release gender female filtraCriteria single = new CriteriaSingle(); //Release detail //--------------------Create composite filter class----------------------------- filtraCriteria singleMale = new AndCriteria(single, male); //Single and male filtraCriteria andCriteria=new AndCriteria(single, female);//Single and female filtraCriteria singleOrFemale = new OrCriteria(single, female); //Single or female //-------------------Print filtered information------------------- System.out.println("male: "); printPersons(male.meetCriteria(persons)); System.out.println("\n Female: "); printPersons(female.meetCriteria(persons)); System.out.println("\n Single man: "); printPersons(singleMale.meetCriteria(persons)); System.out.println("\n Single girl "); printPersons(andCriteria.meetCriteria(persons)); System.out.println("\n Single or female: "); printPersons(singleOrFemale.meetCriteria(persons)); } }
male:
Person: [Name: Robert, gender: male, Marital Status: single]
Person: [Name: John, gender: male, Marital Status: married]
Person: [Name: Mike, gender: male, Marital Status: single]
Person: [Name: Bobby, gender: male, Marital Status: single]
Female:
Person: [Name: Laura, gender: female, Marital Status: married]
Person: [Name: Diana, gender: female, Marital Status: single]
Single men:
Person: [Name: Robert, gender: male, Marital Status: single]
Person: [Name: Mike, gender: male, Marital Status: single]
Person: [Name: Bobby, gender: male, Marital Status: single]
Single girl
Person: [Name: Diana, gender: female, Marital Status: single]
Single or female:
Person: [Name: Robert, gender: male, Marital Status: single]
Person: [Name: Diana, gender: female, Marital Status: single]
Person: [Name: Mike, gender: male, Marital Status: single]
Person: [Name: Bobby, gender: male, Marital Status: single]
Person: [Name: Laura, gender: female, Marital Status: married]
Person: [Name: Diana, gender: female, Marital Status: single]