电子说
过滤器模式是指通过构造一系列不同的标准实现类,通过这些标准实现类把目标对象(通常是多个)按照对应的标准进行过滤,从而得到想要的对象(或对象组)。这些标准实现类也可以构造成多个标准组合的形式(多个标准的“与”、“或”、“非”结合)。
下例中以过滤人员对象为例,人员Person类具有name、gender、maritalStatus等3个属性,在主函数中构造一组人员,使各人员具有不同的属性,然后通过过滤器对这些人员进行过滤。类图如下:
Person类:
package filter;
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;
}
public String getInformation() {
return name+":"+gender+","+maritalStatus;
}
}
Criteria接口类:
package filter;
import java.util.List;
public interface Criteria {
List < Person > meetCriteria(List < Person > persons);
}
CriteriaFemale实现类:
package filter;
import java.util.ArrayList;
import java.util.List;
/*
* 过滤得到女性人员
*/
public class CriteriaFemale implements Criteria{
@Override
public List< Person > meetCriteria(List< Person > persons) {
List < Person > femalePerson = new ArrayList < Person >();
for(Person person: persons) {
if(person.getGender().equals("女")) {
femalePerson.add(person);
}
}
return femalePerson;
}
}
CriteriaMale实现类:
package filter;
import java.util.ArrayList;
import java.util.List;
/*
* 过滤得到男性人员
*/
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().equals("男")) {
malePersons.add(person);
}
}
return malePersons;
}
}
CriteriaSingle实现类:
package filter;
import java.util.ArrayList;
import java.util.List;
/*
* 过滤得到单身人员
*/
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().equals("单身")) {
singlePersons.add(person);
}
}
return singlePersons;
}
}
Andcriteria实现类:
package filter;
import java.util.List;
public class AndCriteria implements Criteria{
private Criteria criteria;
private Criteria otherCriteria;
public AndCriteria(Criteria criteria,Criteria otherCriteria) {
this.criteria=criteria;
this.otherCriteria=otherCriteria;
}
@Override
public List< Person > meetCriteria(List< Person > persons) {
List < Person > firstCriteriaPersons = criteria.meetCriteria(persons);
return otherCriteria.meetCriteria(firstCriteriaPersons);
}
}
OrCriteria实现类:
package filter;
import java.util.List;
public class OrCriteria implements Criteria{
private Criteria criteria;
private Criteria otherCriteria;
public OrCriteria(Criteria criteria, Criteria otherCriteria) {
this.criteria=criteria;
this.otherCriteria=otherCriteria;
}
@Override
public List< Person > meetCriteria(List< Person > persons) {
List< Person > firstCriteriaPersons = criteria.meetCriteria(persons);
List< Person > ohterCriteriaPersons = otherCriteria.meetCriteria(persons);
for(Person person: ohterCriteriaPersons) {
if(!firstCriteriaPersons.contains(person)) {
firstCriteriaPersons.add(person);
}
}
return firstCriteriaPersons;
}
}
主函数调用类:
package main;
import java.util.ArrayList;
import java.util.List;
import filter.*;
public class FilterPattern {
public static void main(String[] args) {
List < Person > persons=new ArrayList < Person > ();
persons.add(new Person("张三","男","单身"));
persons.add(new Person("李四","女","已婚"));
persons.add(new Person("王五","女","单身"));
persons.add(new Person("赵六","男","已婚"));
persons.add(new Person("张七","女","单身"));
persons.add(new Person("李八","男","单身"));
persons.add(new Person("王九","女","单身"));
//过滤得到男性
Criteria male= new CriteriaMale();
List < Person > malePersons=male.meetCriteria(persons);
System.out.println("全部男性:");
for(Person person: malePersons) {
System.out.println(person.getInformation());
}
//过滤得到女性
Criteria female = new CriteriaFemale();
List < Person > femalePersons=female.meetCriteria(persons);
System.out.println("全部女性:");
for(Person person: femalePersons) {
System.out.println(person.getInformation());
}
//过滤得到单身人士
Criteria single = new CriteriaSingle();
List < Person > singlePersons=single.meetCriteria(persons);
System.out.println("全部单身:");
for(Person person: singlePersons) {
System.out.println(person.getInformation());
}
//过滤得到单身女性
Criteria femaleSingle = new AndCriteria(female,single);
List < Person > femaleSinglePersons=femaleSingle.meetCriteria(persons);
System.out.println("全部单身女性:");
for(Person person: femaleSinglePersons) {
System.out.println(person.getInformation());
}
//过滤得到男性或单身人士
Criteria maleOrSingle = new OrCriteria(male,single);
List < Person > maleOrSinglePersons=maleOrSingle.meetCriteria(persons);
System.out.println("全部单身或男性:");
for(Person person: maleOrSinglePersons) {
System.out.println(person.getInformation());
}
}
}
全部0条评论
快来发表一下你的评论吧 !