我们应该在哪里使用模板方法 - 模式? [英] Where should we use Template Method - pattern?

查看:118
本文介绍了我们应该在哪里使用模板方法 - 模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以让我知道应该使用模板方法 - 模式的一些示例情况?



从我们自己的经验中给我一些现实世界的使用。 >

(到目前为止,我发现它只对DA层中的数据进行映射有用。对不起!!!)

解决方案

模板方法模式为执行任何类型的算法或操作提供了一个框架,它允许子类重新定义部分逻辑。 / p>

优点:自然适合构建框架,以便父框架类可以使回调到子程序中实现的方法。



示例:




  • java.util.AbstractList

  • Servlet的doGet和doPost方法

  • MDB的onMessage方法

  • Struts Action类

  • Spring的数据访问clas ses



缺点:限制您在Java中的单一继承。


Can anyone let me know some example situations where Template Method - pattern should be used?

Give me some real-world use from your own experience.

(I have so far found it useful only for mapping data in the DA layer. Sorry!!!)

解决方案

A Template method pattern provides a skeleton for performing any sort of algorithm or an operation, and it allows the sub-classes to re-define part of the logic.

Pros: Natural fit for building frameworks, so that parent framework classes can make callbacks into methods implemented in child.

Examples:

  • java.util.AbstractList
  • Servlet's doGet and doPost methods
  • MDB's onMessage method
  • Struts Action class
  • Spring's data access classes

Cons: Restricts you to a single inheritance in Java.

这篇关于我们应该在哪里使用模板方法 - 模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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