当前位置:   article > 正文

设计模式篇之过滤器模式_过滤器用到什么设计模式

过滤器用到什么设计模式

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

设计思路:

  • 创建一个要过滤的类,普通类即可,要有获得其私有属性的get方法
  • 创建一个接口,规定过滤方法
  • 实现接口,可以依需要来实现过滤的条件(通过重写过滤方法),参数传递的可以说List<过滤类>类的容器
  • 复杂过滤类可以通过设置传递接口参数(复用其他基础过滤类)来实现多重过滤


过滤器模式的 UML 图

  1. //步骤 1
  2. //创建一个类,在该类上应用标准。
  3. Person.java
  4. public class Person {
  5. private String name;
  6. private String gender;
  7. private String maritalStatus;
  8. public Person(String name,String gender,String maritalStatus){
  9. this.name = name;
  10. this.gender = gender;
  11. this.maritalStatus = maritalStatus;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public String getGender() {
  17. return gender;
  18. }
  19. public String getMaritalStatus() {
  20. return maritalStatus;
  21. }
  22. }
  23. //步骤 2
  24. //为标准(Criteria)创建一个接口。
  25. Criteria.java
  26. import java.util.List;
  27. public interface Criteria {
  28. public List<Person> meetCriteria(List<Person> persons);
  29. }
  30. //步骤 3
  31. //创建实现了 Criteria 接口的实体类。
  32. CriteriaMale.java
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. public class CriteriaMale implements Criteria {
  36. @Override
  37. public List<Person> meetCriteria(List<Person> persons) {
  38. List<Person> malePersons = new ArrayList<Person>();
  39. for (Person person : persons) {
  40. if(person.getGender().equalsIgnoreCase("MALE")){
  41. malePersons.add(person);
  42. }
  43. }
  44. return malePersons;
  45. }
  46. }
  47. CriteriaFemale.java
  48. import java.util.ArrayList;
  49. import java.util.List;
  50. public class CriteriaFemale implements Criteria {
  51. @Override
  52. public List<Person> meetCriteria(List<Person> persons) {
  53. List<Person> femalePersons = new ArrayList<Person>();
  54. for (Person person : persons) {
  55. if(person.getGender().equalsIgnoreCase("FEMALE")){
  56. femalePersons.add(person);
  57. }
  58. }
  59. return femalePersons;
  60. }
  61. }
  62. CriteriaSingle.java
  63. import java.util.ArrayList;
  64. import java.util.List;
  65. public class CriteriaSingle implements Criteria {
  66. @Override
  67. public List<Person> meetCriteria(List<Person> persons) {
  68. List<Person> singlePersons = new ArrayList<Person>();
  69. for (Person person : persons) {
  70. if(person.getMaritalStatus().equalsIgnoreCase("SINGLE")){
  71. singlePersons.add(person);
  72. }
  73. }
  74. return singlePersons;
  75. }
  76. }
  77. AndCriteria.java
  78. import java.util.List;
  79. public class AndCriteria implements Criteria {
  80. private Criteria criteria;
  81. private Criteria otherCriteria;
  82. public AndCriteria(Criteria criteria, Criteria otherCriteria) {
  83. this.criteria = criteria;
  84. this.otherCriteria = otherCriteria;
  85. }
  86. @Override
  87. public List<Person> meetCriteria(List<Person> persons) {
  88. List<Person> firstCriteriaPersons = criteria.meetCriteria(persons);
  89. return otherCriteria.meetCriteria(firstCriteriaPersons);
  90. }
  91. }
  92. OrCriteria.java
  93. import java.util.List;
  94. public class OrCriteria implements Criteria {
  95. private Criteria criteria;
  96. private Criteria otherCriteria;
  97. public OrCriteria(Criteria criteria, Criteria otherCriteria) {
  98. this.criteria = criteria;
  99. this.otherCriteria = otherCriteria;
  100. }
  101. @Override
  102. public List<Person> meetCriteria(List<Person> persons) {
  103. List<Person> firstCriteriaItems = criteria.meetCriteria(persons);
  104. List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persons);
  105. for (Person person : otherCriteriaItems) {
  106. if(!firstCriteriaItems.contains(person)){
  107. firstCriteriaItems.add(person);
  108. }
  109. }
  110. return firstCriteriaItems;
  111. }
  112. }
  113. //步骤4
  114. //使用不同的标准(Criteria)和它们的结合来过滤 Person 对象的列表。
  115. CriteriaPatternDemo.java
  116. import java.util.ArrayList;
  117. import java.util.List;
  118. public class CriteriaPatternDemo {
  119. public static void main(String[] args) {
  120. List<Person> persons = new ArrayList<Person>();
  121. persons.add(new Person("Robert","Male", "Single"));
  122. persons.add(new Person("John","Male", "Married"));
  123. persons.add(new Person("Laura","Female", "Married"));
  124. persons.add(new Person("Diana","Female", "Single"));
  125. persons.add(new Person("Mike","Male", "Single"));
  126. persons.add(new Person("Bobby","Male", "Single"));
  127. Criteria male = new CriteriaMale();
  128. Criteria female = new CriteriaFemale();
  129. Criteria single = new CriteriaSingle();
  130. Criteria singleMale = new AndCriteria(single, male);
  131. Criteria singleOrFemale = new OrCriteria(single, female);
  132. System.out.println("Males: ");
  133. printPersons(male.meetCriteria(persons));
  134. System.out.println("\nFemales: ");
  135. printPersons(female.meetCriteria(persons));
  136. System.out.println("\nSingle Males: ");
  137. printPersons(singleMale.meetCriteria(persons));
  138. System.out.println("\nSingle Or Females: ");
  139. printPersons(singleOrFemale.meetCriteria(persons));
  140. }
  141. public static void printPersons(List<Person> persons){
  142. for (Person person : persons) {
  143. System.out.println("Person : [ Name : " + person.getName()
  144. +", Gender : " + person.getGender()
  145. +", Marital Status : " + person.getMaritalStatus()
  146. +" ]");
  147. }
  148. }
  149. }
  150. //步骤 5
  151. //执行程序,输出结果:
  152. Males:
  153. Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
  154. Person : [ Name : John, Gender : Male, Marital Status : Married ]
  155. Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
  156. Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]
  157. Females:
  158. Person : [ Name : Laura, Gender : Female, Marital Status : Married ]
  159. Person : [ Name : Diana, Gender : Female, Marital Status : Single ]
  160. Single Males:
  161. Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
  162. Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
  163. Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]
  164. Single Or Females:
  165. Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
  166. Person : [ Name : Diana, Gender : Female, Marital Status : Single ]
  167. Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
  168. Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]
  169. 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

过滤器模式就是要吧集合中不需要的东西根据不同的的过滤规则,或者组合性质的过滤规则来进行过滤,留下最终想要的数据,而不会因为后来添加新的规则后,或者更换过滤要求而需要进行批量的修改代码,非常简便好用的设计模式。

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

闽ICP备14008679号