模板方法(分离)和策略模式之间的区别?

发布于 2024-08-16 08:07:05 字数 425 浏览 7 评论 0原文

我的老师是一位非常好的老师,我倾向于理解他的观点,但这个问题超出了我的理解范围。 他用两种变体解释了模板方法;
- 统一:标准变体,由抽象类和一些定义固定算法的变体部分的抽象方法组成。
- 分离:他自己的变体(我认为?),其中一个类包含 templateMethod() 并使用对接口的委托来改变算法的各个部分,在我看来,这与策略模式完全相同。

谁能明白他的观点是什么,以及“分离”变体与策略模式有何不同?
我附上了一张包含他的书中的两种图案的图像(尚未出版)。

http://img64.imageshack.us/img64/3620/strategytemplate.jpg

My teacher is a really good one and I tend to understand his points, but this one just goes over my head.
He explains Template Method in two variants;
- Unification: the standard variant, that is composed of an abstract class with some abstract methods defining the variant parts of the otherwise fixed algorithm.
- Separation: his own variant (I think?) where a class contains the templateMethod() and uses delegation to an interface to vary the parts of the algorithm, which looks to me exactly like the Strategy pattern.

Can anyone see what his point is, and how the 'separation' variant is different from the Strategy pattern?
I have attached an image containing the two patterns from his book (which isn't published yet).

http://img64.imageshack.us/img64/3620/strategytemplate.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习ぎ惯性依靠 2024-08-23 08:07:05

我从未听说过模板方法模式的“分离变体”,并且我同意它看起来与策略极其相似。即使对接口所有权或如何从客户端角度调用它们有一些推理,我也几乎发现考虑它们不同的模式有任何好处。

I have never heard of a "Separation variant" of the Template method pattern, and I agree that it looks extremely similar to the Strategy. Even if there is some reasoning about interface ownership or how you invoke them from a client perspective I hardly find there's any benefit to consider them different patterns.

Spring初心 2024-08-23 08:07:05

在常见用法中,模板方法使用子类来提供不同的行为。使用策略,您注入一个算法对象。在您的示例中,模板(分离)策略之间没有有用的区别。考虑到 Gamma 等人书的年龄,介绍此当您与其他程序员交谈时,如果没有充分解释新术语的差异,很可能会引起混乱。避免在课程之外使用它。

模板允许您访问基类中的受保护成员。策略允许您开发与使用它们的对象更加松散耦合的算法,并允许您将相同的算法注入到许多不同类型的对象中。

In common usage, Template method uses subclasses to provide the varied behaviours. With Strategy, you inject an algorithm object. In your example, there is no useful distinction between Template (separation) and Strategy. Given the age of the Gamma et al book, introducing this new terminology withou adequately explaining the difference is likely to simply cause confusion when you talk to other programmers. Avoid using it outside your lessons.

Template allows you to access protected members in the base class. Strategy allows you to develop you algorithms more losely coupled from the objects that use them, and allows you to inject the same algorithm into many different types of object.

风月客 2024-08-23 08:07:05

模板方法:

  1. 这是一种行为设计模式
  2. ,用于创建方法存根并将某些实现步骤推迟到子类。它由某些顺序固定的步骤组成。
  3. 它定义了执行算法的步骤,并且可以提供可能对所有或某些子类通用的默认实现。
  4. 超类模板方法调用子类的方法,

策略模式:

  1. 它是一种行为模式
  2. 它基于委托
  3. 它通过修改方法行为来改变对象的内部
  4. 它用于在家族之间切换算法
  5. 它在运行时改变对象的行为。将从一系列算法中选择一个算法。

基本差异。

  1. 模板方法使用继承,策略使用组合
  2. 由基类实现的模板方法不应被重写。 这样,算法的结构由超类控制细节在子类中实现
  3. 策略 将算法封装在接口后面,这使我们能够在运行时更改算法。多种策略为接口提供了不同的实现。

看看 Journaldev 模板方法策略文章,以便更好地理解 sourcemaking 文章。

Template Method:

  1. It is a behavioural design pattern
  2. it’s used to create a method stub and deferring some of the steps of implementation to the subclasses. It consists of certain steps whose order is fixed.
  3. It defines the steps to execute an algorithm and it can provide default implementation that might be common for all or some of the subclasses.
  4. superclass template method calls methods from subclasses,

Strategy pattern:

  1. It's a behavioural pattern
  2. It's based on delegation
  3. It changes guts of the object by modifying method behaviour
  4. It's used to switch between family of algorithms
  5. It changes the behaviour of the object at run time. One algorithm will be selected from a family of algorithm.

Basic differences.

  1. Template method uses Inheritance and Strategy uses composition
  2. The Template method implemented by the base class should not be overridden. In this way, the structure of the algorithm is controlled by the super class, and the details are implemented in the sub classes
  3. Strategy encapsulates the algorithm behind an interface, which provide us ability to change the algorithm at run time. Multiple strategies provide different implementation to interface.

Have a look at Journaldev Template method and Strategy articles for better understanding along with sourcemaking articles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文