Template What!? - PowerPoint PPT Presentation

About This Presentation
Title:

Template What!?

Description:

To implement invariant parts of an algorithm once then let the ... Bulldog. eat() nap() bark() walkBull() Greyhound. eat() nap() bark() walkGrey() Rottweiler ... – PowerPoint PPT presentation

Number of Views:155
Avg rating:3.0/5.0
Slides: 10
Provided by: brittanb
Category:

less

Transcript and Presenter's Notes

Title: Template What!?


1
Template What ?!
2
The Template Method
  • The Point
  • To implement invariant parts of an algorithm once
    then let the subclasses implement the behavior
    that can vary.
  • How this is done
  • The template method contains the skeleton of an
    algorithm, allowing subclasses to carry out
    specific steps.
  • This is done without changing the algorithms
    structure.

3
When to Use Template Method
  • When there is common behavior among subclasses
    that should be localized in one class to prevent
    code duplication.
  • Template methods are a fundamental technique for
    code re-use.
  • To direct subclass extensions.

4
Trivial Example
Bulldog eat() nap() bark() walkBull()
Greyhound eat() nap() bark() walkGrey()
Rottweiler eat() nap() bark() walkRott()
5
Trivial Example
Bulldog lumbers()
Greyhound prances()
6
Slightly Less Trivial Example
7
Slightly Less Trivial Example
Jason litigation()
Pam patents()
8
Results of the Template Method
  • Code reusability, that is, the subclass reuses
    the code of the base class.
  • Prevents duplication of common behaviors
    (especially important in class libraries).
  • Leads to inverted control structure where the
    parent class calls the operations of a subclass.

9
Any Questions ?
Write a Comment
User Comments (0)
About PowerShow.com