模板方法模式(TemplateMethod)

模板方法模式(TemplateMethod)

2993人阅读 评论(8) 收藏 举报
分类:

 

模板方法模式,定义一个操作中的算法的骨架,而将一些步骤延迟到子类中实现,使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。

 

UML示例

 

代码示例

 

  1. package com.pattern;  
  2.   
  3. public abstract class TemplateMethod {  
  4.       
  5.     public static final String S1="method1";  
  6.     public static final String S2="method2";  
  7.       
  8.     /** 
  9.      * 模板方法 
  10.      * @param methodName 
  11.      */  
  12.     public final void Method(String methodName)  
  13.     {  
  14.         if(S1.equals(methodName))  
  15.         {  
  16.             Method1();  
  17.         }else if(S2.equals(methodName))  
  18.         {  
  19.             Method2();  
  20.         }  
  21.     }  
  22.       
  23.     protected abstract void Method1();  
  24.       
  25.     protected abstract void Method2();  
  26.       
  27. }  


 

  1. package com.pattern;  
  2.   
  3. /** 
  4.  * 具体实现 
  5.  * @author jialin 
  6.  * 
  7.  */  
  8. public class Concrete extends TemplateMethod {  
  9.   
  10.     protected void Method1() {  
  11.         System.out.println("Method1>>>>");  
  12.     }  
  13.   
  14.     protected void Method2() {  
  15.         System.out.println("Method2>>>>");  
  16.     }  
  17.       
  18.   
  19. }  


客户端

  1. package com.pattern;  
  2.   
  3. public class Client {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         Concrete con=new Concrete();  
  7.         //con.Method("method1");  
  8.         con.Method("method2");  
  9.     }  
  10. }  


模板方法在Servlet中有一个典型的应用就是HttpServlet

 

看一下它的源码

把多余的代码去掉,这是HttpServlet的部分代码

  1. public abstract class HttpServlet extends GenericServlet {  
  2.   
  3.   
  4.     private static final String METHOD_DELETE = "DELETE";  
  5.     private static final String METHOD_GET = "GET";  
  6.     private static final String METHOD_POST = "POST";  
  7.   
  8.       
  9.     /** 
  10.      * Does nothing, because this is an abstract class. 
  11.      */  
  12.     public HttpServlet() {  
  13.         // NOOP  
  14.     }  
  15.       
  16.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  17.         throws ServletException, IOException  
  18.     {  
  19.         String protocol = req.getProtocol();  
  20.         String msg = lStrings.getString("http.method_get_not_supported");  
  21.         if (protocol.endsWith("1.1")) {  
  22.             resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);  
  23.         } else {  
  24.             resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);  
  25.         }  
  26.     }  
  27.   
  28.   
  29.   
  30.     protected void doHead(HttpServletRequest req, HttpServletResponse resp)  
  31.         throws ServletException, IOException {  
  32.   
  33.         NoBodyResponse response = new NoBodyResponse(resp);  
  34.   
  35.         doGet(req, response);  
  36.         response.setContentLength();  
  37.     }  
  38.   
  39.   
  40.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  41.         throws ServletException, IOException {  
  42.   
  43.         String protocol = req.getProtocol();  
  44.         String msg = lStrings.getString("http.method_post_not_supported");  
  45.         if (protocol.endsWith("1.1")) {  
  46.             resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);  
  47.         } else {  
  48.             resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);  
  49.         }  
  50.     }  
  51.   
  52.   
  53.      
  54.     protected void doPut(HttpServletRequest req, HttpServletResponse resp)  
  55.         throws ServletException, IOException {  
  56.   
  57.         String protocol = req.getProtocol();  
  58.         String msg = lStrings.getString("http.method_put_not_supported");  
  59.         if (protocol.endsWith("1.1")) {  
  60.             resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);  
  61.         } else {  
  62.             resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);  
  63.         }  
  64.     }  
  65.   
  66.   
  67.     
  68.     protected void doDelete(HttpServletRequest req,  
  69.                             HttpServletResponse resp)  
  70.         throws ServletException, IOException {  
  71.   
  72.         String protocol = req.getProtocol();  
  73.         String msg = lStrings.getString("http.method_delete_not_supported");  
  74.         if (protocol.endsWith("1.1")) {  
  75.             resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);  
  76.         } else {  
  77.             resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);  
  78.         }  
  79.     }  
  80.       
  81.     protected void service(HttpServletRequest req, HttpServletResponse resp)  
  82.         throws ServletException, IOException {  
  83.   
  84.         String method = req.getMethod();  
  85.   
  86.         if (method.equals(METHOD_GET)) {  
  87.             long lastModified = getLastModified(req);  
  88.             if (lastModified == -1) {  
  89.                 // servlet doesn't support if-modified-since, no reason  
  90.                 // to go through further expensive logic  
  91.                 doGet(req, resp);  
  92.             } else {  
  93.                 long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);  
  94.                 if (ifModifiedSince < (lastModified / 1000 * 1000)) {  
  95.                     // If the servlet mod time is later, call doGet()  
  96.                     // Round down to the nearest second for a proper compare  
  97.                     // A ifModifiedSince of -1 will always be less  
  98.                     maybeSetLastModified(resp, lastModified);  
  99.                     doGet(req, resp);  
  100.                 } else {  
  101.                     resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);  
  102.                 }  
  103.             }  
  104.   
  105.         } else if (method.equals(METHOD_HEAD)) {  
  106.             long lastModified = getLastModified(req);  
  107.             maybeSetLastModified(resp, lastModified);  
  108.             doHead(req, resp);  
  109.   
  110.         } else if (method.equals(METHOD_POST)) {  
  111.             doPost(req, resp);  
  112.               
  113.         } else if (method.equals(METHOD_PUT)) {  
  114.             doPut(req, resp);          
  115.               
  116.         } else if (method.equals(METHOD_DELETE)) {  
  117.             doDelete(req, resp);  
  118.               
  119.         } else if (method.equals(METHOD_OPTIONS)) {  
  120.             doOptions(req,resp);  
  121.               
  122.         } else if (method.equals(METHOD_TRACE)) {  
  123.             doTrace(req,resp);  
  124.               
  125.         } else {  
  126.             //  
  127.             // Note that this means NO servlet supports whatever  
  128.             // method was requested, anywhere on this server.  
  129.             //  
  130.   
  131.             String errMsg = lStrings.getString("http.method_not_implemented");  
  132.             Object[] errArgs = new Object[1];  
  133.             errArgs[0] = method;  
  134.             errMsg = MessageFormat.format(errMsg, errArgs);  
  135.               
  136.             resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);  
  137.         }  
  138.     }  
  139.   
  140. }  


其中Service方法就是典型的模板方法,我们写servlet的时候,一般要继承HttpServlet,重新DoGet,DoPost等方法,跟模板方法模式思路一致。

 

 

3
2
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值