过滤器模式(Filter Pattern)

通过多个单一的功能筛选构建出一个复杂的筛选功能。

过滤器模式的实例

首先定义一个对象,我们后续可以通过名字(name),性别(gender),婚姻状况(maritalStatus)

  1. // 定义对象
  2. class Person {
  3. constructor(name, gender, maritalStatus){
  4. this.name = name;
  5. this.gender = gender;
  6. this.maritalStatus = maritalStatus;
  7. }
  8. getName() {
  9. return this.name;
  10. }
  11. getGender() {
  12. return this.gender;
  13. }
  14. getMaritalStatus() {
  15. return this.maritalStatus;
  16. }
  17. }

定义一些单一功能的筛选条件,比如啊判断是男,是女,是不是单身。

  1. // 添加筛选条件
  2. class CriteriaMale {
  3. meetCriteria(persons) {
  4. const malePersons = [];
  5. for (const person of persons) {
  6. if(person.getGender().toUpperCase() == "MALE"){
  7. malePersons.push(person);
  8. }
  9. }
  10. return malePersons;
  11. }
  12. }
  13. class CriteriaFemale {
  14. meetCriteria(persons) {
  15. const femalePersons = [];
  16. for (const person of persons) {
  17. if(person.getGender().toUpperCase() == "FEMALE"){
  18. femalePersons.push(person);
  19. }
  20. }
  21. return femalePersons;
  22. }
  23. }
  24. class CriteriaSingle {
  25. meetCriteria(persons) {
  26. const singlePersons = [];
  27. for (const person of persons) {
  28. if(person.getMaritalStatus().toUpperCase() == "SINGLE"){
  29. singlePersons.push(person);
  30. }
  31. }
  32. return singlePersons;
  33. }
  34. }

将单一功能增加对应的操作符,使单一功能筛选条件能通过组合来实现复杂的筛选。

  1. // 添加筛选操作符
  2. class AndCriteria {
  3. constructor(criteria, otherCriteria) {
  4. this.criteria = criteria;
  5. this.otherCriteria = otherCriteria;
  6. }
  7. meetCriteria(persons) {
  8. const firstCriteriaPersons = this.criteria.meetCriteria(persons);
  9. return this.otherCriteria.meetCriteria(firstCriteriaPersons);
  10. }
  11. }
  12. class OrCriteria{
  13. constructor(criteria, otherCriteria) {
  14. this.criteria = criteria;
  15. this.otherCriteria = otherCriteria;
  16. }
  17. meetCriteria(persons) {
  18. const firstCriteriaItems = this.criteria.meetCriteria(persons);
  19. const otherCriteriaItems = this.otherCriteria.meetCriteria(persons);
  20. for (const person of otherCriteriaItems) {
  21. if(firstCriteriaItems.indexOf(person)==-1){
  22. firstCriteriaItems.push(person);
  23. }
  24. }
  25. return firstCriteriaItems;
  26. }
  27. }

使用单一筛选条件或是组合单一筛选条件来筛选,达到复杂筛选目的

  1. function printPersons(persons){
  2. for (const person of persons) {
  3. console.log(person);
  4. }
  5. }
  6. const persons = [];
  7. persons.push(new Person("Robert","Male", "Single"));
  8. persons.push(new Person("John","Male", "Married"));
  9. persons.push(new Person("Laura","Female", "Married"));
  10. persons.push(new Person("Diana","Female", "Single"));
  11. persons.push(new Person("Mike","Male", "Single"));
  12. persons.push(new Person("Bobby","Male", "Single"));
  13. const male = new CriteriaMale();
  14. const female = new CriteriaFemale();
  15. const single = new CriteriaSingle();
  16. const singleMale = new AndCriteria(single, male);
  17. const singleOrFemale = new OrCriteria(single, female);
  18. console.log("Males: ");
  19. printPersons(male.meetCriteria(persons));
  20. console.log("\nFemales: ");
  21. printPersons(female.meetCriteria(persons));
  22. console.log("\nSingle Males: ");
  23. printPersons(singleMale.meetCriteria(persons));
  24. console.log("\nSingle Or Females: ");
  25. printPersons(singleOrFemale.meetCriteria(persons));
  26. /**
  27. * output:
  28. * Males:
  29. * Person { name: 'Robert', gender: 'Male', maritalStatus: 'Single' }
  30. * Person { name: 'John', gender: 'Male', maritalStatus: 'Married' }
  31. * Person { name: 'Mike', gender: 'Male', maritalStatus: 'Single' }
  32. * Person { name: 'Bobby', gender: 'Male', maritalStatus: 'Single' }
  33. *
  34. * Females:
  35. * Person { name: 'Laura', gender: 'Female', maritalStatus: 'Married' }
  36. * Person { name: 'Diana', gender: 'Female', maritalStatus: 'Single' }
  37. *
  38. * Single Males:
  39. * Person { name: 'Robert', gender: 'Male', maritalStatus: 'Single' }
  40. * Person { name: 'Mike', gender: 'Male', maritalStatus: 'Single' }
  41. * Person { name: 'Bobby', gender: 'Male', maritalStatus: 'Single' }
  42. *
  43. * Single Or Females:
  44. * Person { name: 'Robert', gender: 'Male', maritalStatus: 'Single' }
  45. * Person { name: 'Diana', gender: 'Female', maritalStatus: 'Single' }
  46. * Person { name: 'Mike', gender: 'Male', maritalStatus: 'Single' }
  47. * Person { name: 'Bobby', gender: 'Male', maritalStatus: 'Single' }
  48. * Person { name: 'Laura', gender: 'Female', maritalStatus: 'Married' }
  49. */

过滤器模式优势

在需要做类的筛选的时候,通过每次单一功能的筛选,再做聚合能极大的降低筛选功能的复杂性。

上一页(桥接模式)

下一页(组合模式)