//过滤器接口: Filter
package com.bjsxt.chain;
public interface Filter {
public void doFilter(Request request, Response response);
}
//实现类1 : Fillter1
package com.bjsxt.chain;
public class Filter1 implements Filter {
public void doFilter(Request request, Response response) {
String str = request.getRequestParameter();
str += " pass filter1 ";
request.setRequestParameter(str);
}
}
//实现类2
package com.bjsxt.chain;
public class Filter2 implements Filter {
public void doFilter(Request request, Response response) {
String str = request.getRequestParameter();
str += " pass filter2 ";
request.setRequestParameter(str);
}
}
//过滤链
import java.util.ArrayList;
import java.util.List;
public class Filterchain implements Filter {
List<Filter> filters = new ArrayList<Filter>();
public Filterchain addFilter(Filter filter) {
filters.add(filter);
return this;
}
public void doFilter(Request request, Response response) {
for (Filter f : filters) {
f.doFilter(request, response);
}
}
}
//模拟request
package com.bjsxt.chain;
public class Request {
private String requestParameter = "";
public String getRequestParameter() {
return requestParameter;
}
public void setRequestParameter(String requestParameter) {
this.requestParameter = requestParameter;
}
}
//模拟response
package com.bjsxt.chain;
public class Response {
private String responseParameter;
public String getResponseParameter() {
return responseParameter;
}
public void setResponseParameter(String responseParameter) {
this.responseParameter = responseParameter;
}
}
//测试主类
package com.bjsxt.chain;
public class MainClass {
public static void main(String[] args) {
Request request = new Request();
Response response = new Response();
Filterchain chain1 = new Filterchain();
chain1.addFilter(new Filter1());
Filterchain chain2 = new Filterchain();
chain2.addFilter(new Filter2());
chain1.addFilter(chain2);
chain1.doFilter(request, response);
System.out.println(request.getRequestParameter());
}
}
//改进后的过滤器
//过滤器接口类
package com.bjsxt.chain;
public interface Filter {
public void doFilter(Request request, Response response, FilterChain filterChain);
}
//过滤器实现类1
package com.bjsxt.chain;
public class Filter1 implements Filter {
public void doFilter(Request request, Response response, FilterChain filterChain) {
String str = request.getRequestParameter();
str += "request pass filter1 ";
request.setRequestParameter(str);
filterChain.doFilter(request, response, filterChain);
str = response.getResponseParameter();
str += "response pass filter1";
response.setResponseParameter(str);
}
}
//过滤器实现类2
package com.bjsxt.chain;
public class Filter2 implements Filter {
public void doFilter(Request request, Response response, FilterChain filterChain) {
String str = request.getRequestParameter();
str += "request pass filter2 ";
request.setRequestParameter(str);
filterChain.doFilter(request, response, filterChain);
str = response.getResponseParameter();
str += "response pass filter2";
response.setResponseParameter(str);
}
}
过滤器链
package com.bjsxt.chain;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class FilterChain implements Filter {
List<Filter> filters = new ArrayList<Filter>();
Stack<Filter> stack = new Stack<Filter>();
private int i = 0;
public FilterChain addFilter(Filter filter) {
filters.add(filter);
return this;
}
public void doFilter(Request request, Response response, FilterChain filterChain) {
if (i == filters.size()) return;
Filter filter = filters.get(i);
i++;
filter.doFilter(request, response, filterChain);
}
}
//测试主类
package com.bjsxt.chain;
public class MainClass {
public static void main(String[] args) {
Request request = new Request();
Response response = new Response();
FilterChain chain1 = new FilterChain();
chain1.addFilter(new Filter1())
.addFilter(new Filter2());
chain1.doFilter(request, response, chain1);
System.out.println(request.getRequestParameter());
System.out.println(response.getResponseParameter());
}
java责任链模式
最新推荐文章于 2024-08-23 15:54:50 发布