Servlet中应用到责任链模式,下面是仿照实现的代码,请享用!
package org.design.behavioral.chain.v4; import java.util.ArrayList; import java.util.List; /* 需求: 处理request,response 请求过来的时候是HtmlFilter处理、SensitiveFilter处理、ScriptFilter处理。 响应返回的时候是ScriptFilter处理、SensitiveFilter处理、HtmlFilter处理。 */ public class ServletFilter { public static void main(String[] args) { Request request = new Request(); request.requestStr = "I am request "; Response response = new Response(); response.responseStr = "I am response "; FilterChain filterChain = new FilterChain(); filterChain.add(new HtmlFilter()).add(new SensitiveFilter()).add(new ScriptFilter()); filterChain.doFilter(request, response); System.out.println(request.requestStr); System.out.println(response.responseStr); } } class Request { String requestStr; } class Response { String responseStr; } interface Filter { boolean doFilter(Request request, Response response, FilterChain filterChain); } class HtmlFilter implements Filter { @Override public boolean doFilter(Request request, Response response, FilterChain filterChain) { request.requestStr = request.requestStr + " | HtmlFilter deal!"; filterChain.doFilter(request, response); response.responseStr = response.responseStr + " | HtmlFilter deal!"; return true; } } class SensitiveFilter implements Filter { @Override public boolean doFilter(Request request, Response response, FilterChain filterChain) { request.requestStr = request.requestStr + " | SensitiveFilter deal!"; filterChain.doFilter(request, response); response.responseStr = response.responseStr + " | SensitiveFilter deal!"; return true; } } class ScriptFilter implements Filter { @Override public boolean doFilter(Request request, Response response, FilterChain filterChain) { request.requestStr = request.requestStr + " | ScriptFilter deal!"; filterChain.doFilter(request, response); response.responseStr = response.responseStr + " | ScriptFilter deal!"; return true; } } class FilterChain { List<Filter> filters = new ArrayList<Filter>(); int index = 0; FilterChain add(Filter filter) { this.filters.add(filter); return this; } public boolean doFilter(Request request, Response response) { if (index == filters.size()) return false; Filter f = filters.get(index); index++; f.doFilter(request, response, this); return true; } }
感谢阅读到现在,请在留言区提出宝贵的意见!
更多精彩内容,关注微信公众号:技术严选