责任链模式(Chain of Responsibility)的目标是使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止。
在处理用户的请求时可能要根据不同的情况对请求添加不同的处理逻辑,在这时候就可以利用责任链进行设计。当需要添加一个处理逻辑时可以很方便的添加一个处理的节点。
现在我们的需求是处理用户的请求,将用户提交的字符串信息进行层层处理,同时在处理完成之后返回结果时,也要对返回的字符串进行层层处理,而处理返回的情况时其处理的顺序和先前是正好相反的顺序。
首先建立用户的请求和接收对象Request和Response:
package com.lcq.filter;
public class Request {
String requestStr;
public String getRequestStr() {
return requestStr;
}
public void setRequestStr(String requestStr) {
this.requestStr = requestStr;
}
}
package com.lcq.filter;
public class Response {
String responseStr;
public String getResponseStr() {
return responseStr;
}
public void setResponseStr(String responseStr) {
this.responseStr = responseStr;
}
}
package com.lcq.filter;
public interface Filter {
public void doFilter(Request request, Response response,FilterChain chain);
}
package com.lcq.filter;
import java.util.ArrayList;
import java.util.List;
public class FilterChain implements Filter {
List filters = new ArrayList();
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);
}
}
下面我们编写三个过滤器:
HTMLFilter类:
package com.lcq.filter;
/**
* 过滤HTML中的脚本元素
* @author lcq
*
*/
public class HTMLFilter implements Filter {
@Override
public void doFilter(Request request, Response response,FilterChain chain) {
request.requestStr = request.getRequestStr().replace("<", "[")
.replace(">", "] --------HTMLFilter");
chain.doFilter(request, response, chain);
response.responseStr += "--------HTMLFilter";
}
}
package com.lcq.filter;
public class SesitiveFilter implements Filter {
@Override
public void doFilter(Request request, Response response, FilterChain chain) {
request.requestStr = request.getRequestStr().replace("敏感", " ")
.replace("猫猫", "haha------SesitiveFilter");
chain.doFilter(request, response, chain);
response.responseStr += "------SesitiveFilter";
}
}
package com.lcq.filter;
public class FaceFilter implements Filter {
@Override
public void doFilter(Request request, Response response, FilterChain chain) {
request.requestStr = request.getRequestStr().replace(":)",
"^V^-------FaceFilter");
chain.doFilter(request, response, chain);
response.responseStr += "-------FaceFilter";
}
}
package com.lcq.filter;
public class Main {
public static void main(String[] args) {
String message = "敏感词汇,重庆,