JAVA責任チェーンモード-フィルタ原理

5847 ワード

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であることを考えてみましょう.そのため、Filter Chainを使用してFilterチェーンをシミュレートし、Filterインタフェースを実現しました.
このように私たちのFilterchainは1つの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が戻り情報を処理できるようにするには、1つのFilterに別のFilterを呼び出すことを自然に考えて、最後のFilterが戻るまで待って、残りも1つずつ返して、ここでこの過程を完成して、増加しました.
1つのパラメータFilterChain、FilterChainの中に現在のフィルタインデックスを記録して、巧みに1つのFilterが別のFilterを呼び出す需要を完成しました