亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

模板方法模式(TemplateMethod)

系統 1877 0

模板方法模式,定義一個操作中的算法的骨架,而將一些步驟延遲到子類中實現,使得子類可以不改變一個算法的結構即可重定義該算法的某些特定步驟。

UML示例

模板方法模式(TemplateMethod)

代碼示例

    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;

/**
 * 具體實現
 * @author jialin
 *
 */
public class Concrete extends TemplateMethod {

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

	protected void Method2() {
		System.out.println("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的部分代碼

    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);
        }
    }


   
    protected void doPut(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_put_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 doDelete(HttpServletRequest req,
                            HttpServletResponse resp)
        throws ServletException, IOException {

        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_delete_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 service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        String method = req.getMethod();

        if (method.equals(METHOD_GET)) {
            long lastModified = getLastModified(req);
            if (lastModified == -1) {
                // servlet doesn't support if-modified-since, no reason
                // to go through further expensive logic
                doGet(req, resp);
            } else {
                long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
                if (ifModifiedSince < (lastModified / 1000 * 1000)) {
                    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                } else {
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                }
            }

        } else if (method.equals(METHOD_HEAD)) {
            long lastModified = getLastModified(req);
            maybeSetLastModified(resp, lastModified);
            doHead(req, resp);

        } else if (method.equals(METHOD_POST)) {
            doPost(req, resp);
            
        } else if (method.equals(METHOD_PUT)) {
            doPut(req, resp);        
            
        } else if (method.equals(METHOD_DELETE)) {
            doDelete(req, resp);
            
        } else if (method.equals(METHOD_OPTIONS)) {
            doOptions(req,resp);
            
        } else if (method.equals(METHOD_TRACE)) {
            doTrace(req,resp);
            
        } else {
            //
            // Note that this means NO servlet supports whatever
            // method was requested, anywhere on this server.
            //

            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[1];
            errArgs[0] = method;
            errMsg = MessageFormat.format(errMsg, errArgs);
            
            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
        }
    }

}

  


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

模板方法模式(TemplateMethod)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲成年网 | 一级毛片国产 | 精品视频一区二区三区 | 久久久91精品国产一区二区三区 | 狠狠干中文字幕 | 欧美亚洲另类色国产综合 | 免费不卡毛片 | 欧美精品在线一区二区三区 | 国产精品视频一区二区三区经 | 操片免费| 久久无码精品一区二区三区 | 97欧美在线看欧美视频免费 | 天天综合网天天综合色不卡 | 久久99国产一区二区三区 | 一级片一级毛片 | 精品国产一区二区三区www | 99av在线播放 | 91精品国产美女福到在线不卡 | 中文字幕精品一区二区精品 | 精品国产一区二区三区四区色 | 99热久久国产精品这里有99 | 天天骑天天干 | 亚洲一级毛片在线观播放 | 在线播放国产视频 | 亚洲综合一区二区三区四区 | 男女啪啪网站 | 久久国产精品免费看 | 在线观看 亚洲 | 天天干夜夜夜 | 91精品国产麻豆国产自产在线 | 天天干天天做天天操 | 95视频在线观看在线分类h片 | 91激情视频 | 性做久久久久久久久男女 | 久久青草社区 | 亚洲欧洲国产精品 | 250pp久久新| 九九九精品午夜在线观看 | 中文字幕日韩精品中文区 | 免费久久精品视频 | 亚洲国产一区在线二区三区 |