디자인 패턴 - Filter Pattern

Table of Contents

출처 : https://www.tutorialspoint.com/design_pattern/filter_pattern.htm

Filter pattern 또는 Criteria pattern은 개발자가 다른 기준을 사용하여 개체 집합을 필터링하고 논리적 작업을 통해 분리된 방식으로 연결할 수 있도록 하는 디자인 패턴입니다. 이 유형의 디자인 패턴은 단일 기준을 얻기 위해 여러 기준을 결합하기 때문에 구조적 패턴에 속합니다.

Implementation

Person 개체, Criteria 인터페이스 및 Person 개체 목록을 필터링하기 위해 이 인터페이스를 구현하는 구체적인 클래스를 만들 것입니다. CriteriaPatternDemo, 데모 클래스는 Criteria 개체를 사용하여 다양한 기준과 그 조합을 기반으로 Person 개체 목록을 필터링합니다.

Filter Pattern UML Diagram

Step 1

기준을 적용할 클래스를 만듭니다.

Person.java

 1public class Person {
 2
 3   private String name;
 4   private String gender;
 5   private String maritalStatus;
 6
 7   public Person(String name, String gender, String maritalStatus){
 8      this.name = name;
 9      this.gender = gender;
10      this.maritalStatus = maritalStatus;
11   }
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}

Step 2

Criteria에 대한 인터페이스를 만듭니다.

Criteria.java

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

Step 3

Criteria 인터페이스를 구현하는 구체적인 클래스를 만듭니다.

CriteriaMale.java

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

CriteriaFemale.java

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

CriteriaSingle.java

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

AndCriteria.java

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

OrCriteria.java

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

Step4

다른 기준과 그 조합을 사용하여 사람을 걸러냅니다.

CriteriaPatternDemo.java

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

Step 5

출력을 확인합니다.

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

이 시리즈의 게시물