标签:长度 ons string 顺序 out rgs 多次 print use
public abstract class Handler { /** * 持有后继的责任对象 */ protected Handler successor; /** * 示意处理请求的方法,虽然这个示意方法是没有传入参数的 * 但实际是可以传入参数的,根据具体需要来选择是否传递参数 */ public abstract void handleRequest(); /** * 取值方法 */ public Handler getSuccessor() { return successor; } /** * 赋值方法,设置后继的责任对象 */ public void setSuccessor(Handler successor) { this.successor = successor; } }
public class ConcreteHandler extends Handler { /** * 处理方法,调用此方法处理请求 */ @Override public void handleRequest() { /** * 判断是否有后继的责任对象 * 如果有,就转发请求给后继的责任对象 * 如果没有,则处理请求 */ if(getSuccessor() != null) { System.out.println("放过请求"); getSuccessor().handleRequest(); }else { System.out.println("处理请求"); } } }
public class Client { public static void main(String[] args) { //组装责任链 Handler handler1 = new ConcreteHandler(); Handler handler2 = new ConcreteHandler(); handler1.setSuccessor(handler2); //提交请求 handler1.handleRequest(); } }
public abstract class Handler { /** * 持有下一个处理请求的对象 */ protected Handler successor = null; /** * 取值方法 */ public Handler getSuccessor() { return successor; } /** * 设置下一个处理请求的对象 */ public void setSuccessor(Handler successor) { this.successor = successor; } /** * 处理聚餐费用的申请 * @param user 申请人 * @param fee 申请的钱数 * @return 成功或失败的具体通知 */ public abstract String handleFeeRequest(String user , double fee); }
public class ProjectManager extends Handler { @Override public String handleFeeRequest(String user, double fee) { String str = ""; //项目经理权限比较小,只能在500以内 if(fee < 500) { //为了测试,简单点,只同意张三的请求 if("张三".equals(user)) { str = "成功:项目经理同意【" + user + "】的聚餐费用,金额为" + fee + "元"; }else { //其他人一律不同意 str = "失败:项目经理不同意【" + user + "】的聚餐费用,金额为" + fee + "元"; } }else { //超过500,继续传递给级别更高的人处理 if(getSuccessor() != null) { return getSuccessor().handleFeeRequest(user, fee); } } return str; } } public class DeptManager extends Handler { @Override public String handleFeeRequest(String user, double fee) { String str = ""; //部门经理的权限只能在1000以内 if(fee < 1000) { //为了测试,简单点,只同意张三的请求 if("张三".equals(user)) { str = "成功:部门经理同意【" + user + "】的聚餐费用,金额为" + fee + "元"; }else { //其他人一律不同意 str = "失败:部门经理不同意【" + user + "】的聚餐费用,金额为" + fee + "元"; } }else { //超过1000,继续传递给级别更高的人处理 if(getSuccessor() != null) { return getSuccessor().handleFeeRequest(user, fee); } } return str; } } public class GeneralManager extends Handler { @Override public String handleFeeRequest(String user, double fee) { String str = ""; //总经理的权限很大,只要请求到了这里,他都可以处理 if(fee >= 1000) { //为了测试,简单点,只同意张三的请求 if("张三".equals(user)) { str = "成功:总经理同意【" + user + "】的聚餐费用,金额为" + fee + "元"; }else { //其他人一律不同意 str = "失败:总经理不同意【" + user + "】的聚餐费用,金额为" + fee + "元"; } }else { //如果还有后继的处理对象,继续传递 if(getSuccessor() != null) { return getSuccessor().handleFeeRequest(user, fee); } } return str; } }
public class Client { public static void main(String[] args) { //先要组装责任链 Handler h1 = new GeneralManager(); Handler h2 = new DeptManager(); Handler h3 = new ProjectManager(); h3.setSuccessor(h2); h2.setSuccessor(h1); //开始测试 String test1 = h3.handleFeeRequest("张三", 300); System.out.println("test1 = " + test1); String test2 = h3.handleFeeRequest("李四", 300); System.out.println("test2 = " + test2); System.out.println("---------------------------------------"); String test3 = h3.handleFeeRequest("张三", 700); System.out.println("test3 = " + test3); String test4 = h3.handleFeeRequest("李四", 700); System.out.println("test4 = " + test4); System.out.println("---------------------------------------"); String test5 = h3.handleFeeRequest("张三", 1500); System.out.println("test5 = " + test5); String test6 = h3.handleFeeRequest("李四", 1500); System.out.println("test6 = " + test6); } }
public class TestFilter implements Filter{ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(request, response); } public void destroy() { } public void init(FilterConfig filterConfig) throws ServletException { } }
/** * Filters. */ private ApplicationFilterConfig[] filters = new ApplicationFilterConfig[0];
/** * Implementation of a <code>javax.servlet.FilterConfig</code> useful in * managing the filter instances instantiated when a web application * is first started. * * @author Craig R. McClanahan * @version $Id: ApplicationFilterConfig.java 1201569 2011-11-14 01:36:07Z kkolinko $ */
private ApplicationFilterConfig[] filters = new ApplicationFilterConfig[0];
/** * The int which gives the current number of filters in the chain. */ private int n = 0; public static final int INCREMENT = 10;
void addFilter(ApplicationFilterConfig filterConfig) { // Prevent the same filter being added multiple times for(ApplicationFilterConfig filter:filters) if(filter==filterConfig) return; if (n == filters.length) { ApplicationFilterConfig[] newFilters = new ApplicationFilterConfig[n + INCREMENT]; System.arraycopy(filters, 0, newFilters, 0, n); filters = newFilters; } filters[n++] = filterConfig; }
1 public ApplicationFilterChain createFilterChain 2 (ServletRequest request, Wrapper wrapper, Servlet servlet) { 3 4 // get the dispatcher type 5 DispatcherType dispatcher = null; 6 if (request.getAttribute(DISPATCHER_TYPE_ATTR) != null) { 7 dispatcher = (DispatcherType) request.getAttribute(DISPATCHER_TYPE_ATTR); 8 } 9 String requestPath = null; 10 Object attribute = request.getAttribute(DISPATCHER_REQUEST_PATH_ATTR); 11 12 if (attribute != null){ 13 requestPath = attribute.toString(); 14 } 15 16 // If there is no servlet to execute, return null 17 if (servlet == null) 18 return (null); 19 20 boolean comet = false; 21 22 // Create and initialize a filter chain object 23 ApplicationFilterChain filterChain = null; 24 if (request instanceof Request) { 25 Request req = (Request) request; 26 comet = req.isComet(); 27 if (Globals.IS_SECURITY_ENABLED) { 28 // Security: Do not recycle 29 filterChain = new ApplicationFilterChain(); 30 if (comet) { 31 req.setFilterChain(filterChain); 32 } 33 } else { 34 filterChain = (ApplicationFilterChain) req.getFilterChain(); 35 if (filterChain == null) { 36 filterChain = new ApplicationFilterChain(); 37 req.setFilterChain(filterChain); 38 } 39 } 40 } else { 41 // Request dispatcher in use 42 filterChain = new ApplicationFilterChain(); 43 } 44 45 filterChain.setServlet(servlet); 46 47 filterChain.setSupport 48 (((StandardWrapper)wrapper).getInstanceSupport()); 49 50 // Acquire the filter mappings for this Context 51 StandardContext context = (StandardContext) wrapper.getParent(); 52 FilterMap filterMaps[] = context.findFilterMaps(); 53 54 // If there are no filter mappings, we are done 55 if ((filterMaps == null) || (filterMaps.length == 0)) 56 return (filterChain); 57 58 // Acquire the information we will need to match filter mappings 59 String servletName = wrapper.getName(); 60 61 // Add the relevant path-mapped filters to this filter chain 62 for (int i = 0; i < filterMaps.length; i++) { 63 if (!matchDispatcher(filterMaps[i] ,dispatcher)) { 64 continue; 65 } 66 if (!matchFiltersURL(filterMaps[i], requestPath)) 67 continue; 68 ApplicationFilterConfig filterConfig = (ApplicationFilterConfig) 69 context.findFilterConfig(filterMaps[i].getFilterName()); 70 if (filterConfig == null) { 71 // FIXME - log configuration problem 72 continue; 73 } 74 boolean isCometFilter = false; 75 if (comet) { 76 try { 77 isCometFilter = filterConfig.getFilter() instanceof CometFilter; 78 } catch (Exception e) { 79 // Note: The try catch is there because getFilter has a lot of 80 // declared exceptions. However, the filter is allocated much 81 // earlier 82 Throwable t = ExceptionUtils.unwrapInvocationTargetException(e); 83 ExceptionUtils.handleThrowable(t); 84 } 85 if (isCometFilter) { 86 filterChain.addFilter(filterConfig); 87 } 88 } else { 89 filterChain.addFilter(filterConfig); 90 } 91 } 92 93 // Add filters that match on servlet name second 94 for (int i = 0; i < filterMaps.length; i++) { 95 if (!matchDispatcher(filterMaps[i] ,dispatcher)) { 96 continue; 97 } 98 if (!matchFiltersServlet(filterMaps[i], servletName)) 99 continue; 100 ApplicationFilterConfig filterConfig = (ApplicationFilterConfig) 101 context.findFilterConfig(filterMaps[i].getFilterName()); 102 if (filterConfig == null) { 103 // FIXME - log configuration problem 104 continue; 105 } 106 boolean isCometFilter = false; 107 if (comet) { 108 try { 109 isCometFilter = filterConfig.getFilter() instanceof CometFilter; 110 } catch (Exception e) { 111 // Note: The try catch is there because getFilter has a lot of 112 // declared exceptions. However, the filter is allocated much 113 // earlier 114 } 115 if (isCometFilter) { 116 filterChain.addFilter(filterConfig); 117 } 118 } else { 119 filterChain.addFilter(filterConfig); 120 } 121 } 122 123 // Return the completed filter chain 124 return (filterChain); 125 126 }
public final void invoke(Request request, Response response) throws IOException, ServletException { ...省略中间代码 // Create the filter chain for this request ApplicationFilterFactory factory = ApplicationFilterFactory.getInstance(); ApplicationFilterChain filterChain = factory.createFilterChain(request, wrapper, servlet); ...省略中间代码 filterChain.doFilter(request.getRequest(), response.getResponse()); ...省略中间代码 }
// Call the next filter if there is one if (pos < n) { //拿到下一个Filter,将指针向下移动一位 //pos它来标识当前ApplicationFilterChain(当前过滤器链)执行到哪个过滤器 ApplicationFilterConfig filterConfig = filters[pos++]; Filter filter = null; try { //获取当前指向的Filter的实例 filter = filterConfig.getFilter(); support.fireInstanceEvent(InstanceEvent.BEFORE_FILTER_EVENT, filter, request, response); if (request.isAsyncSupported() && "false".equalsIgnoreCase( filterConfig.getFilterDef().getAsyncSupported())) { request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR, Boolean.FALSE); } if( Globals.IS_SECURITY_ENABLED ) { final ServletRequest req = request; final ServletResponse res = response; Principal principal = ((HttpServletRequest) req).getUserPrincipal(); Object[] args = new Object[]{req, res, this}; SecurityUtil.doAsPrivilege ("doFilter", filter, classType, args, principal); } else { //调用Filter的doFilter()方法 filter.doFilter(request, response, this); }
设计模式(13)--Chain of Responsibility(责任链模式)--行为型
标签:长度 ons string 顺序 out rgs 多次 print use
原文地址:http://www.cnblogs.com/yysbolg/p/7461440.html