当前位置:编程学习 > JAVA >>

模板方法模式(TemplateMethod)

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

 

UML示例

 

\

 

代码示例

 

[java] 
package com.pattern; 
 
public abstract class TemplateMethod { 
     
    public static final String S1="method1"; 
    public static final String S2="method2"; 
     
    /**
     * 模板方法
     * @param methodName
     */ 
    public final void Method(String methodName) 
    { 
        if(S1.equals(methodName)) 
        { 
            Method1(); 
        }else if(S2.equals(methodName)) 
        { 
            Method2(); 
        } 
    } 
     
    protected abstract void Method1(); 
     
    protected abstract void Method2(); 
     

package com.pattern;

public abstract class TemplateMethod {
 
 public static final String S1="method1";
 public static final String S2="method2";
 
 /**
  * 模板方法
  * @param methodName
  */
 public final void Method(String methodName)
 {
  if(S1.equals(methodName))
  {
   Method1();
  }else if(S2.equals(methodName))
  {
   Method2();
  }
 }
 
 protected abstract void Method1();
 
 protected abstract void Method2();
 
}

 

[java]
package com.pattern; 
 
/**
 * 具体实现
 * @author jialin
 *
 */ 
public class Concrete extends TemplateMethod { 
 
    protected void Method1() { 
        System.out.println("Method1>>>>"); 
    } 
 
    protected void Method2() { 
        System.out.println("Method2>>>>"); 
    } 
     
 

package com.pattern;

/**
 * 具体实现
 * @author jialin
 *
 */
public class Concrete extends TemplateMethod {

 protected void Method1() {
  System.out.println("Method1>>>>");
 }

 protected void Method2() {
  System.out.println("Method2>>>>");
 }
 

}

客户端

[java]
package com.pattern; 
 
public class Client { 
    public static void main(String[] args) 
    { 
        Concrete con=new Concrete(); 
        //con.Method("method1");  
        con.Method("method2"); 
    } 

package com.pattern;

public class Client {
 public static void main(String[] args)
 {
  Concrete con=new Concrete();
  //con.Method("method1");
  con.Method("method2");
 }
}

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

 

看一下它的源码

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

[java] 
public abstract class HttpServlet extends GenericServlet { 
 
 
    private static final String METHOD_DELETE = "DELETE"; 
    private static final String METHOD_GET = "GET"; 
    private static final String METHOD_POST = "POST"; 
 
     
    /**
     * Does nothing, because this is an abstract class.
     */ 
    public HttpServlet() { 
        // NOOP  
    } 
     
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException 
    { 
        String protocol = req.getProtocol(); 
        String msg = lStrings.getString("http.method_get_not_supported"); 
        if (protocol.endsWith("1.1")) { 
            resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); 
        } else { 
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); 
        } 
    } 
 
 
 
    protected void doHead(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException { 
 
        NoBodyResponse response = new NoBodyResponse(resp); 
 
        doGet(req, response); 
        response.setContentLength(); 
    } 
 
 
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException { 
 
        String protocol = req.getProtocol(); 
        String msg = lStrings.getString("http.method_post_not_supported"); 
        if (protocol.endsWith("1.1")) { 
            resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); 
        } else { 
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); 
        } 补充:软件开发 , Java ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,