Your SlideShare is downloading. ×
Template Method Pattern
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Template Method Pattern

1,402
views

Published on

Template Method Pattern (TMA)

Template Method Pattern (TMA)

Published in: Technology, Business

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,402
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
36
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Template Method Pattern High Level Algorithm Monis Iqbal Snr. Software Engineer Creative Chaos (pvt.) Limited
  • 2. 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>
  • 3.
    • Different implementations of algorithms leading to different algorithm altogether. (Similar code maintained differently)
    • Every class requires full knowledge of the algorithm.
    • Code duplication (Yuc!!)
  • 4.
    • Algorithm encapsulated in single class
    • Single control/entry point.
    • Subclasses only need to provide specialized algorithm details.
  • 5.  
  • 6. Primitive Methods
    • Abstract class declares primitive methods leaving the subclasses to provide functionality.
    • Primitive methods used in Template Method .
    • Nicely documented and meaningfully named.
  • 7. Concrete methods
    • Used in Template method
    • Defined in abstract class
    • Declared final
  • 8. Hooks
    • Hook placed in 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
    • Example by Code
      • First in First Out (FIFO) Cache
      • Least Recently Used (LRU) Cache
  • 11. Intuition
    • Encapsulation of Algorithm
    • Don’t call us, we’ll call you. (Hollywood Principle).
  • 12. Questions ???