Java Filter mode (Filter/criteria pattern)
Filter pattern or Standard mode (criteria pattern) is a design pattern that allows developers to filter a set of objects using different criteria and connect them in a decoupled way by logical operation. This type of design pattern is a structured pattern that combines multiple criteria to achieve a single standard.
Filter pattern or Standard mode (criteria pattern) is a design pattern that allows developers to filter a set of objects using different criteria and connect them in a decoupled way by logical operation. This type of design pattern is a structured pattern that combines multiple criteria to achieve a single standard.
Realize
We will create a person object, a 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.
Step 1
Create a class that applies a standard 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 = name;
This.gender = gender;
This.maritalstatus = MaritalStatus;
}
Public String GetName () {return
name;
}
Public String Getgender () {return
gender;
}
Public String Getmaritalstatus () {return
maritalstatus
}}
Step 2
Create an interface for the criteria (criteria).
Criteria.java
Import java.util.List;
Public interface The Criteria {public
list<person> Meetcriteria (list<person> persons);
}
Step 3
Creates an entity class that implements the Criteria interface.
Criteriamale.java
Import java.util.ArrayList;
Import java.util.List;
public class Criteriamale implements the 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);
}
}
Return malepersons
}
}
Criteriafemale.java
Import java.util.ArrayList;
Import java.util.List;
public class Criteriafemale implements the 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);
}
Return femalepersons
}
}
Criteriasingle.java
Import java.util.ArrayList;
Import java.util.List;
public class Criteriasingle implements the 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);
}
Return singlepersons
}
}
Andcriteria.java
Import java.util.List;
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);
Return Othercriteria.meetcriteria (firstcriteriapersons);
}
Orcriteria.java
Import java.util.List;
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
) { Firstcriteriaitems.add (person);
}
Return Firstcriteriaitems
}
}
Step 4
Filter the list of person objects using different criteria (criteria) and their combination.
Criteriapatterndemo.java
public class Criteriapatterndemo {public static void main (string[] args) {list<person> persons = new Arrayli
St<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 ("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.prin TLN ("Person: [Name:" + person.getname () + ", Gender:" + person.getgender () + ", Marital Status:" + Perso
N.getmaritalstatus () + "]");
}
}
}
Step 5
Verify the output.
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:mal E, marital status:single] Females:person: [Name:laura, Gender:female, marital status:married] Person: [Na Me:diana, Gender:female, marital status:single] single males:person: [Name:robert, Gender:male, Marital St Atus:single] Person: [Name:mike, Gender:male, marital status:single] Person: [Name:bobby, Gender:male, M Arital 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]
Thank you for reading, I hope to help you, thank you for your support for this site!