为什么JdbcTemplate是Template方法设计模式的示例 [英] Why is JdbcTemplate an example of the Template method design pattern
问题描述
当我的注意力被 在阅读了解释和特定代码之后我仍然想知道为什么这是模板方法设计模式的示例。 根据GoF,此模式的目的是: 定义操作中算法的框架,将某些步骤推迟到子类。模板方法允许子类在不更改算法结构的情况下重新定义算法的某些步骤。 有两个参与者: AbstractClass : 为什么 JdbcOperations中的代码被视为模板方法设计模式? 我得到一个事实,它是非常便于消除 样板代码。但是,为什么这是一个模板方法,而不仅仅是一个巧妙的编码技巧。在我看来,它似乎没有任何模板方法具有的特征。 我同意- 请注意,两种模式的目标和效果非常相似,主要区别在于 template方法使用继承而 callback 使用合成(某种)-请参见 https://en.wikipedia.org/wiki/Composition_over_inheritance 为什么可能会更喜欢。 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:
Why is the code in 'JdbcOperations' considered to be an 'Template method' design pattern? 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. I concur - 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. 这篇关于为什么JdbcTemplate是Template方法设计模式的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-定义抽象的基本操作,具体的子类定义这些操作来实现算法的步骤
-实现模板方法,该方法定义算法的框架。模板方法调用原始操作以及在AbstractClass中定义的操作或其他对象的操作。
ConcreteClass :
实现原始操作以执行算法的特定于子类的步骤。
JdbcTemplate
不是 template method 设计模式的示例。使用的设计模式是 回调 。
- 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.
JdbcTemplate
isn't an example of template method design pattern. The design pattern used is callback.