为什么JdbcTemplate是Template方法设计模式的示例 [英] Why is JdbcTemplate an example of the Template method design pattern

查看:236
本文介绍了为什么JdbcTemplate是Template方法设计模式的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的注意力被

在阅读了解释和特定代码之后我仍然想知道为什么这是模板方法设计模式的示例。



根据GoF,此模式的目的是:


定义操作中算法的框架,将某些步骤推迟到子类。模板方法允许子类在不更改算法结构的情况下重新定义算法的某些步骤。


有两个参与者:


AbstractClass

-定义抽象的基本操作,具体的子类定义这些操作来实现算法的步骤

-实现模板方法,该方法定义算法的框架。模板方法调用原始操作以及在AbstractClass中定义的操作或其他对象的操作。

ConcreteClass

实现原始操作以执行算法的特定于子类的步骤。







为什么 JdbcOperations中的代码被视为模板方法设计模式?




  • 我看不到super / abstract类中定义了任何全局/通用算法,即使将其与类似文件 JmsTemplate中的代码进行比较也是如此。 / li>
  • 在具体类中实现的功能均未在超类中定义。所有定义的方法都是通过使用接口(在这种情况下为接口'JdbcOperations')添加的,实际上没有一个方法会覆盖父方法中的方法。



我得到一个事实,它是非常便于消除 样板代码。但是,为什么这是一个模板方法,而不仅仅是一个巧妙的编码技巧。在我看来,它似乎没有任何模板方法具有的特征。

解决方案

我同意- JdbcTemplate 不是 template method 设计模式的示例。使用的设计模式是 回调



请注意,两种模式的目标和效果非常相似,主要区别在于 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:
- 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.

解决方案

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.

这篇关于为什么JdbcTemplate是Template方法设计模式的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