Design Patterns Filter Pattern

Filter mode

Filter Pattern or Criteria Pattern is a design pattern that allows developers to filter a set of objects using different criteria and connect them by decoupling through logical operations. stand up. This type of design pattern is a structured model that combines multiple standards to achieve a single standard.

Implementation

We will create an Person object, an Criteria interface, and an entity class that implements the interface to filter the list of Person objects. CriteriaPatternDemo, our demo class uses the Criteria object to filter the list of Person objects based on various criteria and their combination.

filter pattern uml diagram

Step 1

Create a class and apply the criteria on that class.

Person.java

public class Person { private String name; private String gender; private String maritalStatus; public Person(String name,String gender,String maritalStatus) { this.name< Span class="hl-code"> = name; this.gender< Span class="hl-code"> = gender; this.maritalStatus< Span class="hl-code"> = maritalStatus; } public String getName() { return name; } public String getGender() { return gender; } public String getMaritalStatus() { return maritalStatus; } }

Step 2

Create an interface for the standard (Criteria).

Criteria.java

import java .util.< Span class="hl-identifier">List; public interface Criteria { public List<Person> meetCriteria(List <Person> persons); }

Step 3

Create an entity class that implements the Criteria interface.

CriteriaMale.java

import java .util.< Span class="hl-identifier">ArrayList; import java.util.List; public class CriteriaMale implements Criteria < Span class="hl-brackets">{ @Override public List<Person> meetCriteria(List <Person> persons) { List<Person > malePersons = new ArrayList <Person>(); for (Person person : persons) { if(person< Span class="hl-code">.getGender().equalsIgnoreCase( "MALE"< Span class="hl-brackets">)){ malePersons.add< Span class="hl-brackets">(person); } } return malePersons; } }

CriteriaFemale.java

import java .util.< Span class="hl-identifier">ArrayList; import java.util.List; public class CriteriaFemale

CriteriaSingle.java

import java .util.< Span class="hl-identifier">ArrayList; import java.util.List; public class CriteriaSingle implements Criteria < Span class="hl-brackets">{ @Override public List<Person> meetCriteria(List <Person> persons) { List<Person > singlePersons = new ArrayList <Person>(); for (Person person : persons) { if(person< Span class="hl-code">.getMaritalStatus().equalsIgnoreCase( "SINGLE"< Span class="hl-brackets">)){ singlePersons.add< Span class="hl-brackets">(person); } } return singlePersons; } }

OrCriteria.java

import java .util.< Span class="hl-identifier">List; public class OrCriteria implements Criteria < Span class="hl-brackets">{ private Criteria criteria; private Criteria otherCriteria; public OrCriteria(Criteria criteria, Criteria otherCriteria) { this.criteria< Span class="hl-code"> = criteria; this.otherCriteria< Span class="hl-code"> = 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(!< Span class="hl-identifier">firstCriteriaItems.contains(person)) { firstCriteriaItems.

Step 4

Filter a list of Person objects using different criteria (Criteria) and their combination.

CriteriaPatternDemo.java

import java .util.< Span class="hl-identifier">ArrayList; import java.util.List; public class CriteriaPatternDemo { public static void main(String[] args) { List<Person > persons = new ArrayList <Person>(); persons.add< Span class="hl-brackets">(new Person("Robert< /span>","Male", "Single")< /span>); persons.add< Span class="hl-brackets">(new Person("John< /span>","Male", "Married")< /span>); persons.add< Span class="hl-brackets">(new Person("Laura< /span>","Female", "Married")< /span>); persons.add< Span class="hl-brackets">(new Person("Diana< /span>","Female",

Step 5

Executing the program, outputting results:

Males: 
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 ]

Females: 
Person : [ Name : Laura, Gender : Female, Marital Status : Married ]
Person : [ Name : Diana, Gender : Female, Marital Status : Single ]

Single Males: 
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 Or Females: 
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 ]





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition