【過濾器設計模式詳解】C/Java/JS/Go/Python/TS不同語言實現

刀法如飛發表於2023-04-06

簡介

過濾器模式(Filter Pattern)或標準模式(Criteria Pattern),是一種結構型模式。這種模式允許使用不同的標準條件來過濾一組物件,並透過邏輯運算的方式把各條件連線起來,它結合多個標準來獲得單一標準。

例子將建立一個 Person 物件、Criteria 介面和實現了該介面的實體類,來過濾 Person 物件的列表。Test 類使用 Criteria 物件,基於各種標準和它們的結合來過濾 Person 物件的列表。

 

作用

  1. 可以透過層層篩選,過濾出自己想要的結果。
  2. 面向介面程式設計,將物件過濾,介面不依賴物件;物件無入侵,每次只需要增加規則就行了,不用修改過濾物件類。

 

實現步驟

  1. 建立Criteria基礎介面,定義過濾方法。
  2. 建立多個條件類,實現過濾邏輯。
  3. 客戶端呼叫時,通條件類過濾資料。

 

UML

 

 

Java程式碼

 

標準介面類

// Criteria.java 定義抽象標準介面,聚合實體物件
public interface Criteria {
   public List<Person> filter(List<Person> persons);
}

 

 

具體標準實現類

 

// AndCriteria.java 定義And過濾標準
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> filter(List<Person> persons) {
      List<Person> firstCriteriaPersons = criteria.filter(persons);
      return otherCriteria.filter(firstCriteriaPersons);
   }
}
 

// OrCriteria.java 定義Or過濾標準
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> filter(List<Person> persons) {
      List<Person> firstCriteriaItems = criteria.filter(persons);
      List<Person> otherCriteriaItems = otherCriteria.filter(persons);

      for (Person person : otherCriteriaItems) {
         if (!firstCriteriaItems.contains(person)) {
            firstCriteriaItems.add(person);
         }
      }
      return firstCriteriaItems;
   }
}
 

// CriteriaFemale.java 根據標準介面實現的過濾
public class CriteriaFemale implements Criteria {

  @Override
  public List<Person> filter(List<Person> persons) {
    List<Person> femalePersons = new ArrayList<Person>();
    for (Person person : persons) {
      if (person.getGender().equalsIgnoreCase("FEMALE")) {
        femalePersons.add(person);
      }
    }
    return femalePersons;
  }
}
 

// CriteriaMale.java 根據標準介面實現的過濾
public class CriteriaMale implements Criteria {

   @Override
   public List<Person> filter(List<Person> persons) {
      List<Person> malePersons = new ArrayList<Person>();
      for (Person person : persons) {
         if (person.getGender().equalsIgnoreCase("MALE")) {
            malePersons.add(person);
         }
      }
      return malePersons;
   }
}
 

// CriteriaSingle.java 根據標準介面實現按屬性的過濾
public class CriteriaSingle implements Criteria {

   @Override
   public List<Person> filter(List<Person> persons) {
      List<Person> singlePersons = new ArrayList<Person>();
      for (Person person : persons) {
         if (person.getStatus().equalsIgnoreCase("SINGLE")) {
            singlePersons.add(person);
         }
      }
      return singlePersons;
   }
}
 

 

業務實體類

 

// Person.java 定義一個實體類,用來過濾的物件
public class Person {

   private String name;
   private String gender;
   private String status;

   public Person(String name, String gender, String status) {
      this.name = name;
      this.gender = gender;
      this.status = status;
   }

   public String getName() {
      return name;
   }

   public String getGender() {
      return gender;
   }

   public String getStatus() {
      return status;
   }

   public String toString() {
      return "Person : [ Name : " + getName() + ", Gender : "
            + getGender() + ", Marital Status : " + getStatus() + " ]";
   }
}
 

 

