定义:
定义一个操作中算法的骨架,而将一些步骤延迟到子类中,使得子类可以不改变算法的结构即可重定义该算法中的某些特定步骤。这里的算法的结构,可以理解为你根据需求设计出来的业务流程。特定的步骤就是指那些可能在内容上存在变数的环节。
开闭原则是指一个软件实体应该对扩展开放,对修改关闭。也就是说软件实体必须是在不被修改的情况下被扩展。模板方法模式意图是由抽象父类控制*逻辑,并把基本操作的实现推迟到子类去实现,这是通过继承的手段来达到对象的复用,同时也遵守了开闭原则!
类图:
模板方法模式需要开发抽象类和具体子类的设计师之间的协作。一个设计师负责给出一个算法的轮廓和骨架,另一些设计师则负责给出这个算法的各个逻辑步骤。代表这些具体逻辑步骤的方法称做基本方法(primitive method);而将这些基本方法汇总起来的方法叫做模板方法(template method),这个设计模式的名字就是从此而来。
模板方法所代表的行为称为*行为,其逻辑称为*逻辑。模板方法模式的静态结构图如下所示:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOcAAAERCAIAAAAPKifHAAAZqklEQVR4nO2dMZKsNheFJ5qqP5tNUPXCCV9A4NApO3CVtkDwNkB5A8odsAIih4ROyF1sAIcOCBzqD071rfskQUM3Glrd54t6GLW4SKelK7jovjlCcuPtbAMI2Q1VS/KDqiX5QdWS/KBqSX5QtSQ/dqj28/PzjZCj+fz8TKjatzcOzOR4btAVVUtOhqol+UHVkvygakl+ULUkP15FteM4zvMsf07T1Pf9eeZ8NeM4tm17thWH8RKqnaapKAqR6TzPVVU1TXOqUT7DMJRl2TRNURR1XRtjDrFwnuemaaqqKsvy/toehJdQbdu2ZVnWdS1HmqbZpQn93aPo+34YBvnTWovfVVEUOG6MOepcTdNsVK1n1WPyEqrFuKW7bZdq53mOdvk0TXdapfUhDoyo9ob6tRek2a5az6rH5CVUW1UVnAQ50jSNMcYYU5ZlVVVyvK7rqqown8qAV1XV29sbjo/jiArLsizL0lqLD1AY5mJd0jk3jqMcRJ1t21ZVVRQFzu55nKJaATZIybZti6JADXIJ4ziWZYmS3kW5mGqlTim5ZFV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iO1R6PCnQoA1xHWMpN60TNhYLJ8kGKIYUANeqyVWCovWjlUEjrSq187qXKwbVux2XMZo8MqAhg2tJnT70GssBTBLQe1FsPITDFyV4jCg/AQHoJMUjq4xF06ZilYDi8IeO8RoHv0BxdEP8pAGFVtGDOp+1vEuhJLuqTaY9/xCiO4o60HPNUuRW/mwmOp1pMRCiwFJpdlaYwxxuiIbLi2ErgtJSWCu1Ev82xUbfQFGx2k58k06gzIuzcH4kVwR1tPzNaqlXhLR9VerT3FWOvUoALVSiypVm3UvERjrX4lbqnMIXgR3LvGWqr2xtqjLyFqz0w7u2FgsnYxoZLhgjfW6mWcdi7DoPKoarVfq1/ARFXRW2xhZPr2O8cyV1wlGsEtZ9RXGvq18qe3Gs6C01SrV8F64sbqHkt7Xd4LTMa7ABCTVNX3PY6M44gCUokEhntm6KByLKtxCl2/u6wXvUhn1Nn3vRcZ7ZybpklLYZcsdnnA3hVFWw8W4vLFfgTIt22Ldttu3iPAOIRUyDtFh7+QSKhakh9ULckPqpbkB1VL8oOqJfmRVrUfHx+3pjkhZJGPj4+EqiXkQaBqSX5QtSQ/qFqSH1QtyQ+qluQHVUvyg6ol+UHVkvygakl+ULUkP6hakh//B49cHz6aLmUaAAAAAElFTkSuQmCC" alt="" width="317" height="375" />
这里涉及到两个角色:
抽象模板(Abstract Template)角色有如下责任:
■ 定义了一个或多个抽象操作,以便让子类实现。这些抽象操作叫做基本操作,它们是一个*逻辑的组成步骤。
■ 定义并实现了一个模板方法。这个模板方法一般是一个具体方法,它给出了一个*逻辑的骨架,而逻辑的组成步骤在相应的抽象操作中,推迟到子类实现。*逻辑也有可能调用一些具体方法
具体模板(Concrete Template)角色又如下责任:
■ 实现父类所定义的一个或多个抽象方法,它们是一个*逻辑的组成步骤。
■ 每一个抽象模板角色都可以有任意多个具体模板角色与之对应,而每一个具体模板角色都可以给出这些抽象方法的不同实现,从而使得*逻辑的实现各不相同。
源代码
抽象模板角色类,abstractMethod()、hookMethod()等基本方法是*逻辑的组成步骤,这个*逻辑由templateMethod()方法代表。
public abstract class AbstractTemplate {
/**
* 模板方法
*/
public void templateMethod(){
//调用基本方法
abstractMethod();
hookMethod();
concreteMethod();
}
/**
* 基本方法的声明(由子类实现)
*/
protected abstract void abstractMethod();
/**
* 基本方法(空方法)
*/
protected void hookMethod(){}
/**
* 基本方法(已经实现)
*/
private final void concreteMethod(){
//业务相关的代码
}
}
具体模板角色类,实现了父类所声明的基本方法,abstractMethod()方法所代表的就是强制子类实现的剩余逻辑,而hookMethod()方法是可选择实现的逻辑,不是必须实现的。
public class ConcreteTemplate extends AbstractTemplate{
//基本方法的实现
@Override
public void abstractMethod() {
//业务相关的代码
}
//重写父类的方法
@Override
public void hookMethod() {
//业务相关的代码
}
}
模板方法模式中的方法
模板方法中的方法可以分为两大类:模板方法和基本方法。
模板方法
一个模板方法是定义在抽象类中的,把基本操作方法组合在一起形成一个总算法或一个总行为的方法。
一个抽象类可以有任意多个模板方法,而不限于一个。每一个模板方法都可以调用任意多个具体方法。
(是骨架,一般是具体方法,实现对基本方法的调度,完成固定的逻辑,为了防止恶意操作,一般模板方法会加上final关键字,不允许被覆写)。
基本方法
基本方法定义了一个或多个抽象操作,以便让子类实现。
基本方法又可以分为三种:抽象方法(Abstract Method)、具体方法(Concrete Method)和钩子方法(Hook Method)。
● 抽象方法:一个抽象方法由抽象类声明,由具体子类实现。在Java语言里抽象方法以abstract关键字标示。
● 具体方法:一个具体方法由抽象类声明并实现,而子类并不实现或置换。
● 钩子方法:一个钩子方法由抽象类声明并实现,而子类会加以扩展。通常抽象类给出的实现是一个空实现,作为方法的默认实现。
在上面的例子中,AbstractTemplate是一个抽象类,它带有三个方法。其中abstractMethod()是一个抽象方法,它由抽象类声明为抽象方法,并由子类实现;hookMethod()是一个钩子方法,它由抽象类声明并提供默认实现,并且由子类置换掉。concreteMethod()是一个具体方法,它由抽象类声明并实现。
默认钩子方法
一个钩子方法常常由抽象类给出一个空实现作为此方法的默认实现。这种空的钩子方法叫做“Do Nothing Hook”。显然,这种默认钩子方法在缺省适配模式里面已经见过了,一个缺省适配模式讲的是一个类为一个接口提供一个默认的空实现,从而使得缺省适配类的子类不必像实现接口那样必须给出所有方法的实现,因为通常一个具体类并不需要所有的方法。
命名规则
命名规则是设计师之间赖以沟通的管道之一,使用恰当的命名规则可以帮助不同设计师之间的沟通。
钩子方法的名字应当以do开始,这是熟悉设计模式的Java开发人员的标准做法。在上面的例子中,钩子方法hookMethod()应当以do开头;在HttpServlet类中,也遵从这一命名规则,如doGet()、doPost()等方法。
模板方法模式在Servlet中的应用
使用过Servlet的人都清楚,除了要在web.xml做相应的配置外,还需继承一个叫HttpServlet的抽象类。HttpService类提供了一个service()方法,这个方法调用doPost()、doGet()、doPut()、doDelete()、doHead()……七个do方法中的一个或几个,完成对客户端调用的响应。这些do方法在HttpServlet抽象类中都是以空实现的钩子方法存在的,需要由具体子类提供,因此这是典型的模板方法模式。
下面给出一个简单的Servlet例子:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUsAAAG6CAIAAAAOG/N1AAAgAElEQVR4nO2dL6/tuNnFL7rSy+6XiDTwwAsCBpaGFY4U0g8QUDYo6heIVDggoDioMLAkUuEoqCwDCwIu9AuWztPnPHZyvHf2PzvrB4728XYc7yTL/2Ivf3GEkHz58uwMEELuCBVOSM5Q4YTkDBVOSM48TuFvb29fCCFxvL293UR3j1P4ly9sLxASy630QoUT8opQ4YTkDBVOSM5Q4YTkDBVOSM5Q4WG6rnvAWR7DNE1d19V1fasE53m+YWr7TNM0TdO6rubzcfq+b9t2nudpmm6S4Lqu0zQ1TfNSDw8VHqDrui9fvszzfO8TPQY8dmVZ3jDBoii2vg0q8GpZIuc4XH8+kqZzrqqqYRimaarr+lZXBgovy7Jt28j4NznvPlR4mGzkDdq2jXyO+76PqdO2FL4si/98BwMj0Tk3vyLYjojJf9d1TdPIv1VVXZe3IPEKf0w7iAq3oNnW970ORPvQ/ItSwHxlWn0o1xFiYm4xTdOyLMuy+I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NVVdD5pbYQJsEdYmwh1nWtqgoFAX4y+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()。
参考来自:
http://www.cnblogs.com/java-my-life/archive/2012/05/14/2495235.html
http://blog.163.com/java_player@126/blog/static/127930738200981555022398/
http://blog.csdn.net/zhangliangzi/article/details/51475133
java_设计模式_模板方法模式_Template Method Pattern(2016-08-11)的更多相关文章
-
乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern)
原文:乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 模板方法模式(Template Method ...
-
二十四种设计模式:模板方法模式(Template Method Pattern)
模板方法模式(Template Method Pattern) 介绍定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.Template Method使得子类可以不改变一个算法的结构即可重定义该算法 ...
-
设计模式 - 模板方法模式(template method pattern) JFrame 具体解释
模板方法模式(template method pattern) JFrame 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(templ ...
-
设计模式 - 模板方法模式(template method pattern) 排序(sort) 具体解释
模板方法模式(template method pattern) 排序(sort) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(tem ...
-
设计模式 - 模板方法模式(template method pattern) 具体解释
模板方法模式(template method pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 模板方法模式(template metho ...
-
模板方法模式(Template Method Pattern)——复杂流程步骤的设计
模式概述 在现实生活中,很多事情都包含几个实现步骤,例如请客吃饭,无论吃什么,一般都包含点单.吃东西.买单等几个步骤,通常情况下这几个步骤的次序是:点单 --> 吃东西 --> 买单. 在 ...
-
设计模式(九): 从醋溜土豆丝和清炒苦瓜中来学习";模板方法模式";(Template Method Pattern)
今天是五.四青年节,祝大家节日快乐.看着今天这标题就有食欲,夏天到了,醋溜土豆丝和清炒苦瓜适合夏天吃,好吃不上火.这两道菜大部分人都应该吃过,特别是醋溜土豆丝,作为“鲁菜”的代表作之一更是为大众所熟知 ...
-
java_设计模式_工厂模式_Factory Pattern(2016-08-04)
工厂模式主要是为创建对象提供了接口.工厂模式按照<Java与模式>中的提法分为三类: (1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory ...
-
模板方法模式(Tempalte Method Pattern)
模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式实现,然后声明一些抽象方法来迫使子类实现剩余的逻辑.不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有 ...
随机推荐
-
IPv4分析
IPv4的头部格式: 1. Version 版本号,默认是4. 2. IHL(Internet Header Length) 就是IPv4头部长度.这个长度的单位是32bit,一般是5,那么头部的长度 ...
-
Android开发/源代码资源汇总
全网最全的Android游戏源代码汇总 mod=viewthread&tid=5080&fromuid=5" target="_blank">htt ...
-
Activity的onSaveInstanceState()和onRestoreInstanceState()以及API详解
为了弄清楚onSaveInstanceState()方法和onRestoreInstanceState()方法,我翻译一下谷歌的API,翻译如下: There are a few scenarios ...
-
MySQL中information_schema数据库的内容
大家在安装或使用MYSQL时,会发现除了自己安装的数据库以外,还有一个information_schema数据库. information_schema数据库是做什么用的呢,使用WordPress博客 ...
-
Redis 桌面管理器
使用Redis桌面管理器,可以方便开发人员进行开发测试,对Redis存储内容进行可视化管理. 下载安装:https://redisdesktop.com/download 1. 为了方便测试,打开re ...
-
java使用redis数据库
1.安装 Redis 支持 32 位和 64 位,根据实际情况选择不同的安装版本. 安装完成后打开命令提示窗口,切换到redis安装目录,启动redis客户端, redis-cli命令启动redis客 ...
-
mysql导出导入数据
使用sql语句导出数据: 导出时如果不写绝对路径,会提示The MySQL server is running with the --secure-file-priv option so it can ...
-
SpringBoot拦截器
在实际开发中,总存在着这样的场景,比如拦截请求的ip地址,或者在所有的请求都返回相同的数据,如果每一个方法都写出相同数据固然可以实现,但是随着项目的变大,重复的代码会越来越多,所以在这种情况我们可以用 ...
-
Qt Creater中Clang-format的使用
起因在于习惯性的想格式化代码,发现Qt Creater默认居然是没有代码格式化的,只有一个缩进,搞毛线啊!!! 搜索了下,倒是很容易就搜到了,Qt Creater中有个插件:beautifier,在 ...
-
Java-Runoob-高级教程-实例-环境设置实例:3.Java 实例 - 如何执行指定class文件目录(classpath)?
ylbtech-Java-Runoob-高级教程-实例-环境设置实例:3.Java 实例 - 如何执行指定class文件目录(classpath)? 1.返回顶部 1. Java 实例 - 如何执行指 ...