JAVA责任链模式-过滤器原理

package test;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String msg = "大家好:),<script>,敏感,网络授课没感觉,因为看不见大伙";
		/**
		 * 需求:网站需要对我提交的内容进行过滤
		 */
		
		MagProcessor magProcessor = new MagProcessor();
		magProcessor.setMsg(msg);
		String msgdb = magProcessor.process();
	}

}

package test;

public class MagProcessor {
	String msg;

	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String process(){
		//process html tag
		String r = msg.replace("<", "[")
					.replace(">", "]");
		//process the sensitive words
		r=r.replace("被就业", "就业");
		
		
		return null;
	}
}
通过MagProcessor 的process对信息进行过滤,一旦发生新的需求,我们不得不修养这个方法,这就违背了对修改关闭,对扩张开发的设计思想,那么怎么办呢?
请看第二节。
package com.bjsxt.dp.filter;

public class MsgProcessor {
	private String msg;
	
	Filter[] filters = {new HTMLFilter(), new SesitiveFilter(), new FaceFilter()};
	
	public FilterChain getFc() {
		return fc;
	}

	public void setFc(FilterChain fc) {
		this.fc = fc;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String process() {
		String msg  = "";
		
		for(Filter f:filters){
                 r=f.doFilters(r);
                }
		return r;
	}
}

通过这样的方式,我们分化了各个校验规则,提高了可扩展性,下面请考虑另外一个问题,如果有其他的链条,如何拼接呢?

package com.bjsxt.dp.filter;

public class MsgProcessor {
	private String msg;
	
	//Filter[] filters = {new HTMLFilter(), new SesitiveFilter(), new FaceFilter()};
	FilterChain fc;
	
	public FilterChain getFc() {
		return fc;
	}

	public void setFc(FilterChain fc) {
		this.fc = fc;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String process() {
		
		
		return fc.doFilter(msg);
		
		
	}
}

package com.bjsxt.dp.filter;

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

public class FilterChain implements Filter {
	List<Filter> filters = new ArrayList<Filter>();
	
	public FilterChain addFilter(Filter f) {
		this.filters.add(f);
		return this;
	}
	
	public String doFilter(String str) {
		String r = str;
		for(Filter f: filters) {
			r = f.doFilter(r);
		}
		return r;
	}
}

package com.bjsxt.dp.filter;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String msg = "大家好:),<script>,敏感,被就业,网络授课没感觉,因为看不见大家伙儿";
		MsgProcessor mp = new MsgProcessor();
		mp.setMsg(msg);
		FilterChain fc = new FilterChain();
		fc.addFilter(new HTMLFilter())
		  .addFilter(new SesitiveFilter())
		  ;
		FilterChain fc2 = new FilterChain();
		fc2.addFilter(new FaceFilter());
		
		fc.addFilter(fc2);
		mp.setFc(fc);
		String result = mp.process();
		System.out.println(result);
	}

}

我们要实现链条的拼接,其实我们想想,一个Filter链条本身就是一个Filter,所以我们使用FilterChain来模拟Filter链条,并且实现了Filter接口,

这样我们的Filterchain不仅可以添加一个个Filter,还能直接添加Filter链条,现在的问题是如何让我们的Filter也可以处理返回的信息呢?

package com.bjsxt.dp.filter;

public interface Filter {
	void doFilter(Request request, Response response, FilterChain chain);
}
package com.bjsxt.dp.filter;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String msg = "大家好:),<script>,敏感,被就业,网络授课没感觉,因为看不见大家伙儿";
		Request request = new Request();
		request.setRequestStr(msg);
		Response response = new Response();
		response.setResponseStr("response");
		FilterChain fc = new FilterChain();
		fc.addFilter(new HTMLFilter())
		  .addFilter(new SesitiveFilter())
		  ;
		
		fc.doFilter(request, response, fc);
		System.out.println(request.getRequestStr());
		System.out.println(response.getResponseStr());
	}

}


package com.bjsxt.dp.filter;

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

public class FilterChain implements Filter {
	List<Filter> filters = new ArrayList<Filter>();
	int index = 0;
	
	public FilterChain addFilter(Filter f) {
		this.filters.add(f);
		return this;
	}
	
	@Override
	public void doFilter(Request request, Response response, FilterChain chain) {
		if(index == filters.size()) return ;
		
		Filter f = filters.get(index);
		index ++;
		f.doFilter(request, response, chain);
	}
}


package com.bjsxt.dp.filter;

public class HTMLFilter implements Filter {

	

	@Override
	public void doFilter(Request request, Response response, FilterChain chain) {
		//process the html tag <>
		request.requestStr = request.requestStr.replace('<', '[')
				   .replace('>', ']') + "---HTMLFilter()";
		chain.doFilter(request, response, chain);
		response.responseStr += "---HTMLFilter()";
	}

}

package com.bjsxt.dp.filter;

public class SesitiveFilter implements Filter {

	

	@Override
	public void doFilter(Request request, Response response, FilterChain chain) {
		request.requestStr = request.requestStr.replace("被就业", "就业")
		 .replace("敏感", "") + "---SesitiveFilter()";
		chain.doFilter(request, response, chain);
		response.responseStr += "---SesitiveFilter()";
	
	}
	
	

}

要想让我们的Filter可以处理返回信息,很自然的想到让一个Filter去调用另外一个Filter,等到最后一个Filter返回,其余的也逐个返回,这里要完成这一过程,增加了

一个参数FilterChain,FilterChain里面记录了当前过滤器索引,巧妙的完成了一个Filter调用另外一个Filter的需求

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值