測試呼叫

 

    /**
     * 過濾器模式就是不斷組合過濾條件,然後層層過濾的模式
     * 這裡是簡單演示,用List篩選來模擬過濾,實際例子有各種資料結構
     */
    List<Person> persons = new ArrayList<Person>();

    persons.add(new Person("王男單", "Male", "Single"));
    persons.add(new Person("李男婚", "Male", "Married"));
    persons.add(new Person("張女婚", "Female", "Married"));
    persons.add(new Person("趙女單", "Female", "Single"));
    persons.add(new Person("劉男單", "Male", "Single"));
    persons.add(new Person("楊男單", "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.filter(persons));

    // 查詢女性
    System.out.println("\nFemales: ");
    printPersons(female.filter(persons));

    // 巢狀查詢女性且單身
    System.out.println("\nFemales and Single: ");
    printPersons(single.filter(female.filter(persons)));

    // 查詢男性男性單身
    System.out.println("\nSingle Males: ");
    printPersons(singleMale.filter(persons));

    // 查詢女性或單身
    System.out.println("\nSingle Or Females: ");
    printPersons(singleOrFemale.filter(persons));
 

 

C程式碼

 

head檔案

 

// func.h檔案
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

char *str_toupper(char str[]);
char *str_tolower(char str[]);

// 定義用於過濾的Person陣列查詢物件
// 因C語言無法定義動態陣列,將陣列長度存在此處
typedef struct FilterPersons
{
    int length;
    struct Person **persons;
} FilterPersons;

// 定義一個實體類,用來過濾的物件
typedef struct Person
{
    char name[50];
    char gender[20];
    char status[20];
    char *(*get_name)(struct Person *);
    char *(*to_string)(struct Person *);
    bool (*is_contained)(struct Person *, struct FilterPersons *);
} Person;
Person *person_constructor(char *name, char *gender, char *status);

// 定義抽象標準介面,聚合要過濾的物件集合
typedef struct Criteria
{
    struct Criteria *first_criteria;
    struct Criteria *other_criteria;
    FilterPersons *(*filter)(FilterPersons *, struct Criteria *);
} Criteria;

// 根據標準介面實現的過濾
typedef struct CriteriaFemale
{
    FilterPersons *(*filter)(FilterPersons *, struct Criteria *);
} CriteriaFemale;
CriteriaFemale *criteria_female_constructor();

// 根據標準介面實現的過濾
typedef struct CriteriaMale
{
    FilterPersons *(*filter)(FilterPersons *, struct Criteria *);
} CriteriaMale;
CriteriaMale *criteria_male_constructor();

// 根據標準介面實現的過濾
typedef struct CriteriaSingle
{
    FilterPersons *(*filter)(FilterPersons *, struct Criteria *);
} CriteriaSingle;
CriteriaSingle *criteria_single_constructor();

// 定義And過濾標準
typedef struct AndCriteria
{
    struct Criteria *first_criteria;
    struct Criteria *other_criteria;
    FilterPersons *(*filter)(FilterPersons *, struct Criteria *);
} AndCriteria;
AndCriteria *and_criteria_constructor(Criteria *, Criteria *);

// 定義Or過濾標準
typedef struct OrCriteria
{
    struct Criteria *first_criteria;
    struct Criteria *other_criteria;
    FilterPersons *(*filter)(FilterPersons *, struct Criteria *);
} OrCriteria;
OrCriteria *or_criteria_constructor(Criteria *, Criteria *);
 

 

條件介面類

 

// criteria.c 定義抽象標準介面,聚合實體物件
#include "func.h"
// c語言沒有抽象類或介面,Criterial作為基礎struct定義在head, 這裡放一些公共函式
char *str_toupper(char str[])
{
    int size = strlen(str);
    char *result = (char *)malloc(size * sizeof(char));
    int i = 0;
    while (str[i])
    {
        result[i] = toupper(str[i]);
        i++;
    }

    return result;
}

char *str_tolower(char str[])
{
    int size = strlen(str);
    char *result = (char *)malloc(size * sizeof(char));
    int i = 0;
    while (str[i])
    {
        result[i] = tolower(str[i]);
        i++;
    }

    return result;
}

 

 

具體標準實現類

 

// and_criteria.c 定義And過濾標準
#include "func.h"
// 先過濾條件1,再把結果按照條件2進行過濾
FilterPersons *and_criteria_filter(FilterPersons *filter, Criteria *criteria)
{
  FilterPersons *first_filter = criteria->first_criteria->filter(filter, criteria);
  return criteria->other_criteria->filter(first_filter, criteria);
}

AndCriteria *and_criteria_constructor(Criteria *first_criteria, Criteria *other_criteria)
{
  Criteria *criteria = (Criteria *)malloc(sizeof(Criteria));
  criteria->filter = &and_criteria_filter;
  AndCriteria *and_criteria = (AndCriteria *)criteria;
  and_criteria->first_criteria = first_criteria;
  and_criteria->other_criteria = other_criteria;
  and_criteria->filter = &and_criteria_filter;
  return and_criteria;
}
 

// or_criteria.c 定義Or過濾標準
#include "func.h"
// 先過濾條件1,再把結果按照條件2進行過濾
FilterPersons *or_criteria_filter(FilterPersons *filter, Criteria *criteria)
{
  FilterPersons *first_filter = criteria->first_criteria->filter(filter, criteria);
  FilterPersons *other_filter = criteria->other_criteria->filter(filter, criteria);

  int first_size = first_filter->length;
  int other_size = other_filter->length;

  for (int i = 0; i < other_size; i++)
  {
    Person *person = other_filter->persons[i];
    // 符合項如果不存在條件1裡,則追加到條件1裡去
    if (!person->is_contained(person, first_filter))
    {
      // 這裡陣列長度直接增加
      first_filter->persons[first_size++] = person;
    }
  }

  first_filter->length = first_size;

  return first_filter;
}

OrCriteria *or_criteria_constructor(Criteria *first_criteria, Criteria *other_criteria)
{
  Criteria *criteria = (Criteria *)malloc(sizeof(Criteria));
  criteria->filter = &or_criteria_filter;
  OrCriteria *or_criteria = (OrCriteria *)criteria;
  or_criteria->first_criteria = first_criteria;
  or_criteria->other_criteria = other_criteria;
  or_criteria->filter = &or_criteria_filter;
  return or_criteria;
}

 

// ccriteria_female.c 根據標準介面實現的過濾
#include "func.h"
// 根據是否女性進行過濾
FilterPersons *criteria_female_filter(FilterPersons *filter, Criteria *criteria)
{
  int person_size = filter->length;
  int *female_index_list = (int *)malloc(person_size * sizeof(int));
  int count = 0;
  char *gender;
  for (int i = 0; i < person_size; i++)
  {
    if (filter->persons[i] == NULL)
    {
      break;
    }
    gender = str_toupper(filter->persons[i]->gender);
    if (strcmp(gender, "FEMALE") == 0)
    {
      // 記錄下所有符合條件的person下標
      female_index_list[count] = i;
      count += 1;
    }
  }
  free(gender);
  free(female_index_list);

  // 將符合條件的person追加到新陣列
  Person **female_persons = (Person **)calloc(count, sizeof(Person));
  for (int i = 0; i < count; i++)
  {
    female_persons[i] = filter->persons[female_index_list[i]];
  }
  FilterPersons *female_filter = (FilterPersons *)calloc(1, sizeof(FilterPersons));
  female_filter->length = count;
  female_filter->persons = female_persons;
  return female_filter;
}

CriteriaFemale *criteria_female_constructor()
{
  Criteria *criteria = (Criteria *)malloc(sizeof(Criteria));
  criteria->filter = &criteria_female_filter;
  CriteriaFemale *criteria_female = (CriteriaFemale *)criteria;
  criteria_female->filter = &criteria_female_filter;
  return criteria_female;
}

 

// criteria_male.c 根據標準介面實現的過濾
#include "func.h"

// 根據是否男性進行過濾
FilterPersons *criteria_male_filter(FilterPersons *filter, Criteria *criteria)
{
  int person_size = filter->length;
  int *male_index_list = (int *)malloc(person_size * sizeof(int));
  int count = 0;
  char *gender;
  for (int i = 0; i < person_size; i++)
  {
    if (filter->persons[i] == NULL)
    {
      break;
    }
    gender = str_tolower(filter->persons[i]->gender);
    if (strcmp(gender, "male") == 0)
    {
      // 記錄下所有符合條件的person下標
      male_index_list[count] = i;
      count += 1;
    }
  }
  free(gender);
  free(male_index_list);

  // 將符合條件的person追加到新陣列
  Person **male_persons = (Person **)calloc(count, sizeof(Person));
  for (int i = 0; i < count; i++)
  {
    male_persons[i] = filter->persons[male_index_list[i]];
  }
  FilterPersons *male_filter = (FilterPersons *)calloc(1, sizeof(FilterPersons));
  male_filter->length = count;
  male_filter->persons = male_persons;
  return male_filter;
}

CriteriaMale *criteria_male_constructor()
{
  Criteria *criteria = (Criteria *)malloc(sizeof(Criteria));
  criteria->filter = &criteria_male_filter;
  CriteriaMale *criteria_male = (CriteriaMale *)criteria;
  criteria_male->filter = &criteria_male_filter;
  return criteria_male;
}
 

// criteria_single.c 根據標準介面實現按屬性的過濾
#include "func.h"

// 根據是否單身進行過濾
FilterPersons *criteria_single_filter(FilterPersons *filter, Criteria *criteria)
{
  int person_size = filter->length;
  int *single_index_list = (int *)malloc(person_size * sizeof(int));
  int count = 0;
  char *status;
  for (int i = 0; i < person_size; i++)
  {
    if (filter->persons[i] == NULL)
    {
      break;
    }
    status = str_tolower(filter->persons[i]->status);
    if (strcmp(status, "single") == 0)
    {
      // 記錄下所有符合條件的person下標
      single_index_list[count] = i;
      count += 1;
    }
  }
  free(status);
  free(single_index_list);

  // 將符合條件的person追加到新陣列
  Person **single_persons = (Person **)calloc(count, sizeof(Person));
  for (int i = 0; i < count; i++)
  {
    single_persons[i] = filter->persons[single_index_list[i]];
  }

  FilterPersons *single_filter = (FilterPersons *)calloc(1, sizeof(FilterPersons));
  single_filter->length = count;
  single_filter->persons = single_persons;
  return single_filter;
}

CriteriaSingle *criteria_single_constructor()
{
  Criteria *criteria = (Criteria *)malloc(sizeof(Criteria));
  criteria->filter = &criteria_single_filter;
  CriteriaSingle *criteria_single = (CriteriaSingle *)criteria;
  criteria_single->filter = &criteria_single_filter;
  return criteria_single;
}

 

 

業務實體類

 

// person.c 定義一個實體類,用來過濾的物件
#include "func.h"
// 是否被包含在物件陣列中
bool person_is_contained(Person *person, FilterPersons *filter)
{
  int persons_size = filter->length;
  for (int i = 0; i < persons_size; i++)
  {
    if (filter->persons[i] == person)
    {
      return true;
    }
  }
  return false;
}

char *person_get_name(Person *person)
{
  return person->name;
}

// 返回字串
char *person_to_string(Person *person)
{
  char *result = (char *)malloc(500 * sizeof(char));
  strcat(result, "Person :[ name :");
  strcat(result, person->name);
  strcat(result, ", gender : ");
  strcat(result, person->gender);
  strcat(result, ", status : ");
  strcat(result, person->status);
  strcat(result, "]");
  return result;
}

Person *person_constructor(char *name, char *gender, char *status)
{
  Person *person = (Person *)malloc(sizeof(Person));
  strncpy(person->name, name, 50);
  strncpy(person->gender, gender, 20);
  strncpy(person->status, status, 20);
  person->get_name = &person_get_name;
  person->to_string = &person_to_string;
  person->is_contained = &person_is_contained;
  return person;
}
 

 

測試呼叫

 

    /**
     * 過濾器模式就是不斷組合過濾條件,然後層層過濾的模式
     * 這裡是簡單演示,用List篩選來模擬過濾,實際例子有各種資料結構。
     */

    int data_size = 6;
    // 定義一些資料
    char data[6][3][100] = {
        {"王男單", "Male", "Single"},
        {"李男婚", "Male", "Married"},
        {"張女婚", "Female", "Married"},
        {"趙女單", "Female", "Single"},
        {"劉男單", "Male", "Single"},
        {"楊男單", "Male", "Single"}};

    // 定義persons陣列
    Person *persons[data_size];
    for (int i = 0; i < data_size; i++)
    {
        char *name = data[i][0];
        char *gender = data[i][1];
        char *status = data[i][2];
        Person *person = person_constructor(name, gender, status);
        persons[i] = person;
    }

    // 構建查詢物件
    FilterPersons *filter_persons = (FilterPersons *)malloc(sizeof(FilterPersons *));
    filter_persons->length = data_size;
    filter_persons->persons = persons;

    // 宣告屬性過濾條件,可用Criteria或具體條件宣告
    Criteria *criteria_male = (Criteria *)criteria_male_constructor();
    CriteriaFemale *criteria_female = criteria_female_constructor();
    Criteria *criteria_single = (Criteria *)criteria_single_constructor();

    // 宣告邏輯條件,傳入屬性過濾條件
    Criteria *single_male = (Criteria *)and_criteria_constructor(criteria_single, criteria_male);
    OrCriteria *single_or_female = or_criteria_constructor(criteria_single, (Criteria *)criteria_female);

    // 查詢男性
    printf("\n Males: ");
    print_persons(((CriteriaMale *)criteria_male)->filter(filter_persons, criteria_male));

    // 查詢女性
    printf("\nFemales: ");
    print_persons(criteria_female->filter(filter_persons, (Criteria *)criteria_female));

    // 巢狀查詢女性且單身
    printf("\nFemales and Single: ");
    FilterPersons *females = criteria_female->filter(filter_persons, (Criteria *)criteria_female);
    print_persons(((CriteriaSingle *)criteria_single)->filter(females, criteria_single));

    // 查詢男性男性單身
    printf("\nSingle Males: ");
    // 逐個條件過濾,與下面AndCriteria效果相同
    FilterPersons *single_males = ((CriteriaMale *)criteria_male)->filter(filter_persons, criteria_male);
    print_persons(((CriteriaSingle *)criteria_single)->filter(single_males, criteria_single));
    printf("\nSingle Males: ");
    // 透過AndCriteria來過濾
    FilterPersons *single_males2 = ((AndCriteria *)single_male)->filter(filter_persons, single_male);
    print_persons(single_males2);

    // 查詢女性或單身
    printf("\nSingle Or Females: ");
    print_persons(single_or_female->filter(filter_persons, (Criteria *)single_or_female));

    free(filter_persons);
    free(criteria_male);
    free(criteria_female);
    free(criteria_single);
    free(single_male);
    free(single_or_female);

    return 0;

 

 

更多語言版本

不同語言實現設計模式:https://github.com/microwind/design-pattern

相關文章