I was reading about design patterns, in particular about the template method, when my attention was caught by this question on SO.
After reading the explanation and specific code I am still wondering why this is an example of the 'Template method' design pattern.
According to GoF, the intent of this pattern is:
“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.”
and has two participants:
AbstractClass:
- defines abstract primitive operations that concrete subclasses define to implement steps of an algorith
- Implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations definied in AbstractClass or those of other object.
ConcreteClass:
implements the primitive operations to carry out subclass-specific steps of the algorithm.
Why is the code in 'JdbcOperations' considered to be an 'Template method' design pattern?
- I dont see any 'global/general' algorithm being defined in the super/abstract class, even when I compare it with the code in a similar file like 'JmsTemplate'.
- None of the functions implemented in the concrete classes are also defined in the super class. All the defined methods are added through the use of the interfaces, in this case the interface 'JdbcOperations', and none actually override the ones in the parent.
I get the fact that it is extremely handy for eliminating boilerplate code. But why is this a template method and not just a nifty coding trick. To me it looks like it shares none of the characteristics that a template method has.
JdbcTemplate
is an example of the template pattern (as stated in the accepted answer in the linked question). – Eric