结构型模式- 过滤器模式(过滤对象)

过滤器模式Filter pattern,标准模式Criteria Pattern

意图

允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们链接起来。

示例

创建一个Person对象,Criteria接口和具体类,实现该接口以过滤Person对象列表。CriteriaPatternDemo是一个演示类,使用Criteria对象基于各种标准及其组合过滤拥有Person对象的列表(List)。

UML类图

实现代码

  1. 创建Person,待过滤对象
// 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;
      }
}
  1. 创建过滤接口Criteria
// Criteria.java
/**
* Filter person list using Criteria
*/
public interface Criteria {
      public List<Person> meetCriteria(List<Person> persons);
}
  1. 创建实现接口Criteria的实体类FemaleCriteria, MaleCriteria, SingleCriteria, AndCriteria, OrCriteria,用于过滤Person列表,得到符合要求的Person 列表
// FemaleCriteria.java
public class FemaleCriteria 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;
      }
}

// MaleCriteria.java
public class MaleCriteria 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;
      }
}
// SingleCriteria.java
public class SingleCriteria 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;
      }
}

// AndCriteria.java
/**
* Filter Person list which meet criteria and otherCriteria
*/
public class AndCriteria implements Criteria {
      protected Criteria criteria;
      protected 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
/**
* filter Person list which meet criteria or otherCriteria
*/
public class OrCriteria implements Criteria {
      protected Criteria criteria;
      protected Criteria otherCriteria;
      
      public OrCriteria(Criteria criteria, Criteria otherCriteria) {
            this.criteria = criteria;
            this.otherCriteria = otherCriteria;
      }
      
      @Override
      public List<Person> meetCriteria(List<Person> persons) {
            List<Person> firstCriteriaPersons =  criteria.meetCriteria(persons);
            List<Person> otherCriterialPersons =  criteria.meetCriteria(persons);
            
            for(Person person: otherCriterialPersons)
            {
                  if(!firstCriteriaPersons.contains(person))
                  {
                        firstCriteriaPersons.add(person);
                  }
            }
            
            return firstCriteriaPersons;
      }
}
  1. 创建FilterPatternDemo类使用过滤模式,新建Person列表对象和具体的过滤对象,用过滤模式过滤Person列表得到期望的Person列表
// FilterPatternDemo.java
public class FilterPatternDemo {
      public static void main(String[] args) {
            List<Person> persons = new ArrayList<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(new Person("Mike", "Male", "Single"));
            persons.add(new Person("Bobby", "Male", "Single"));
            
            Criteria male = new MaleCriteria();
            Criteria female = new FemaleCriteria();
            Criteria single = new SingleCriteria();
            Criteria singleMale = new AndCriteria(single, male);
            Criteria singleOrFemal = new OrCriteria(single, female);
            
            System.out.println("Males:");
            printPersons(male.meetCriteria(persons));
            System.out.println("Females:");
            printPersons(female.meetCriteria(persons));
            System.out.println("Single Males:");
            printPersons(singleMale.meetCriteria(persons));
            System.out.println("Single or Females:");
            printPersons(singleOrFemal.meetCriteria(persons));
      }
      
      static void printPersons(List<Person> persons) {
            for(Person person: persons) {
                  System.out.println("Person: [ Name: " +  person.getName()
                  + ", Gender:" + person.getGender()
                  + ", Marital Status: "
                  + person.getMaritalStatus() + "]");
            }
      }
}

运行结果

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]
posted @ 2021-01-12 00:02  明明1109  阅读(215)  评论(0编辑  收藏  举报