KEMBAR78
Template Method Pattern | PPT
Template Method Pattern High Level Algorithm Monis Iqbal Snr. Software Engineer Creative Chaos (pvt.) Limited
Definition Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. <GOF>
Different implementations of algorithms leading to different algorithm altogether. (Similar code maintained differently) Every class requires full knowledge of the algorithm. Code duplication (Yuc!!)
Algorithm encapsulated in single class Single control/entry point. Subclasses only need to provide specialized algorithm details.
 
Primitive Methods Abstract class declares primitive methods leaving the subclasses to provide functionality. Primitive methods used in  Template Method . Nicely documented and meaningfully named.
Concrete methods Used in Template method Defined in abstract class Declared final
Hooks Hook placed in the Template method. Abstract class provides implementation. Subclasses can “hook into” the algorithm if they want.
abstract class AbstractClass { final void templateMethod() { primitiveOperation1(); primitiveOperation2(); concreteOperation(); if (hookIsTrue()) hook(); } abstract void primitiveOperation1(); abstract void primitiveOperation2(); void final concreteOperation() { … .. } boolean hookIsTrue() { return false; } void hook() {} }
Cache revisited Example by Code First in First Out (FIFO) Cache Least Recently Used (LRU) Cache
Intuition Encapsulation of Algorithm Don’t call us, we’ll call you. (Hollywood Principle).
Questions ???

Template Method Pattern

  • 1.
    Template Method PatternHigh Level Algorithm Monis Iqbal Snr. Software Engineer Creative Chaos (pvt.) Limited
  • 2.
    Definition Define theskeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. <GOF>
  • 3.
    Different implementations ofalgorithms leading to different algorithm altogether. (Similar code maintained differently) Every class requires full knowledge of the algorithm. Code duplication (Yuc!!)
  • 4.
    Algorithm encapsulated insingle class Single control/entry point. Subclasses only need to provide specialized algorithm details.
  • 5.
  • 6.
    Primitive Methods Abstractclass declares primitive methods leaving the subclasses to provide functionality. Primitive methods used in Template Method . Nicely documented and meaningfully named.
  • 7.
    Concrete methods Usedin Template method Defined in abstract class Declared final
  • 8.
    Hooks Hook placedin the Template method. Abstract class provides implementation. Subclasses can “hook into” the algorithm if they want.
  • 9.
    abstract class AbstractClass{ final void templateMethod() { primitiveOperation1(); primitiveOperation2(); concreteOperation(); if (hookIsTrue()) hook(); } abstract void primitiveOperation1(); abstract void primitiveOperation2(); void final concreteOperation() { … .. } boolean hookIsTrue() { return false; } void hook() {} }
  • 10.
    Cache revisited Exampleby Code First in First Out (FIFO) Cache Least Recently Used (LRU) Cache
  • 11.
    Intuition Encapsulation ofAlgorithm Don’t call us, we’ll call you. (Hollywood Principle).
  • 12.