结构型模式-过滤器模式

允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。

@AllArgsConstructor
@Data
public class Person {
    private String name;
    private String gender;
    private String maritalStatus;
}
public interface Criteria {
     List meetCriteria(List persons);
}
@AllArgsConstructor
public class AndCriteria implements Criteria{

    private final Criteria criteria;
    private final Criteria otherCriteria;
    
    @Override
    public List meetCriteria(List persons) {

        List firstCriteriaPersons = criteria.meetCriteria(persons);
        return otherCriteria.meetCriteria(firstCriteriaPersons);
    }
}
public class CriteriaFemale implements Criteria{
    @Override
    public List meetCriteria(List persons) {
        List femalePersons = new ArrayList();
        for (Person person : persons) {
            if(person.getGender().equalsIgnoreCase("FEMALE")){
                femalePersons.add(person);
            }
        }
        return femalePersons;
    }
}
public class CriteriaMale implements Criteria{
    @Override
    public List meetCriteria(List persons) {
        List malePersons = new ArrayList();
        for (Person person : persons) {
            if(person.getGender().equalsIgnoreCase("MALE")){
                malePersons.add(person);
            }
        }
        return malePersons;
    }
}
public class CriteriaSingle implements Criteria{
    @Override
    public List meetCriteria(List persons) {
        List singlePersons = new ArrayList();
        for (Person person : persons) {
            if(person.getMaritalStatus().equalsIgnoreCase("SINGLE")){
                singlePersons.add(person);
            }
        }
        return singlePersons;
    }
}
@AllArgsConstructor
public class OrCriteria implements Criteria{

    private final Criteria criteria;
    private final Criteria otherCriteria;
    
    @Override
    public List meetCriteria(List persons) {
        List firstCriteriaItems = criteria.meetCriteria(persons);
        List otherCriteriaItems = otherCriteria.meetCriteria(persons);
        for (Person person : otherCriteriaItems) {
            if (!firstCriteriaItems.contains(person)){
                firstCriteriaItems.add(person);
            }
        }
        return firstCriteriaItems;
    }
}
   @Test
    public void test1(){
        List 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));

    }

    private  void printPersons(List 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 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 ]

 

你可能感兴趣的:(设计模式,java,设计模式)