模板方法模式是类的行为模式。准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式实现,然后声明一些抽象方法来迫使子类实现剩余的逻辑。不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有不同的实现。这就是模板方法模式的用意。

模板方法模式中的方法

  模板方法中的方法可以分为两大类:模板方法和基本方法。

  模板方法

  一个模板方法是定义在抽象类中的,把基本操作方法组合在一起形成一个总算法或一个总行为的方法。

  一个抽象类可以有任意多个模板方法,而不限于一个。每一个模板方法都可以调用任意多个具体方法。

  基本方法

  基本方法又可以分为三种:抽象方法(Abstract Method)、具体方法(Concrete Method)和钩子方法(Hook Method)。

  ●  抽象方法:一个抽象方法由抽象类声明,由具体子类实现。在Java语言里抽象方法以abstract关键字标示。

  ●  具体方法:一个具体方法由抽象类声明并实现,而子类并不实现或置换。

  ●  钩子方法:一个钩子方法由抽象类声明并实现,而子类会加以扩展。通常抽象类给出的实现是一个空实现,作为方法的默认实现。

默认钩子方法

  一个钩子方法常常由抽象类给出一个空实现作为此方法的默认实现。这种空的钩子方法叫做“Do Nothing Hook”。显然,这种默认钩子方法在缺省适配模式里面已经见过了,一个缺省适配模式讲的是一个类为一个接口提供一个默认的空实现,从而使得缺省适配类的子类不必像实现接口那样必须给出所有方法的实现,因为通常一个具体类并不需要所有的方法。

命名规则

  命名规则是设计师之间赖以沟通的管道之一,使用恰当的命名规则可以帮助不同设计师之间的沟通。

  钩子方法的名字应当以do开始,这是熟悉设计模式的Java开发人员的标准做法。在上面的例子中,钩子方法hookMethod()应当以do开头;在HttpServlet类中,也遵从这一命名规则,如doGet()、doPost()等方法。

模板方法模式在Servlet中的应用

  使用过Servlet的人都清楚,除了要在web.xml做相应的配置外,还需继承一个叫HttpServlet的抽象类。HttpService类提供了一个service()方法,这个方法调用七个do方法中的一个或几个,完成对客户端调用的响应。这些do方法需要由HttpServlet的具体子类提供,因此这是典型的模板方法模式。下面是service()方法的源代码:

    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例子:

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUsAAAG6CAIAAAAOG/N1AAAgAElEQVR4nO2dL6/tuNnFL7rSy+6XiDTwwAsCBpaGFY4U0g8QUDYo6heIVDggoDioMLAkUuEoqCwDCwIu9AuWztPnPHZyvHf2PzvrB4728XYc7yTL/2Ivf3GEkHz58uwMEELuCBVOSM5Q4YTkDBVOSM48TuFvb29fCCFxvL293UR3j1P4ly9sLxASy630QoUT8opQ4YTkDBVOSM5Q4YTkDBVOSM5Q4WG6rnvAWR7DNE1d19V1fasE53m+YWr7TNM0TdO6rubzcfq+b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Y9JciUfhT8eNLZvO8BGyG2hwgnJGSqckJyhwgnJGSqckJyhwm8MXq3hhdyz80JIXgr/1AFimqZpmjBB9R5vkvTr66ZpdtaxPwbaQpAkFX61A8QwDLLOrGma62wbIm0h1nWVcz0F2kIQl6jCr3aAMHXXddM5420hYmxkngUVfhLyUXiMA4RRuFmt4Vd3fuBFthBmLeoOtIUgdyIHhcc7QGgddl2nW+wYG9Ptahw+TVNVVdD5pbYQJsEdYmwh1nWtqgoFAX4y+gu0hSA7pKTw4w4Q0GfTNLoTrvvP4zhKESDiNAZP8bYQKB0O/GiLrKKZ51nyTFsIskNKCpfPV68PD7alt1ZoYvGm39CNt4W4x5A1zi6Nfy4aJ/tQ4ZsKB2g/6858vC2EXuN5K9BgofELiSRJhV/tABFUeLCVrofozFHxthDxKoq3hRjHEQMBwbPQFoIYklS4Id4Bous6GaDSQOS6E44uNEbajMFDpC3ERevG420hnPeSj7YQZIccFA4u8m+I5zpbiMj9la4jOOWGthAkSD4KfxHQ2D64KRoht4IKJyRnqHBCcoYKJyRnqHBCciYZhX/79u0LIeRCvn37dlB6rFoJyRkqnJCcocIJyRkqnJCcocIJyRkqnJCcocIJyRkqnJCcocIJyRkqnJCcocIJyRkqnJCcocIJyRkqnJCcocIJyZn/B5nGp/pAvJzjAAAAAElFTkSuQmCC" alt="" />

  从上面的类图可以看出,TestServlet类是HttpServlet类的子类,并且置换掉了父类的两个方法:doGet()和doPost()。

