结构型模式-过滤器模式

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

java 复制代码
@AllArgsConstructor
@Data
public class Person {
    private String name;
    private String gender;
    private String maritalStatus;
}
java 复制代码
public interface Criteria {
     List<Person> meetCriteria(List<Person> persons);
}
java 复制代码
@AllArgsConstructor
public class AndCriteria implements Criteria{

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

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

    private final Criteria criteria;
    private final Criteria 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;
    }
}
java 复制代码
   @Test
    public void test1(){
        List<Person> persons = new ArrayList<Person>();

        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<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 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 ]

相关推荐
m0_571957582 小时前
Java | Leetcode Java题解之第543题二叉树的直径
java·leetcode·题解
魔道不误砍柴功4 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
NiNg_1_2344 小时前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
闲晨4 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
测开小菜鸟5 小时前
使用python向钉钉群聊发送消息
java·python·钉钉
P.H. Infinity6 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天6 小时前
java的threadlocal为何内存泄漏
java
caridle7 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
^velpro^7 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋37 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx