一果港、定義
定義一個(gè)操作中的算法的骨架辛掠,而將一些步驟延遲到子類中萝衩。模板方法使得子類可以不改變一個(gè)算法的結(jié)構(gòu)即可重新定義該算法的某些特定步驟猩谊。
二牌捷、模板方法模式結(jié)構(gòu)圖
結(jié)構(gòu)圖
三、模式的實(shí)現(xiàn)
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的區(qū)別之一就是:
LinkedHashMap也是一個(gè)HashMap,但是內(nèi)部維持了一個(gè)雙向鏈表,可以保持順序寄月,也就是說大多數(shù)邏輯(骨架)都是相似的无牵,只是LinkedHashMap附加了一個(gè)維持雙向鏈表的邏輯(特定實(shí)現(xiàn))合敦。
下面拿插入操作分析,直接上代碼
/**
* 整個(gè)方法就是插入流程的算法骨架
*
* @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中都是沒有實(shí)現(xiàn)的(不需要)场躯。
void afterNodeAccess(Node<K,V> p) { }
void afterNodeInsertion(boolean evict) { }
void afterNodeRemoval(Node<K,V> p) { }
具體實(shí)現(xiàn)是在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);
}
}
五、模板方法模式的特點(diǎn)
1.模板方法模式是通過不變行為搬移到超類签舞,去除子類的重復(fù)代碼來體現(xiàn)它的優(yōu)勢(shì)儒搭。
2.模板方法模式就是提供了一個(gè)很好的代碼復(fù)用平臺(tái)搂鲫。
3.通過模板方法可以幫助子類拜托重復(fù)的不變行為的糾纏魂仍。