过滤器模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.util.ArrayList;
import java.util.List;

class Person {

String name;
String gender;
String maritalStatus;

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

public String getName() {
return name;
}

public String getGender() {
return gender;
}

public String getMaritalStatus() {
return maritalStatus;
}
}

interface Criteria {
List<Person> meetCriteria(List<Person> persons);
}

class CriteriaMale implements Criteria {

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

class CriteriaFemale implements Criteria {

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

class CriteriaSingle implements Criteria {

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

class AndCriteria implements Criteria {

Criteria criteria;
Criteria otherCriteria;

public AndCriteria(Criteria criteria, Criteria otherCriteria) {
this.criteria = criteria;
this.otherCriteria = otherCriteria;
}

@Override
public List<Person> meetCriteria(List<Person> persons) {
List<Person> firstCriteriaPersons = criteria.meetCriteria(persons);
return otherCriteria.meetCriteria(firstCriteriaPersons);
}
}

class OrCriteria implements Criteria {

Criteria criteria;
Criteria otherCriteria;

public OrCriteria(Criteria criteria, Criteria otherCriteria) {
this.criteria = criteria;
this.otherCriteria = otherCriteria;
}

@Override
public List<Person> meetCriteria(List<Person> persons) {
List<Person> firstCriteriaItems = criteria.meetCriteria(persons);
List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persons);

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

public class CriteriaPatternDemo {
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();

persons.add(new Person("Robert", "Male", "Single"));
persons.add(new Person("John", "Male", "Married"));
persons.add(new Person("Laura", "Female", "Married"));
persons.add(new Person("Diana", "Female", "Single"));
persons.add(new Person("Mike", "Male", "Single"));
persons.add(new Person("Bobby", "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.meetCriteria(persons));

System.out.println("\nFemales: ");
printPersons(female.meetCriteria(persons));

System.out.println("\nSingle Males: ");
printPersons(singleMale.meetCriteria(persons));

System.out.println("\nSingle Or Females: ");
printPersons(singleOrFemale.meetCriteria(persons));
}

public static void printPersons(List<Person> persons) {
for (Person person : persons) {
System.out.println("Person : [ Name : " + person.getName()
+ ", Gender : " + person.getGender()
+ ", Marital Status : " + person.getMaritalStatus()
+ " ]");
}
}
}

过滤器模式
https://wangqian0306.github.io/2021/design-filter-pattern/
作者
WangQian
发布于
2021年7月21日
许可协议