Java笔记 ·

Java设计模式学习笔记—过滤器模式

前言

目前设计模式学习主要基于菜鸟教程的设计模式,后期不排除会追加从其他地方学来内容。

文章最后“Java设计模式笔记示例代码整合”为本系列代码整合,所有代码均为个人手打并运行测试,不定期更新。本节内容位于其Filter包(package)中。

过滤器模式

 

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

具体实现

我们将创建一个 Person 对象、Criteria 接口和实现了该接口的实体类,来过滤 Person 对象的列表。

CriteriaPatternDemo,我们的演示类使用 Criteria 对象,基于各种标准和它们的结合来过滤 Person 对象的列表。

1、创建一个类,在该类上应用标准。

Person.java

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、为标准(Criteria)创建一个接口。

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

3、创建实现了 Criteria 接口的实体类。

CriteriaMale.java

public class CriteriaMale implements Criteria{
    public List<Person> meetCriteria(List<Person> persons) {
        List<Person> melePersons = new ArrayList<Person>();
        for(Person person:persons){
            if (person.getGender().equalsIgnoreCase("MALE")){
                melePersons.add(person);
            }
        }
        return melePersons;
    }
}

CriteriaFemale.java

public class CriteriaFemal implements Criteria {
    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;
    }
}

CriteriaSingle.java

public class CriteraSingle implements Criteria {
    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;
    }
}

AndCriteria.java

public class AndCriteria implements Criteria {
    private Criteria criteria;
    private Criteria otherCriteria;

    public AndCriteria(Criteria criteria,Criteria otherCriteria){
        this.criteria =criteria;
        this.otherCriteria = otherCriteria;
    }
    public List<Person> meetCriteria(List<Person> persions) {
        List<Person> firstCriteriaPersons = criteria.meetCriteria(persions);
        return  otherCriteria.meetCriteria(firstCriteriaPersons);
    }
}

OrCriteria.java

public class OrCriteria implements Criteria {
    private Criteria criteria;
    private Criteria otherCriteria;

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

    public List<Person> meetCriteria(List<Person> persions) {
        List<Person> firstCriteriaItems = criteria.meetCriteria(persions);
        List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persions);
        for (Person person : otherCriteriaItems){
            if (!firstCriteriaItems.contains(person)){
                firstCriteriaItems.add(person);
            }
        }
        return firstCriteriaItems;
    }
}

4、使用不同的标准(Criteria)和它们的结合来过滤 Person 对象的列表。

public class CriteriaPatternDemo {

    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();
        Criteria female = new CriteriaFemal();
        Criteria single = new CriteraSingle();
        Criteria singleMale = new AndCriteria(single,male);
        Criteria singleOrFemale = new OrCriteria(single,female);

        System.out.println("Males: ");
        printPersons(male.meetCriteria(persons));

        System.out.println("\nFemale: ");
        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()
                    +" ]");
        }
    }
}

5、验证输出。

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 ]

Female:

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设计模式笔记示例代码整合

1
0.0 满分5 基于 0 个用户评分
立即购买

参与评论