public class TestServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { System.out.println("using the GET method"); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { System.out.println("using the POST method");
} }

  从上面的例子可以看出这是一个典型的模板方法模式。

  HttpServlet担任抽象模板角色

    模板方法:由service()方法担任。

    基本方法:由doPost()、doGet()等方法担任。

  TestServlet担任具体模板角色

    TestServlet置换掉了父类HttpServlet中七个基本方法中的其中两个,分别是doGet()和doPost()。

java设计模式之模版方法模式以及在java中作用的更多相关文章

  1. JAVA设计模式之模版方法模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述模板方法(Template Method)模式的: 模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式 ...

  2. Java设计模式系列-工厂方法模式

    原创文章,转载请标注出处:<Java设计模式系列-工厂方法模式> 一.概述 工厂,就是生产产品的地方. 在Java设计模式中使用工厂的概念,那就是生成对象的地方了. 本来直接就能创建的对象 ...

  3. Java设计模式之工厂方法模式(转) 实现是抽象工厂?

    Java设计模式之工厂方法模式 责任编辑:覃里作者:Java研究组织   2009-02-25   来源:IT168网站   文本Tag: 设计模式 Java [IT168 技术文章]         ...

  4. JS常用的设计模式(10)——模版方法模式

    模式方法是预先定义一组算法,先把算法的不变部分抽象到父类,再将另外一些可变的步骤延迟到子类去实现.听起来有点像工厂模式( 非前面说过的简单工厂模式 ). 最大的区别是,工厂模式的意图是根据子类的实现最 ...

  5. 设计模式 笔记 模版方法模式 Template Method

    //---------------------------15/04/28---------------------------- //TemplateMethod 模版方法模式----类行为型模式 ...

  6. 设计模式之模版方法模式(Template Method Pattern)

    一.什么是模版方法模式? 首先,模版方法模式是用来封装算法骨架的,也就是算法流程 既然被称为模版,那么它肯定允许扩展类套用这个模版,为了应对变化,那么它也一定允许扩展类做一些改变 事实就是这样,模版方 ...

  7. java设计模式(二)---工厂方法模式

    2普通工厂方法模式 就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建. 2.1创建接口 /** * 发送接口 * Created by mrf on 2016/2/25. */ public ...

  8. Java设计模式 之 工厂方法模式

    1. 使用设计模式的好处:可提高代码的重复性,让代码更容易被他人理解,保证代码的可靠性. 2. 工厂模式定义:就是创建一个工厂类来创建你需要的类,工厂模式包括工厂模式和抽象工厂模式,抽象工厂模式是工厂 ...

  9. JAVA设计模式之工厂方法模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述工厂方法模式的: 工厂方法模式是类的创建模式,又叫做虚拟构造子(Virtual Constructor)模式或者多态性工厂(Polymor ...

随机推荐

  1. mac os设置root密码

    不知怎么忘记mac os系统的root密码了 方法一: 首先,启动机器,启动时按住Apple和S键,以单用户模式(single user mode)进入系统.输入:mount -uw / 然后回车.输 ...

  2. 【Python】[函数] 调用与定义

    1.数据类型转换 int(),float(),str(),bool('1')2.定义函数使用 def关键字,依次写出 函数名.括号.括号中的参数.冒号,然后在缩进体中写函数内容 例子 求绝对值的函数 ...

  3. 面向生产环境的大集群模式安装Hadoop

    一.实验说明 1.本实验将使用DNS而不是hosts文件解析主机名: 2.使用NFS共享密钥文件,而不是逐个手工拷贝添加密钥: 3.复制Hadoop时使用批量拷贝脚本而不是逐台复制. 测试环境: Ho ...

  4. js插件动态加载js、css解决方案

    最近因为工作需要做了一个js自动导入的插件,一开始很天真的以为动态创建个script添加到head中就ok了,试了之后才发现了问题,就是如果同时引入了多个js文件,而且后一个文件中用到了前一个文件中的 ...

  5. 使用openCV的静态库编译

    转载请注明出处: http://www.cnblogs.com/sysuzyq/p/6183568.html By 少侠阿朱 摘要: 本文主要讲述如何使用opencv静态库进行编译,生成脱离openc ...

  6. Repeater获取某一行TextBox值

    TextBox tb = (TextBox)e.Item.FindControl("TextBoxID");

  7. JS中call、apply的用法说明

    JS Call()与Apply()的区别 ECMAScript规范给所有函数都定义了Call()与apply()两个方法,call与apply的第一个参数都是需要调用的函数对象,在函数体内这个参数就是 ...

  8. org.gradle.api.internal.tasks.DefaultTaskInputs$TaskInputUnionFileCollection cannot be cast to org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection

    转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6709758.html Android Studio导入项目报错: org.gradle.api.inter ...

  9. msfvenom向apk注入payload

    首先安装apt-get install apkinjector 这个东西,msfvenom重新组装apk的时候会自动调用 msfvenom -x /路径/apk android/meterpreter ...

  10. SpringMVC框架学习笔记——各种异常、报错解决

    1.Target runtime com.genuitec.runtime.generic.jee60 is not defined. 找到导入项目的.setting文件夹org.eclipse.ws ...