过滤器模式代码举例(java语言版)

前言:这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。它结合多个标准来获得单一标准。(一般的过滤逻辑写在一个类里面,后期过滤逻辑增加会导致类代码急速膨胀,不利于维护。过滤器器模式是对过滤逻辑解耦)

JAVA语言版过滤器模式(又叫标准模式)

创建需要过滤的pojo类:

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 void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getMaritalStatus() {
        return maritalStatus;
    }

    public void setMaritalStatus(String maritalStatus) {
        this.maritalStatus = maritalStatus;
    }
}

创建接口:

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

创建接口实现类:

public class CriteriaFemale implements Criteria {
    @Override
    public List<Person> meetCriteria(List<Person> personList) {
        List<Person> list = new ArrayList<>();
        for (Person person : personList) {
            if (person.getGender().equalsIgnoreCase("female")) {
                list.add(person);
            }
        }
        return list;
    }
}

public class CriteriaMale implements Criteria {
    @Override
    public List<Person> meetCriteria(List<Person> personList) {
        List<Person> list = new ArrayList<>();
        for (Person person : personList) {
            if (person.getGender().equalsIgnoreCase("male")) {
                list.add(person);
            }
        }
        return list;
    }
}

public class CriteriaSingle implements Criteria {
    @Override
    public List<Person> meetCriteria(List<Person> personList) {
        List<Person> list = new ArrayList<>();
        for (Person person : personList) {
            if (person.getMaritalStatus().equalsIgnoreCase("Single")) {
                list.add(person);
            }
        }
        return list;
    }
}


public 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> personList) {
        List<Person> personListOne = criteria.meetCriteria(personList);
        List<Person> personListTwo = otherCriteria.meetCriteria(personList);
        for (Person person:personListTwo){
           if(!personListOne.contains(person)){
               personListOne.add(person);
           }
        }
        return personListOne;
    }
}

public 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> personList) {
        List<Person> list = criteria.meetCriteria(personList);
        return otherCriteria.meetCriteria(list);
    }
}



使用FilterPatternDome类演示过滤器模式:

public class FilterPatternDemo {
    public static void main(String[] args) {
        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();
        System.out.println("用CriteriaMale过滤器只想留下男性: ");
        printPersons(male.meetCriteria(persons));

        Criteria female = new CriteriaFemale();
        System.out.println("\n用CriteriaFemale过滤器只想留下女性:");
        printPersons(female.meetCriteria(persons));

        Criteria single = new CriteriaSingle();
        System.out.println("\n用CriteriaSingle过滤器只想留下单身:");
        printPersons(single.meetCriteria(persons));

        Criteria singleMale = new AndCriteria(single, male);
        System.out.println("\n用AndCriteria过滤器只想留下单身男性:");
        printPersons(singleMale.meetCriteria(persons));

        Criteria singleOrFemale = new OrCriteria(single, female);
        System.out.println("\n用OrCriteria过滤器想留下单身的或者女性:");
        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()
                    +" ]");
        }
    }
}

输出结果:

用CriteriaMale过滤器只想留下男性: 
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 ]

用CriteriaFemale过滤器只想留下女性:
Person : [ Name : Laura, Gender : Female, Marital Status : Married ]
Person : [ Name : Diana, Gender : Female, Marital Status : Single ]

用CriteriaSingle过滤器只想留下单身:
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 ]

用AndCriteria过滤器只想留下单身男性:
Person : [ Name : Robert, Gender : Male, Marital Status : Single ]
Person : [ Name : Mike, Gender : Male, Marital Status : Single ]
Person : [ Name : Bobby, Gender : Male, Marital Status : Single ]

用OrCriteria过滤器想留下单身的或者女性:
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 ]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值