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;
}

}

import java.util.List;

//以标准创建一个接口

public interface Cirteria {
public List meetCirteria(List persons);
}

import java.util.ArrayList;
import java.util.List;

//创建实现Cirteria接口的实体类

public class CirteriaMale implements Cirteria {
@Override
public List meetCirteria(List persons) {
List malePersons = new ArrayList();
for (Person person : persons) {
if (person.getGender().equalsIgnoreCase("MALE")) {
malePersons.add(person);
}
}
return malePersons;
}
}

import java.util.ArrayList;
import java.util.List;

public class CirteriaFemale implements Cirteria {
@Override
public List meetCirteria(List persons) {
List femalePersons = new ArrayList();
for (Person person : persons) {
if (person.getGender().equalsIgnoreCase("FEMALE")) {
femalePersons.add(person);
}
}
return femalePersons;
}
}

import java.util.ArrayList;
import java.util.List;;

public class CirteriaSingle implements Cirteria {
@Override
public List meetCirteria(List persons) {
List singlePersons = new ArrayList();
for (Person person : persons) {
if (person.getMaritalStatus().equalsIgnoreCase("SINGLE")) {
singlePersons.add(person);
}
}
return singlePersons;
}
}

import java.util.List;

public class AndCirteria implements Cirteria {

private Cirteria cirteria;
private Cirteria otherCirteria;

public AndCirteria(Cirteria cirteria, Cirteria otherCirteria) {
    this.cirteria = cirteria;
    this.otherCirteria = otherCirteria;
}

@Override
public List meetCirteria(List persons) {
    List firstCirteriaPersons = cirteria.meetCirteria(persons);
    return otherCirteria.meetCirteria(firstCirteriaPersons);
}

}

import java.util.List;

public class OrCirteria implements Cirteria {

private Cirteria cirteria;
private Cirteria otherCirteria;

public OrCirteria(Cirteria cirteria, Cirteria otherCirteria) {
    this.cirteria = cirteria;
    this.otherCirteria = otherCirteria;
}

@Override
public List meetCirteria(List persons) {
    List firstCirteriaItems = cirteria.meetCirteria(persons);
    List otherCirteriaItems = otherCirteria.meetCirteria(persons);
    for (Person person : otherCirteriaItems) {
        if (!firstCirteriaItems.contains(person)) {
            firstCirteriaItems.add(person);
        }
    }
    return firstCirteriaItems;
}

}

import java.util.ArrayList;
import java.util.List;

//使用不用的标准和他们的结合来过滤person对象

public class CirteriaPatternDemo {
public static void main(String[] args) {
/*
* 过滤器模式或标准模式是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把他们链接起来,这种类型的设计模式属于结构型模式,他结合多个标准来获得单一标准
*/
List persons = new ArrayList();

    persons.add(new Person("Robert", "Male", "Single"));
    persons.add(new Person("Jhon", "Male", "Married"));
    persons.add(new Person("Laure", "Female", "Married"));
    persons.add(new Person("Diana", "Female", "Single"));
    persons.add(new Person("Mike", "Male", "Single"));
    persons.add(new Person("Bobby", "Male", "Single"));
    
    Cirteria male = new CirteriaMale();
    Cirteria female = new CirteriaFemale();
    Cirteria single = new CirteriaSingle();
    Cirteria singleMale = new AndCirteria(single, male);
    Cirteria singleOrFemale = new OrCirteria(single, female);
    
    System.out.println("Males: ");
    printPersons(male.meetCirteria(persons));
    
    System.out.println("\nFemales: ");
    printPersons(female.meetCirteria(persons));
    
    System.out.println("\nSing Males: ");
    printPersons(singleMale.meetCirteria(persons));
    
    System.out.println("\nSingle Or Females: ");
    printPersons(singleOrFemale.meetCirteria(persons));
    
}

public static void printPersons(List persons) {
    for (Person person : persons) {
        System.out.println("person : [name : " + person.getName() + ", gender : " + person.getGender() + ", marital status : " + person.getMaritalStatus() + " ]");
    }
}

}

你可能感兴趣的:(Java过滤器模式)