📜  Java的过滤器模式

📅  最后修改于: 2022-05-13 01:54:20.280000             🧑  作者: Mango

Java的过滤器模式

过滤模式或标准模式是一种设计模式,它使开发人员能够使用不同的标准过滤一组对象,并通过逻辑操作以解耦的方式将它们链接起来。

程序:

  1. 创建一个要应用标准的类。
  2. 为 Criteria 创建一个接口。
  3. 创建实现 Criteria 接口的具体类。
  4. 使用不同的标准及其组合来过滤人员。
  5. 验证输出

执行 :

我们将创建一个 Person 对象、Criteria 接口和实现这个接口的具体类来过滤 Person 对象列表。 CriteriaPatternDemo,我们的演示类使用 Criteria 对象根据各种条件及其组合过滤 Person 对象列表。



步骤 1:创建一个要应用标准的类。

  • 人。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;
   }    
}

第 2 步:为 Criteria 创建一个接口。

  • 标准。Java
import java.util.List;

public interface Criteria {
   public List meetCriteria(List persons);
}

第 3 步:创建实现 Criteria 接口的具体类。

  • 标准男。Java
import java.util.ArrayList;
import java.util.List;

public class CriteriaMale implements Criteria {

   @Override
   public List meetCriteria(List persons) {
      List malePersons = new ArrayList(); 
      
      for (Person person : persons) {
         if(person.getGender().equalsIgnoreCase("MALE")){
            malePersons.add(person);
         }
      }
      return malePersons;
   }
}
  • 标准女性。Java
import java.util.ArrayList;
import java.util.List;

public class CriteriaFemale implements Criteria {

   @Override
   public List meetCriteria(List persons) {
      List femalePersons = new ArrayList(); 
      
      for (Person person : persons) {
         if(person.getGender().equalsIgnoreCase("FEMALE")){
            femalePersons.add(person);
         }
      }
      return femalePersons;
   }
}
  • 标准单。Java
import java.util.ArrayList;
import java.util.List;

public class CriteriaSingle implements Criteria {

   @Override
   public List meetCriteria(List persons) {
      List singlePersons = new ArrayList(); 
      
      for (Person person : persons) {
         if(person.getMaritalStatus().equalsIgnoreCase("SINGLE")){
            singlePersons.add(person);
         }
      }
      return singlePersons;
   }
}
  • 和标准。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 meetCriteria(List persons) {
   
      List firstCriteriaPersons = criteria.meetCriteria(persons);        
      return otherCriteria.meetCriteria(firstCriteriaPersons);
   }
}
  • 或标准。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 meetCriteria(List persons) {
      List firstCriteriaItems = criteria.meetCriteria(persons);
      List otherCriteriaItems = otherCriteria.meetCriteria(persons);

      for (Person person : otherCriteriaItems) {
         if(!firstCriteriaItems.contains(person)){
            firstCriteriaItems.add(person);
         }
      }    
      return firstCriteriaItems;
   }
}

第 4 步:使用不同的 Criteria 及其组合来过滤人员。

  • 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 persons = new ArrayList();
        
     // 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 persons){
     
      for (Person person : persons) {
         System.out.println("Person : [ Name : " + person.getName() + ", Gender : " + person.getGender() + ", Marital Status : " + person.getMaritalStatus() + " ]");
      }
   }      
}


步骤 5:验证输出。

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 ]