Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Template Method pattern

 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me understand the advantages and disadvantages of the Template Method pattern?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Certainly! What is your current understanding?
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-pro: minimizes duplication
-con: typical implementation tightly couples classes to superclass
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is as follows -
Say I have a class A, in which I have written some algorithmic code.
Classes B and C extend class A. Now A provides only the basic structure for the code while the specification is implemented in the respective inherited classes without changing the basic structure.
So, i can actually say that the methods in A are pretty much final.
Advantages I see-
1. Provides flexibility
2. Reduces the number of redundant lines of code.
Disadvantages-
??
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've always been fuzzy ... does template apply to a framework class that provides basic functionality and occasionally calls methods that are empty in the base class so derived classes can optionally plug in customizations? (Deep breath)
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
I've always been fuzzy ... does template apply to a framework class that provides basic functionality and occasionally calls methods that are empty in the base class so derived classes can optionally plug in customizations? (Deep breath)
That's how I have understood it as well.
 
Srikanth Shenoy
author
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly.
The super-hyped IoC, Hollywood Principle - "Dont call us we will call you"
are all avatars of this Template method (or its variation - Strategy pattern).
Pros: Natural fit for building frameworks, so that parent framework classes can make callbacks into methods implemented in child.
Examples: Struts Action class.. The execute method is a call back from its parent Action.
Disadvantages: Restricts you to a single inheritance in Java.
Abstract parent methods are finie. However overriding parent functionality defeats encapsulation.
A better alternative is Strategy pattern.
Here the framework class delegates the handling to an interface. You have to only provide appropriate implementations of the strategy interface.
This is preferable in a lot of cases.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srikanth Shenoy:
Disadvantages: Restricts you to a single inheritance in Java.
Abstract parent methods are finie. However overriding parent functionality defeats encapsulation.

Another one: you can't change the behaviour during the lifetime of an object - it is defined at instanciation time by the concrete class you use.

A better alternative is Strategy pattern.

Strategy accomplishes the same using delegation instead of inheritance, yes. It is the better alternative when you can make use of the increased flexibility. In simple cases, it's already too complex, though.
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for the inputs. That was very helpful.
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's another thing to consider with the Template Method pattern, though. It seems to work exceptionally well when the implementation captured in the superclass is orthogonal to the implementation captured in the subclass. Here's an example of that...consider a WorkerThread class. You want subclasses to basically be able to override it so they can define runnables for your home grown thread pool:

This is a particularly neat usage of Template Method because the processing that removes and returns the worker thread to the thread pool is performed as part of the run() method, and it is completely and totally independent from whatever task the thread is meant to execute in its doRun() method.
sev
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic