什么是过滤器模式?

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

过滤器模式就像是一连串的过滤器,按条件或标准筛选一组对象出来。

适用场景

需要按条件筛选对象的场景都可以使用过滤器模式。

代码实现

示例: 找对象
假设要求:白富美 18-23岁 女性

  1. //示例: 找对象
  2. //假设要求:白富美 18-23岁 女性
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.stream.Collectors;
  6. /**
  7. * 人类
  8. */
  9. class Person {
  10. private String name;//姓名
  11. private int age;//年龄
  12. private String gender;//性别
  13. private String skinColour;//肤色
  14. private String riches;//财富
  15. private String looks;//长相
  16. public Person(String name, int age, String gender, String skinColour, String riches, String looks) {
  17. this.name = name;
  18. this.age = age;
  19. this.gender = gender;
  20. this.skinColour = skinColour;
  21. this.riches = riches;
  22. this.looks = looks;
  23. }
  24. @Override
  25. public String toString() {
  26. return
  27. "姓名:" + name +
  28. ", 年龄:" + age +
  29. ", 性别:" + gender +
  30. ", 肤色:" + skinColour +
  31. ", 财富:" + riches +
  32. ", 长相:" + looks;
  33. }
  34. //region IDEA折叠代码
  35. public String getName() {
  36. return name;
  37. }
  38. public void setName(String name) {
  39. this.name = name;
  40. }
  41. public int getAge() {
  42. return age;
  43. }
  44. public void setAge(int age) {
  45. this.age = age;
  46. }
  47. public String getGender() {
  48. return gender;
  49. }
  50. public void setGender(String gender) {
  51. this.gender = gender;
  52. }
  53. public String getSkinColour() {
  54. return skinColour;
  55. }
  56. public void setSkinColour(String skinColour) {
  57. this.skinColour = skinColour;
  58. }
  59. public String getRiches() {
  60. return riches;
  61. }
  62. public void setRiches(String riches) {
  63. this.riches = riches;
  64. }
  65. public String getLooks() {
  66. return looks;
  67. }
  68. public void setLooks(String looks) {
  69. this.looks = looks;
  70. }
  71. //endregion
  72. }
  73. /**
  74. * 过滤器
  75. */
  76. interface Filter {
  77. List<Person> filter(List<Person> persons);
  78. }
  79. /**
  80. * 过滤年龄
  81. * 要18-23岁
  82. */
  83. class AgeFilter implements Filter {
  84. @Override
  85. public List<Person> filter(List<Person> persons) {
  86. return persons
  87. .stream()
  88. .filter(person -> person.getAge() >= 18 && person.getAge() < +23)
  89. .collect(Collectors.toList());
  90. }
  91. }
  92. /**
  93. * 过滤性别
  94. * 要女性
  95. */
  96. class GenderFilter implements Filter {
  97. @Override
  98. public List<Person> filter(List<Person> persons) {
  99. return persons
  100. .stream()
  101. .filter(person -> person.getGender().equals("女性"))
  102. .collect(Collectors.toList());
  103. }
  104. }
  105. /**
  106. * 过滤肤色,要白色
  107. */
  108. class SkinColourFilter implements Filter {
  109. @Override
  110. public List<Person> filter(List<Person> persons) {
  111. return persons
  112. .stream()
  113. .filter(person -> person.getSkinColour().equals("白色"))
  114. .collect(Collectors.toList());
  115. }
  116. }
  117. /**
  118. * 过滤财富,要富裕
  119. */
  120. class RichesFilter implements Filter {
  121. @Override
  122. public List<Person> filter(List<Person> persons) {
  123. return persons
  124. .stream()
  125. .filter(person -> person.getSkinColour().equals("富裕"))
  126. .collect(Collectors.toList());
  127. }
  128. }
  129. /**
  130. * 过滤长相,要好看
  131. */
  132. class LooksFilter implements Filter {
  133. @Override
  134. public List<Person> filter(List<Person> persons) {
  135. return persons
  136. .stream()
  137. .filter(person -> person.getSkinColour().equals("好看"))
  138. .collect(Collectors.toList());
  139. }
  140. }
  141. /**
  142. * 过滤器模式
  143. */
  144. public class FilterPattern {
  145. public static void main(String[] args) {
  146. //选妃大赛 找对象
  147. //准备妃子
  148. List<Person> persons = new ArrayList<>();
  149. persons.add(new Person("小红", 3, "女", "白色", "富裕", "好看"));
  150. persons.add(new Person("小蓝", 25, "女", "黄色", "贫穷", "难看"));
  151. persons.add(new Person("小绿", 5, "男", "白色", "富裕", "好看"));
  152. persons.add(new Person("小白", 56, "女", "白色", "贫穷", "好看"));
  153. persons.add(new Person("小橙", 35, "女", "黄色", "富裕", "好看"));
  154. persons.add(new Person("小黄", 12, "男", "黑色", "贫穷", "难看"));
  155. persons.add(new Person("小青", 23, "女", "黄色", "富裕", "好看"));
  156. persons.add(new Person("小丹", 18, "女", "白色", "贫穷", "好看"));
  157. persons.add(new Person("小紫", 16, "男", "白色", "贫穷", "难看"));
  158. persons.add(new Person("小粉", 17, "女", "黄色", "富裕", "好看"));
  159. persons.add(new Person("小褐", 19, "女", "白色", "贫穷", "难看"));
  160. persons.add(new Person("小棕", 20, "女", "黄色", "富裕", "难看"));
  161. persons.add(new Person("小黑", 43, "男", "白色", "贫穷", "好看"));
  162. persons.add(new Person("小灰", 91, "女", "黑色", "贫穷", "难看"));
  163. persons.add(new Person("小暗", 32, "女", "白色", "富裕", "好看"));
  164. persons.add(new Person("小亮", 56, "女", "黑色", "贫穷", "难看"));
  165. //过滤器
  166. AgeFilter ageFilter = new AgeFilter();
  167. GenderFilter genderFilter = new GenderFilter();
  168. SkinColourFilter skinColourFilter = new SkinColourFilter();
  169. RichesFilter richesFilter = new RichesFilter();
  170. LooksFilter looksFilter = new LooksFilter();
  171. //层层选拔
  172. System.out.println("选拨前人数:" + persons.size());
  173. persons = ageFilter.filter(persons);
  174. System.out.println("过滤年龄后人数:" + persons.size());
  175. persons = genderFilter.filter(persons);
  176. System.out.println("过滤性别后人数:" + persons.size());
  177. persons = skinColourFilter.filter(persons);
  178. System.out.println("过滤肤色后人数:" + persons.size());
  179. persons = richesFilter.filter(persons);
  180. System.out.println("过滤富裕后人数:" + persons.size());
  181. persons = looksFilter.filter(persons);
  182. System.out.println("过滤长相后人数:" + persons.size());
  183. //输出:
  184. //选拨前人数:16
  185. //过滤年龄后人数:3
  186. //过滤性别后人数:0
  187. //过滤肤色后人数:0
  188. //过滤富裕后人数:0
  189. //过滤长相后人数:0
  190. //打光棍吧 没找到 ……
  191. }
  192. }