责任链模式

责任链模式中主要有两个接口 Filter 和FilterChain ,多个Filter组成了一个FilterChain,

而一个整体的FilterChain 又可以看成是一个Filter,可以很方便的在一个已有的FilterChain中添加一个FilterChain

interface Filter {
   
	public void doFilter (Request request , Response response , FilterChain fc);
}



FilterChain ,doFilter方法调用下个filter
import java.util.ArrayList;
import java.util.List;

//FilterChain 实现Filter则可以实现很方便的  在已有的FilterChain中在添加一个FilterChain
public class FilterChain implements Filter {

	private List<Filter> query = new ArrayList<Filter>();
	private int index = 0; // 用于指明当前掉用到的filter

	@Override
	public void doFilter(Request request, Response response, FilterChain fc) {
		//递归调用下一个Filter
		if (index == query.size())
			return;
		Filter f = query.get(index);
		index++;
		f.doFilter(request, response, fc);
	}

	public void addFilter(Filter f) {
		query.add(f);
	}

}



MyFilter
  public class MyFilter implements Filter {

	@Override
	public void doFilter(Request request, Response response, FilterChain fc) {
             		System.out.println(request.getRequestStr()+"--MyFilter");
             		fc.doFilter(request, response, fc);
             		System.out.println(response.getResponseStr()+"--MyFilter");
	}

}

你可能感兴趣的:(java,F#)