Why is JdbcTemplate an example of the Template method design pattern
Asked Answered
A

3

5

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.

Airfield answered 15/10, 2015 at 16:0 Comment(3)
I think you are right to doubt that JdbcTemplate is an example of the template pattern (as stated in the accepted answer in the linked question).Eric
It wraps up common JDBC code (e.g., tx management) and defers to callbacks instead of a subclass--a distinction I don't find useful. It may not be a great example, but meh.Geodynamics
and don't forget to check out SpringTemplate which makes it easier to use Spring framework...Eric
U
3

Two design patterns in GoF represents the similar idea-

"Define the skeleton of an algorithm in an operation (Generic API), and delegating some specializing steps to the client.

  1. Template Design Pattern: The specialization is done by subclassing. (This is not the approach in JdbcTemplate.

  2. Strategy: The specialization is done by callback (delegation). (This is the approach in JdbcTemplate. We use callbacks like ResultSetExtractor which provides the specialization logic)

JdbcTemplate is more a strategy than template.

Uralaltaic answered 18/6, 2017 at 10:31 Comment(0)
N
2

I concur - JdbcTemplate isn't an example of template method design pattern. The design pattern used is callback.

Note that the goal and effect of both patterns is very similar, the main difference is that template method uses inheritance while callback uses composition (sort of) - see https://en.wikipedia.org/wiki/Composition_over_inheritance for why this might be preferred.

Niles answered 15/10, 2015 at 16:20 Comment(0)
P
1

As per the JdbcTemplate documentation

It simplifies the use of JDBC and helps to avoid common errors. It executes core JDBC workflow, leaving application code to provide SQL and extract results. This class executes SQL queries or updates, initiating iteration over ResultSets and catching JDBC exceptions and translating them to the generic, more informative exception hierarchy defined in the org.springframework.dao package.

JdbcTemplate provides abstraction to each of the method's internal working. For e.g. insert() or update() internally uses the implementation from concrete class for the chosen database. The client code doesn't have to know or implement these methods as they are implemented by database vendors. That is the reason it matches closely to Template design pattern.

Pilose answered 15/10, 2015 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.