千家信息网

Java过滤器模式怎么实现

发表于:2025-01-18 作者:千家信息网编辑
千家信息网最后更新 2025年01月18日,本篇内容主要讲解"Java过滤器模式怎么实现",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java过滤器模式怎么实现"吧!过滤器模式(Filter Pat
千家信息网最后更新 2025年01月18日Java过滤器模式怎么实现

本篇内容主要讲解"Java过滤器模式怎么实现",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java过滤器模式怎么实现"吧!

过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。

实现

我们将创建一个 Person 对象、Criteria 接口和实现了该接口的实体类,来过滤 Person 对象的列表。CriteriaPatternDemo,我们的演示类使用 Criteria 对象,基于各种标准和它们的结合来过滤 Person 对象的列表。

步骤 1

创建一个类,在该类上应用标准。

Person.javapublic 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)创建一个接口。

Criteria.javaimport java.util.List;public interface Criteria {  public List meetCriteria(List persons);}

步骤 3

创建实现了 Criteria 接口的实体类。

CriteriaMale.javaimport 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;  }}CriteriaFemale.javaimport 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;  }}CriteriaSingle.javaimport 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;  }}AndCriteria.javaimport 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);  }}OrCriteria.javaimport 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)和它们的结合来过滤 Person 对象的列表。

CriteriaPatternDemo.javaimport java.util.ArrayList;import java.util.List;public class CriteriaPatternDemo {  public static void main(String[] args) {     List persons = new ArrayList();     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);     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 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 ]

到此,相信大家对"Java过滤器模式怎么实现"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0