java 设计模式之过滤器模式(Filter)

Posted 咩咩文

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 设计模式之过滤器模式(Filter)相关的知识,希望对你有一定的参考价值。

1.概述:

允许开发人员根据不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把他们连接起来

2.实际场景

定义一个Person类,拥有姓名(name)、性别(gender)、婚姻状况(marital)三个属性。在实际的系统运用中需要对这三个属性进行不同条件的筛选 比如:

1.获取所有的男生信息

2.获取所有的单身人信息

3.获取所有的已婚人信息

4.获取所有的单身男生信息

5.获取所有的已婚女生信息


3.示例代码

Person类

package com.ven.domain;
/**
 * perison
 * @author xiaowen
 *
 */
public class Person 

	//姓名
	private String name;
	//性别:MALE FEMALE
	private String gender;
	//婚姻状况:已婚或未婚
	private String marital;
	
	public Person(String name, String gender, String marital) 
		this.name = name;
		this.gender = gender;
		this.marital = marital;
	

	public String getName() 
		return name;
	

	public void setName(String name) 
		this.name = name;
	

	public String getGender() 
		return gender;
	

	public void setGender(String gender) 
		this.gender = gender;
	

	public String getMarital() 
		return marital;
	

	public void setMarital(String marital) 
		this.marital = marital;
	
	
	

IFilter接口

package com.ven.service;

import java.util.List;

import com.ven.domain.Person;
/**
 * 过滤器接口
 * @author xiaowen
 *
 */
public interface IFilter 
	
	   List<Person> filter(List<Person> persons);

根据不同的条件实现过滤器接口

package com.ven.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ven.consist.CommonConsist;
import com.ven.domain.Person;
/**
 * 男生过滤器
 * @author xiaowen
 *
 */
public class MaleFilter implements IFilter 


	@Override
	public List<Person> filter(List<Person> persons) 
		List<Person> result = new ArrayList<>();
		List<Person> convertResult = persons.stream().filter(person->person.getGender().equals(CommonConsist.GENDER_MALE)).collect(Collectors.toList());
		convertResult.forEach(p->result.add(p););
		return result;
	



package com.ven.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ven.consist.CommonConsist;
import com.ven.domain.Person;
/**
 * 单身过滤器
 * @author xiaowen
 *
 */
public class SingleFilter implements IFilter 


	@Override
	public List<Person> filter(List<Person> persons) 
		List<Person> result = new ArrayList<>();
		List<Person> convertResult = persons.stream().filter(person->person.getMarital().equals(CommonConsist.MARITAL_SINGLE)).collect(Collectors.toList());
		convertResult.forEach(p->result.add(p););
		return result;
	


package com.ven.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ven.consist.CommonConsist;
import com.ven.domain.Person;
/**
 * 单身过滤器
 * @author xiaowen
 *
 */
public class MarryFilter implements IFilter 


	@Override
	public List<Person> filter(List<Person> persons) 
		List<Person> result = new ArrayList<>();
		List<Person> convertResult = persons.stream().filter(person->person.getMarital().equals(CommonConsist.MARITAL_MARRIED)).collect(Collectors.toList());
		convertResult.forEach(p->result.add(p););
		return result;
	



package com.ven.service;

import java.util.List;

import com.ven.domain.Person;
/**
 * 处理并且逻辑的组合条件过滤器
 * @author xiaowen
 *
 */
public class FilterAnd implements IFilter 
	private IFilter filter;
	private IFilter otherFilter;
	
	
	public FilterAnd(IFilter filter, IFilter otherFilter) 
		this.filter = filter;
		this.otherFilter = otherFilter;
	
	@Override
	public List<Person> filter(List<Person> persons) 
		List<Person> personList = filter.filter(persons);
		List<Person> result = otherFilter.filter(personList);
		return result;
	


接口常量类

package com.ven.consist;
/**
 * 接口常量类
 * @author xiaowen
 *
 */
public interface CommonConsist 
	static final String GENDER_MALE="MALE";
	static final String GENDER_FEMALE="FEMALE";
	static final String  MARITAL_SINGLE="SINGLE";
	static final String  MARITAL_MARRIED="MARRIED";


测试

package com.ven.test;

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

import com.ven.domain.Person;
import com.ven.service.FilterAnd;
import com.ven.service.MaleFilter;
import com.ven.service.MarryFilter;
import com.ven.service.SingleFilter;
/**
 * 过滤器模式(Filter)测试
 * @author xiaowen
 *
 */
public class Test02 
	public static void main(String[] args) 
		// 初始化数据
	      List<Person> persons = new ArrayList<>();
	      persons.add(new Person("霍一", "FEMALE", "MARRIED"));
	      persons.add(new Person("邓二", "MALE", "MARRIED"));
	      persons.add(new Person("张三", "MALE", "SINGLE"));
	      persons.add(new Person("李四", "FEMALE", "MARRIED"));
	      persons.add(new Person("王五", "MALE", "SINGLE"));
	      persons.add(new Person("赵六", "FEMALE", "SINGLE"));
	      persons.add(new Person("孙七", "MALE", "SINGLE"));
	      persons.add(new Person("罗八", "MALE", "MARRIED"));
	      persons.add(new Person("刘九", "FEMALE", "SINGLE"));
	      persons.add(new Person("史十", "FEMALE", "SINGLE"));
	      
	      
	      System.out.println("---------------获取所有的男性信息------------------");
	      List<Person> maleList = new MaleFilter().filter(persons);
	      maleList.forEach(male->
	    	  System.out.println(male.getName()+"-"+male.getGender()+"-"+male.getMarital());
	      );
	      System.out.println("---------------获取所有单身------------------");
	      List<Person> singleList = new SingleFilter().filter(persons);
	      singleList.forEach(single->
	    	  System.out.println(single.getName()+"-"+single.getGender()+"-"+single.getMarital());
	      );
	      System.out.println("--------------------所有单身的男性-------------------");
	      List<Person> singleAndMale = new FilterAnd(new MaleFilter(), new SingleFilter()).filter(persons);
	      singleAndMale.forEach(result->
	    	  System.out.println(result.getName()+"-"+result.getGender()+"-"+result.getMarital());
	      );
	      System.out.println("--------------------所有已婚的男性-------------------");
	      List<Person> marryList = new FilterAnd(new MaleFilter(), new MarryFilter()).filter(persons);
	      marryList.forEach(marrayAndMale->
	    	  System.out.println(marrayAndMale.getName()+"-"+marrayAndMale.getGender()+"-"+marrayAndMale.getMarital());
	      );



控制台信息

---------------获取所有的男性信息------------------
邓二-MALE-MARRIED
张三-MALE-SINGLE
王五-MALE-SINGLE
孙七-MALE-SINGLE
罗八-MALE-MARRIED
---------------获取所有单身------------------
张三-MALE-SINGLE
王五-MALE-SINGLE
赵六-FEMALE-SINGLE
孙七-MALE-SINGLE
刘九-FEMALE-SINGLE
史十-FEMALE-SINGLE
--------------------所有单身的男性-------------------
张三-MALE-SINGLE
王五-MALE-SINGLE
孙七-MALE-SINGLE
--------------------所有已婚的男性-------------------
邓二-MALE-MARRIED
罗八-MALE-MARRIED








以上是关于java 设计模式之过滤器模式(Filter)的主要内容,如果未能解决你的问题,请参考以下文章

在 Java 过滤器中获取配置的 url 模式 [重复]

JDK设计模式之——责任链

java设计模式之代理模式

java设计模式之代理模式

行为型设计模式之责任链模式

Java拦截过滤器模式