1. 首页
  2. 设计模式2

12-十二、过滤器模式 ( Filter Pattern )

过滤器模式(Filter Pattern)或允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来

过滤器模式(Filter Pattern) 又称 标准模式(Criteria Pattern

过滤器模式属于结构型模式,它结合多个标准来获得单一标准

实现

img_1.png

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

范例

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

Person.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


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)创建一个接口

Criteria.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


import java.util.List;

public interface Criteria {
   public List<Person> meetCriteria(List<Person> persons);
}

3. 创建实现了 Criteria 接口的实体类

CriteriaMale.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


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;
   }
}

CriteriaFemale.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


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;
   }
}

CriteriaSingle.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


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;
   }
}

AndCriteria.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


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

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


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;
   }
}

4. 使用不同的标准(Criteria)和它们的结合来过滤 Person 对象的列表

CriteriaPatternDemo.java

// author: 研发军团(www.ycbbs.vip)
// Copyright © 2015-2065 www.ycbbs.vip. All rights reserved.

package com.ycbbs.tech.gof;


import java.util.ArrayList; 
import java.util.List;

public class CriteriaPatternDemo {
   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 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.println("Person : [ Name : " + person.getName() 
            +", Gender : " + person.getGender() 
            +", Marital Status : " + person.getMaritalStatus()
            +" ]");
      }
   }      
}

编译运行以上 Java 范例,输出结果如下

$ javac -d . src/main/com/ycbbs/tech/gof/CriteriaPatternDemo.java
$ java  com.ycbbs.tech.gof.CriteriaPatternDemo
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 ]

希望读者能够给小编留言,也可以点击[此处扫下面二维码关注微信公众号](https://www.ycbbs.vip/?p=28 "此处扫下面二维码关注微信公众号")

看完两件小事

如果你觉得这篇文章对你挺有启发,我想请你帮我两个小忙:

  1. 关注我们的 GitHub 博客,让我们成为长期关系
  2. 把这篇文章分享给你的朋友 / 交流群,让更多的人看到,一起进步,一起成长!
  3. 关注公众号 「方志朋」,公众号后台回复「666」 免费领取我精心整理的进阶资源教程
  4. JS中文网,Javascriptc中文网是中国领先的新一代开发者社区和专业的技术媒体,一个帮助开发者成长的社区,是给开发者用的 Hacker News,技术文章由为你筛选出最优质的干货,其中包括:Android、iOS、前端、后端等方面的内容。目前已经覆盖和服务了超过 300 万开发者,你每天都可以在这里找到技术世界的头条内容。

    本文著作权归作者所有,如若转载,请注明出处

    转载请注明:文章转载自「 Java极客技术学习 」https://www.javajike.com

    标题:12-十二、过滤器模式 ( Filter Pattern )

    链接:https://www.javajike.com/article/2372.html

« 13-十三、组合模式 ( Composite Pattern )
11-十一、桥接模式 ( Bridge Pattern )»

相关推荐

QR code