wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Template Method pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Template Method pattern" Watch "Template Method pattern" New topic
Author

Template Method pattern

Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Can someone help me understand the advantages and disadvantages of the Template Method pattern?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Certainly! What is your current understanding?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
-pro: minimizes duplication
-con: typical implementation tightly couples classes to superclass


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
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

Joined: Jan 29, 2003
Posts: 8791
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)


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Srikanth Shenoy
author
Ranch Hand

Joined: Jan 24, 2004
Posts: 184
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.


Srikanth Shenoy
Author of Struts Survival Guide : Basics to Best Practices
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jan 19, 2004
Posts: 125
Thanks everyone for the inputs. That was very helpful.
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Template Method pattern
 
Similar Threads
Why Abstract Class required?
Banning concrete inheritance
when to use abstract class and when to use an interface
force a class to use a method
Is this a design pattern?