El patrón de filtro o patrón de criterios es un patrón de diseño que permite a los desarrolladores filtrar un conjunto de objetos utilizando diferentes criterios y encadenándolos de forma desacoplada a través de operaciones lógicas.
Procedimiento:
- Cree una clase en la que se aplicarán los Criterios.
- Cree una interfaz para Criterios.
- Crear clases concretas implementando la interfaz Criteria.
- Utilice diferentes criterios y su combinación para filtrar personas.
- Verificar la salida
Implementación:
Vamos a crear un objeto Person, una interfaz Criteria y clases concretas que implementen esta interfaz para filtrar la lista de objetos Person. CriteriaPatternDemo, nuestra clase de demostración utiliza objetos de criterios para filtrar objetos de lista de personas en función de varios criterios y sus combinaciones.
Paso 1: Cree una clase en la que se aplicarán los Criterios.
- Persona.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; } }
Paso 2: Cree una interfaz para Criteria.
- Criterios.java
import java.util.List; public interface Criteria { public List<Person> meetCriteria(List<Person> persons); }
Paso 3: Crear clases concretas implementando la interfaz Criteria.
- CriteriosMasculino.java
import java.util.ArrayList; import java.util.List; 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); } } return malePersons; } }
- CriteriosMujer.java
import java.util.ArrayList; import java.util.List; 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); } } return femalePersons; } }
- CriteriosSingle.java
import java.util.ArrayList; import java.util.List; 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); } } return singlePersons; } }
- YCriterios.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(person)){ firstCriteriaItems.add(person); } } return firstCriteriaItems; } }
Paso 4: Use diferentes Criterios y su combinación para filtrar personas.
- CriteriaPatternDemo.java
Java
// Importing ArrayList and List class // from java.util package import java.util.ArrayList; import java.util.List; // Class // Main class public class CriteriaPatternDemo { // Main driver method public static void main(String[] args) { // Creating a onbject of List in main() method // Declaring object of user-defined datatype 'Person' List<Person> persons = new ArrayList<Person>(); // Adding elements to the object created above // using the add() method of List // Custom entries 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); // Display message System.out.println("Males: "); printPersons(male.meetCriteria(persons)); // Display message System.out.println("\nFemales: "); printPersons(female.meetCriteria(persons)); // Display message System.out.println("\nSingle Males: "); printPersons(singleMale.meetCriteria(persons)); // Display message 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() + " ]"); } } }
Paso 5: Verifique la salida.
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 ]