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
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- ??
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
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)
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.
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.
Joined: Jan 19, 2004
Thanks everyone for the inputs. That was very helpful.
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