过滤器模式
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
创建一个过滤人性别的过滤器模式
创建person对象
public class Person {
private String name;
private String gender;//xingbie
public Person(String name,String gender){
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
}
创建标准接口 Criteria
public interface Criteria {
public List<Person> meetCriteria(List<Person> persons);
}
创建man和women类实现Criteria接口
public class man implements Criteria{
@Override
public List<Person> meetCriteria(List<Person> persons) {
List<Person> list = new ArrayList<>();
for (Person person : persons) {
if(person.getGender().equalsIgnoreCase("man")){
list.add(person);
}
}
return list;
}
}
public class women implements Criteria{
@Override
public List<Person> meetCriteria(List<Person> persons) {
List<Person> list = new ArrayList<>();
for (Person person : persons) {
if(person.getGender().equalsIgnoreCase("women")){
list.add(person);
}
}
return list;
}
}
创建and和or两种过滤Criteria
public class and implements Criteria{
private Criteria criteria;
private Criteria othercriteria;
public and(Criteria criteria, Criteria othercriteria) {
super();
this.criteria = criteria;
this.othercriteria = othercriteria;
}
@Override
public List<Person> meetCriteria(List<Person> persons) {
List<Person> list1 = criteria.meetCriteria(persons);
return othercriteria.meetCriteria(list1);
}
}
public class or implements Criteria{
private Criteria criteria;
private Criteria othercriteria;
public and(Criteria criteria, Criteria othercriteria) {
super();
this.criteria = criteria;
this.othercriteria = othercriteria;
}
@Override
public List<Person> meetCriteria(List<Person> persons) {
List<Person> list1 = Criteria.meetCriteria(persons);
List<Person> meetCriteria = otherCriteria.meetCriteria(persons);
for (Person person : meetCriteria) {
if (!list1.contains(person)) {
list1.add(person);
}
}
return list1;
}
}
main测试
public static void main(String[] args) {
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("Robert","man"));
persons.add(new Person("John","man"));
persons.add(new Person("Laura","women"));
persons.add(new Person("Diana","women"));
persons.add(new Person("Mike","man"));
persons.add(new Person("Bobby","man"));
Criteria male = new man();//男的
Criteria female = new women();//女的
Criteria singleMale = new and(female, male);//既是男的也是女的
Criteria singleOrFemale = new or(male, female);//是男的或是女的
System.out.println("man: ");
printPersons(male.meetCriteria(persons));
System.out.println("\\women: ");
printPersons(female.meetCriteria(persons));
System.out.println("\\and: ");
printPersons(singleMale.meetCriteria(persons));
System.out.println("\\Or: ");
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()
+" ]");
}
}