问题
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.
回答1:
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.
回答2:
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.
回答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.
Template Design Pattern: The specialization is done by subclassing. (This is not the approach in JdbcTemplate.
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.
来源:https://stackoverflow.com/questions/33153252/why-is-jdbctemplate-an-example-of-the-template-method-design-pattern