• 过滤器模式


    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));
    
  • 相关阅读:
    服务器控件的 ID,ClientID,UniqueID 的区别
    GridView使用总结
    javascript对象
    如何对SQL Server 2005进行设置以允许远程连接(转载)
    Master Pages and JavaScript document.getElementById
    Linux paste命令
    linux脚本和代码的加密
    SDWAN的优势
    dsd
    ASA防火墙忘记密码之后的恢复步骤
  • 原文地址:https://www.cnblogs.com/kungFuPander/p/13474601.html
Copyright © 2020-2023  润新知