以不同條件組合過濾人群為例惋砂。
[圖片上傳失敗...(image-c07ea4-1632304498237)]
- 1屑宠、創(chuàng)建人群類:
public class Person {
private String name;
private String gender;
private 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;
}
}
- 2厢洞、創(chuàng)建標準接口:
public interface Criteria {
public List<Person> meetCriteria(List<Person> persons);
}
- 3、創(chuàng)建實現(xiàn)了 Criteria 接口的實體類:
public class CriteriaMale implements Criteria{
@Override
public List<Person> meetCriteria(List<Person> persons) {
List<Person> malePersons = persons.stream().filter((item) -> {
return item.getGender().equalsIgnoreCase("male");
}).collect(Collectors.toList());
return malePersons;
}
}
--------------------------------------------------------------------------
public class CriteriaFemale implements Criteria{
@Override
public List<Person> meetCriteria(List<Person> persons) {
List<Person> malePersons = persons.stream().filter((item) -> {
return item.getGender().equalsIgnoreCase("female");
}).collect(Collectors.toList());
return malePersons;
}
}
--------------------------------------------------------------------------
public class CriteriaSingle implements Criteria{
@Override
public List<Person> meetCriteria(List<Person> persons) {
List<Person> malePersons = persons.stream().filter((item) -> {
return item.getMaritalStatus().equalsIgnoreCase("single");
}).collect(Collectors.toList());
return malePersons;
}
}
--------------------------------------------------------------------------
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> meetCriteria(List<Person> persons) {
List<Person> firstPersons = criteria.meetCriteria(persons);
List<Person> resultPersons = otherCriteria.meetCriteria(firstPersons);
return resultPersons;
}
}
--------------------------------------------------------------------------
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> meetCriteria(List<Person> persons) {
List<Person> firstPersons = criteria.meetCriteria(persons);
List<Person> otherPersons = otherCriteria.meetCriteria(persons);
otherPersons.stream().forEach(item -> {
if (!firstPersons.contains(item)){
firstPersons.add(item);
}
});
return firstPersons;
}
}
- 4典奉、使用不同的標準(Criteria)和它們的結合來過濾 Person 對象的列表:
public class CriteriaPatternDemo {
public static void main(String[] args) {
List<Person> persons = new ArrayList<Person>() {{
add(new Person("Robert", "Male", "Single"));
add(new Person("John", "Male", "Married"));
add(new Person("Laura", "Female", "Married"));
add(new Person("Diana", "Female", "Single"));
add(new Person("Mike", "Male", "Single"));
add(new Person("Bobby", "Male", "Single"));
}};
Criteria male = new CriteriaMale();
Criteria female = new CriteriaFemale();
Criteria single = new CriteriaSingle();
Criteria singleAndMale = 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 And Males: ");
printPersons(singleAndMale.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()
+" ]");
}
}
}
- 結果:
Males:
Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
Person : [ Name : John, Gender : Male, Marital Status : Married ]
Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]
Females:
Person : [ Name : Laura, Gender : Female, Marital Status : Married ]
Person : [ Name : Diana, Gender : Female, Marital Status : Single ]
Single And Males:
Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]
Single Or Females:
Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
Person : [ Name : Diana, Gender : Female, Marital Status : Single ]
Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]
Person : [ Name : Laura, Gender : Female, Marital Status : Married ]