类型:行为类模式
类图:
事实上,模版方法是编程中一个经常用到的模式。先来看一个例子,某日,程序员A拿到一个任务:给定一个整数数组,把数组中的数由小到大排序,然后把排序之后的结果打印出来。经过分析之后,这个任务大体上可分为两部分,排序和打印,打印功能好实现,排序就有点麻烦了。但是A有办法,先把打印功能完成,排序功能另找人做。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
abstract <span class="hljs-default-class" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 132px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">class</span> <span class="hljs-default-title" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 79px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">AbstractSort</span> {</span> <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 194px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">/** * 将数组array由小到大排序 * @param array */</span> <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 271px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 60px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">protected</span> abstract <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">void</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">sort</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 86px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 19px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span>[] <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>)</span></span>; <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 257px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">public</span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 27px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">void</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 93px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">showSortResult</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 85px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span>[] <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>)</span></span>{ <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">this</span>.sort(<span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>); System.out.print(<span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 73px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">"排序结果:"</span>); <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 19px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">for</span> (<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 19px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span> i = <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span>; i < <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>.length; i++){ System.out.<span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 39px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">printf</span>(<span class="hljs-default-string" style="color: #98c379; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #98c379; font-weight: 400; font-style: normal;">"%3s"</span>, <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>[i]); } } } |
写完后,A找到刚毕业入职不久的同事B说:有个任务,主要逻辑我已经写好了,你把剩下的逻辑实现一下吧。于是把AbstractSort类给B,让B写实现。B拿过来一看,太简单了,10分钟搞定,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<span class="hljs-default-class" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 270px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">class</span> <span class="hljs-default-title" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 80px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">ConcreteSort</span> <span class="hljs-default-title" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 46px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">extends</span> <span class="hljs-default-title" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 79px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">AbstractSort</span> {</span> @<span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 264px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">Override <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 59px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">protected</span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">void</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">sort</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 86px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 19px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span>[] <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>)</span></span>{ <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">for</span>(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span> i=<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span>; i<<span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>.length<span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 13px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">-1</span>; i++){ selectSort(<span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>, i); } } <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 316px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 46px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">private</span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 27px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">void</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 66px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">selectSort</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 159px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span>[] <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>, <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 19px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span> index)</span> </span>{ <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span> MinValue = <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">32767</span>; <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 93px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">// 最小值变量 </span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span> indexMin = <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span>; <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 117px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">// 最小值索引变量 </span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span> Temp; <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 81px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">// 暂存变量 </span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">for</span> (<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span> i = index; i < <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>.length; i++) { <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 14px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">if</span> (<span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>[i] < MinValue){ <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 93px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">// 找到最小值 </span> MinValue = <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>[i]; <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 93px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">// 储存最小值 </span> indexMin = i; } } Temp = <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>[index]; <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 92px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">// 交换两数值 </span> <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>[index] = <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>[indexMin]; <span class="hljs-default-built_in" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">array</span>[indexMin] = Temp; } } |
写好后交给A,A拿来一运行:
1 2 3 4 5 6 7 8 |
<span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 39px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">public</span> <span class="hljs-default-class" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 92px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 33px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">class</span> <span class="hljs-default-title" style="color: #e6c07b; background: rgba(0, 0, 0, 0); display: inline; width: 39px; text-decoration: none solid #e6c07b; font-weight: 400; font-style: normal;">Client</span> {</span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">public</span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">static</span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 19px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">int</span>[] a = { <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 13px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">10</span>, <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 13px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">32</span>, <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">1</span>, <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">9</span>, <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">5</span>, <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 6px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">7</span>, <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 13px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">12</span>, <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 7px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">0</span>, <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 6px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">4</span>, <span class="hljs-default-number" style="color: #d19a66; background: rgba(0, 0, 0, 0); display: inline; width: 6px; text-decoration: none solid #d19a66; font-weight: 400; font-style: normal;">3</span> }; <span class="hljs-default-comment" style="color: #5c6370; background: rgba(0, 0, 0, 0); display: inline; width: 105px; text-decoration: none solid #5c6370; font-weight: 400; font-style: italic;">// 预设数据数组 </span> <span class="hljs-default-function" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 251px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;"><span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">public</span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 40px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">static</span> <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">void</span> <span class="hljs-default-title" style="color: #61aeee; background: rgba(0, 0, 0, 0); display: inline; width: 26px; text-decoration: none solid #61aeee; font-weight: 400; font-style: normal;">main</span><span class="hljs-default-params" style="color: #abb2bf; background: rgba(0, 0, 0, 0); display: inline; width: 99px; text-decoration: none solid #abb2bf; font-weight: 400; font-style: normal;">(String[] args)</span></span>{ AbstractSort s = <span class="hljs-default-keyword" style="color: #c678dd; background: rgba(0, 0, 0, 0); display: inline; width: 20px; text-decoration: none solid #c678dd; font-weight: 400; font-style: normal;">new</span> ConcreteSort(); s.showSortResult(a); } } |
运行结果:
排序结果: 0 1 3 4 5 7 9 10 12 32
运行正常。行了,任务完成。没错,这就是模版方法模式。大部分刚步入职场的毕业生应该都有类似B的经历。一个复杂的任务,由公司中的牛人们将主要的逻辑写好,然后把那些看上去比较简单的方法写成抽象的,交给其他的同事去开发。这种分工方式在编程人员水平层次比较明显的公司中经常用到。比如一个项目组,有架构师,高级工程师,初级工程师,则一般由架构师使用大量的接口、抽象类将整个系统的逻辑串起来,实现的编码则根据难度的不同分别交给高级工程师和初级工程师来完成。怎么样,是不是用到过模版方法模式?
模版方法模式的结构
模版方法模式由一个抽象类和一个(或一组)实现类通过继承结构组成,抽象类中的方法分为三种:
抽象方法:父类中只声明但不加以实现,而是定义好规范,然后由它的子类去实现。
模版方法:由抽象类声明并加以实现。一般来说,模版方法调用抽象方法来完成主要的逻辑功能,并且,模版方法大多会定义为final类型,指明主要的逻辑功能在子类中不能被重写。
钩子方法:由抽象类声明并加以实现。但是子类可以去扩展,子类可以通过扩展钩子方法来影响模版方法的逻辑。
抽象类的任务是搭建逻辑的框架,通常由经验丰富的人员编写,因为抽象类的好坏直接决定了程序是否稳定性。
实现类用来实现细节。抽象类中的模版方法正是通过实现类扩展的方法来完成业务逻辑。只要实现类中的扩展方法通过了单元测试,在模版方法正确的前提下,整体功能一般不会出现大的错误。
模版方法的优点及适用场景
容易扩展。一般来说,抽象类中的模版方法是不易反生改变的部分,而抽象方法是容易反生变化的部分,因此通过增加实现类一般可以很容易实现功能的扩展,符合开闭原则。
便于维护。对于模版方法模式来说,正是由于他们的主要逻辑相同,才使用了模版方法,假如不使用模版方法,任由这些相同的代码散乱的分布在不同的类中,维护起来是非常不方便的。
比较灵活。因为有钩子方法,因此,子类的实现也可以影响父类中主逻辑的运行。但是,在灵活的同时,由于子类影响到了父类,违反了里氏替换原则,也会给程序带来风险。这就对抽象类的设计有了更高的要求。
在多个子类拥有相同的方法,并且这些方法逻辑相同时,可以考虑使用模版方法模式。在程序的主框架相同,细节不同的场合下,也比较适合使用这种模式。
链接:http://blog.csdn.net/zhengzhb/article/details/7405608
数据科学与编程 » 23种设计模式(12)-模版方法模式