当前位置:   article > 正文

设计模式 过滤器模式

设计模式 过滤器模式

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

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

实现

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

                     

步骤 1

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

Person.java

  1. public class Person {
  2. private String name;
  3. private String gender;
  4. private String maritalStatus;
  5. public Person(String name,String gender,String maritalStatus){
  6. this.name = name;
  7. this.gender = gender;
  8. this.maritalStatus = maritalStatus;
  9. }
  10. public String getName() {
  11. return name;
  12. }
  13. public String getGender() {
  14. return gender;
  15. }
  16. public String getMaritalStatus() {
  17. return maritalStatus;
  18. }
  19. }

步骤 2

为标准(Criteria)创建一个接口。

Criteria.java

  1. import java.util.List;
  2. public interface Criteria {
  3. public List<Person> meetCriteria(List<Person> persons);
  4. }

步骤 3

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

CriteriaMale.java

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class CriteriaMale implements Criteria {
  4. @Override
  5. public List<Person> meetCriteria(List<Person> persons) {
  6. List<Person> malePersons = new ArrayList<Person>();
  7. for (Person person : persons) {
  8. if(person.getGender().equalsIgnoreCase("MALE")){
  9. malePersons.add(person);
  10. }
  11. }
  12. return malePersons;
  13. }
  14. }

CriteriaFemale.java

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class CriteriaFemale implements Criteria {
  4. @Override
  5. public List<Person> meetCriteria(List<Person> persons) {
  6. List<Person> femalePersons = new ArrayList<Person>();
  7. for (Person person : persons) {
  8. if(person.getGender().equalsIgnoreCase("FEMALE")){
  9. femalePersons.add(person);
  10. }
  11. }
  12. return femalePersons;
  13. }
  14. }

 CriteriaSingle.java

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class CriteriaSingle implements Criteria {
  4. @Override
  5. public List<Person> meetCriteria(List<Person> persons) {
  6. List<Person> singlePersons = new ArrayList<Person>();
  7. for (Person person : persons) {
  8. if(person.getMaritalStatus().equalsIgnoreCase("SINGLE")){
  9. singlePersons.add(person);
  10. }
  11. }
  12. return singlePersons;
  13. }
  14. }

 AndCriteria.java

  1. import java.util.List;
  2. public class AndCriteria implements Criteria {
  3. private Criteria criteria;
  4. private Criteria otherCriteria;
  5. public AndCriteria(Criteria criteria, Criteria otherCriteria) {
  6. this.criteria = criteria;
  7. this.otherCriteria = otherCriteria;
  8. }
  9. @Override
  10. public List<Person> meetCriteria(List<Person> persons) {
  11. List<Person> firstCriteriaPersons = criteria.meetCriteria(persons);
  12. return otherCriteria.meetCriteria(firstCriteriaPersons);
  13. }
  14. }

OrCriteria.java

  1. import java.util.List;
  2. public class OrCriteria implements Criteria {
  3. private Criteria criteria;
  4. private Criteria otherCriteria;
  5. public OrCriteria(Criteria criteria, Criteria otherCriteria) {
  6. this.criteria = criteria;
  7. this.otherCriteria = otherCriteria;
  8. }
  9. @Override
  10. public List<Person> meetCriteria(List<Person> persons) {
  11. List<Person> firstCriteriaItems = criteria.meetCriteria(persons);
  12. List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persons);
  13. for (Person person : otherCriteriaItems) {
  14. if(!firstCriteriaItems.contains(person)){
  15. firstCriteriaItems.add(person);
  16. }
  17. }
  18. return firstCriteriaItems;
  19. }
  20. }

步骤4

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

CriteriaPatternDemo.java

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class CriteriaPatternDemo {
  4. public static void main(String[] args) {
  5. List<Person> persons = new ArrayList<Person>();
  6. persons.add(new Person("Robert","Male", "Single"));
  7. persons.add(new Person("John","Male", "Married"));
  8. persons.add(new Person("Laura","Female", "Married"));
  9. persons.add(new Person("Diana","Female", "Single"));
  10. persons.add(new Person("Mike","Male", "Single"));
  11. persons.add(new Person("Bobby","Male", "Single"));
  12. Criteria male = new CriteriaMale();
  13. Criteria female = new CriteriaFemale();
  14. Criteria single = new CriteriaSingle();
  15. Criteria singleMale = new AndCriteria(single, male);
  16. Criteria singleOrFemale = new OrCriteria(single, female);
  17. System.out.println("Males: ");
  18. printPersons(male.meetCriteria(persons));
  19. System.out.println("\nFemales: ");
  20. printPersons(female.meetCriteria(persons));
  21. System.out.println("\nSingle Males: ");
  22. printPersons(singleMale.meetCriteria(persons));
  23. System.out.println("\nSingle Or Females: ");
  24. printPersons(singleOrFemale.meetCriteria(persons));
  25. }
  26. public static void printPersons(List<Person> persons){
  27. for (Person person : persons) {
  28. System.out.println("Person : [ Name : " + person.getName()
  29. +", Gender : " + person.getGender()
  30. +", Marital Status : " + person.getMaritalStatus()
  31. +" ]");
  32. }
  33. }
  34. }

步骤 5

执行程序,输出结果:

  1. Males:
  2. Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
  3. Person : [ Name : John, Gender : Male, Marital Status : Married ]
  4. Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
  5. Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]
  6. Females:
  7. Person : [ Name : Laura, Gender : Female, Marital Status : Married ]
  8. Person : [ Name : Diana, Gender : Female, Marital Status : Single ]
  9. Single Males:
  10. Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
  11. Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
  12. Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]
  13. Single Or Females:
  14. Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
  15. Person : [ Name : Diana, Gender : Female, Marital Status : Single ]
  16. Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
  17. Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]
  18. Person : [ Name : Laura, Gender : Female, Marital Status : Married ]

总结

过滤模式的实现在java8里面有典型的应用方法就是分组操作,可以根据指定的指标进行分组筛选。

  1. Map<Integer, List<Person >> groupMap = persons.stream().collect(Collectors.groupingBy(Person::getGender));
  2. groupMap.forEach((k, v) -> {
  3. System.out.println(k);
  4. v.forEach(System.out::println);
  5. });

得到的结果形式就是:

  •  k:是分组的指标,上面代码中的 gender
  •  v:是一个list的集合对象,就是 personList

可以参考:https://blog.csdn.net/chenhao_c_h/article/details/80691284

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/482014
推荐阅读
相关标签
  

闽ICP备14008679号