欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

模板方法模式(TemplateMethod)

系統 1943 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條評論
主站蜘蛛池模板: 2021国产精品一区二区在线 | 亚洲图片欧洲电影 | 亚洲免费三区 | 亚洲国产七七久久桃花 | 日韩国产无矿砖一线二线图 | 高清久久 | 日本毛片高清免费视频 | 日本久草视频 | 日韩视频专区 | 婷婷成人免费视频 | 欧美黄色片在线观看 | 五月婷婷综合在线视频 | 亚洲我不卡 | 色综合国产 | 亚洲国产精品久久久久网站 | 日韩国产欧美一区二区三区 | 欧美另类视频一区二区三区 | 亚洲欧美激情精品一区二区 | 先锋影音av最新资源 | 免费在线亚洲视频 | 精品久久久久久国产 | 欧美精品二区 | 亚洲a网| 免费看91| 欧美啊啊啊 | 欧美一级视频在线观看欧美 | 婷婷丁香色综合图亚洲 | 在线免费观看毛片 | 看亚洲a级一级毛片 | 日本高清免费h色视频在线观看 | 国产精品亚洲一区二区三区在线 | 91在线视屏 | 1024app成人无限观看 | 四虎影院新地址 | 日韩久久久久久 | 久色一区 | 不卡久久 | 国产女人与拘做受视频 | 日本黄色一级片视频 | 国产精品手机在线观看 | 黄视频免费在线观看 |