过滤器模式

1、简介

过滤器模式(Filter Pattern),这种模式允许开发人员以不同的标准来过滤一组对象,通过逻辑运算以解耦的方式将他们连接起来。


2、测试

// 过滤接口
public interface Criteria {
	
	List<People> meetingPeople(List<People> peoples);
}
// 具体过滤类
public class CriteriaFemale implements Criteria{

	@Override
	public List<People> meetingPeople(List<People> peoples) {
		List<People> femalePeoples = new ArrayList<People>();
		for(People peo: peoples) {
			if(peo.getGender().equalsIgnoreCase("female")) {
				femalePeoples.add(peo);
			}
		}
		return femalePeoples;
	}
}

public class CriteriaMale implements Criteria{
	
	@Override
	public List<People> meetingPeople(List<People> peoples) {
		List<People> malePeoples = new ArrayList<People>();
		for(People peo: peoples) {
			if(peo.getGender().equalsIgnoreCase("male")) {
				malePeoples.add(peo);
			}
		}
		return malePeoples;
	}
}

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<People> meetingPeople(List<People> peoples) {
		return otherCriteria.meetingPeople(criteria.meetingPeople(peoples));
	}
}

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<People> meetingPeople(List<People> peoples) {
		List<People> firstCriteriaPeoples = criteria.meetingPeople(peoples);
		List<People> otherCriteriaPeoples = otherCriteria.meetingPeople(peoples);
		
		for(People peo: firstCriteriaPeoples) {
			if(!otherCriteriaPeoples.contains(peo)) {
				otherCriteriaPeoples.add(peo);
			}
		}
		return otherCriteriaPeoples;
	}
}

public class CriteriaSingle implements	Criteria{

	@Override
	public List<People> meetingPeople(List<People> peoples) {
		List<People> singlePeoples = new ArrayList<People>();
		for(People peo: peoples) {
			if(peo.getMaritalStatus().equalsIgnoreCase("single")) {
				singlePeoples.add(peo);
			}
		}
		return singlePeoples;
	}
}

		List<People> peoples = new ArrayList<People>(10);
		People peo1 = new People("Robert One", "male", "single");
		People peo2 = new People("Robert Two", "female", "double");
		People peo3 = new People("Robert Three", "male", "single");
		People peo4 = new People("Robert Four", "male", "single");
		People peo5 = new People("Robert Five", "female", "double");
		People peo6 = new People("Robert Six", "male", "single");
		People peo7 = new People("Robert Seven", "female", "single");
		People peo8 = new People("Robert Eight", "male", "double");
		People peo9 = new People("Robert Nine", "male", "single");
		People peo10 = new People("Robert Ten", "male", "single");
		peoples.add(peo1);
		peoples.add(peo2);
		peoples.add(peo3);
		peoples.add(peo4);
		peoples.add(peo5);
		peoples.add(peo6);
		peoples.add(peo7);
		peoples.add(peo8);
		peoples.add(peo9);
		peoples.add(peo10);
		
		Criteria maleCriteria = new CriteriaMale();
		Criteria femaleCriteria = new CriteriaFemale();
		Criteria singleCriteria = new CriteriaSingle();
		Criteria andCriteria = new AndCriteria(singleCriteria, maleCriteria);
		Criteria orCriteria = new OrCriteria(singleCriteria, femaleCriteria);
		
		System.out.println("male--------------------------------------------------");
		Utils.printList(maleCriteria.meetingPeople(peoples));
		System.out.println("female--------------------------------------------------");
		Utils.printList(femaleCriteria.meetingPeople(peoples));
		System.out.println("and single male--------------------------------------------------");
		Utils.printList(andCriteria.meetingPeople(peoples));
		System.out.println("or single female--------------------------------------------------");
		Utils.printList(orCriteria.meetingPeople(peoples));
posted @ 2020-08-11 11:39  kungFuPander  阅读(155)  评论(0编辑  收藏  举报