我们应该在哪里使用模板方法

编程入门 行业动态 更新时间:2024-10-27 14:29:55
本文介绍了我们应该在哪里使用模板方法 - 模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

(到目前为止,我发现它只对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.

更多推荐

我们应该在哪里使用模板方法

本文发布于:2023-10-27 18:24:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534172.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我们应该   模板   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!