一、定义
定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定步骤。
二、模板方法模式结构图
三、模式的实现
public abstract class AbstractClass {
public abstract void primitiveOperation1();
public abstract void primitiveOperation2();
public void TemplateMethod() {
primitiveOperation1();
primitiveOperation2();
...;
}
}
public class ConcreteClass extends AbstractClass {
@Override
public void primitiveOperation1() {
System.out.println("primitiveOperation1");
}
@Override
public void primitiveOperation2() {
System.out.println("primitiveOperation2");
}
}
public static void main(String[] args) {
AbstractClass abstractClass = new ConcreteClass();
abstractClass.TemplateMethod();
}
四、使用的案例
linkedHashMap和HashMap的区别之一就是:
LinkedHashMap也是一个HashMap,但是内部维持了一个双向链表,可以保持顺序,也就是说大多数逻辑(骨架)都是相似的,只是LinkedHashMap附加了一个维持双向链表的逻辑(特定实现)。
下面拿插入操作分析,直接上代码
/**
* 整个方法就是插入流程的算法骨架
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
// 具体逻辑1 链表中存在相同的key,改变链表顺序
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
//具体逻辑2 插入之后更新链表
afterNodeInsertion(evict);
return null;
}
而维持链表的操作HashMap中都是没有实现的(不需要)。
void afterNodeAccess(Node<K,V> p) { }
void afterNodeInsertion(boolean evict) { }
void afterNodeRemoval(Node<K,V> p) { }
具体实现是在LinkedHashMap
void afterNodeAccess(Node<K,V> e) { // move node to last
LinkedHashMap.Entry<K,V> last;
if (accessOrder && (last = tail) != e) {
LinkedHashMap.Entry<K,V> p =
(LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
p.after = null;
if (b == null)
head = a;
else
b.after = a;
if (a != null)
a.before = b;
else
last = b;
if (last == null)
head = p;
else {
p.before = last;
last.after = p;
}
tail = p;
++modCount;
}
}
void afterNodeInsertion(boolean evict) { // possibly remove eldest
LinkedHashMap.Entry<K,V> first;
if (evict && (first = head) != null && removeEldestEntry(first)) {
K key = first.key;
removeNode(hash(key), key, null, false, true);
}
}
五、模板方法模式的特点
1.模板方法模式是通过不变行为搬移到超类,去除子类的重复代码来体现它的优势。
2.模板方法模式就是提供了一个很好的代码复用平台。
3.通过模板方法可以帮助子类拜托重复的不变行为的纠缠。