模板方法和策略模式有什么区别?
有人可以向我解释一下模板方法模式和策略模式有什么区别吗?
据我所知,它们 99% 是相同的 - 唯一的区别是 模板方法模式有一个抽象类作为基础 类,而策略类使用已实现的接口 由每个具体策略类。
然而,就客户而言,它们的消费方式完全相同 - 这是正确的吗?
Can someone please explain to me what is the difference between the template method pattern and the strategy pattern is?
As far as I can tell they are 99% the same - the only difference being
that the template method pattern has an abstract class as the base
class whereas the strategy class uses an interface that is implemented
by each concrete strategy class.
However, as far as the client is concerned they are consumed in exactly the same way - is this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
当特定操作具有一些可以根据其他变化的原始行为定义的不变行为时,使用模板模式。 抽象类定义不变的行为,而实现类定义依赖方法。
在策略中,行为实现是独立的——每个实现类都定义行为,并且它们之间没有共享代码。 两者都是行为模式,因此客户的消费方式大致相同。 通常策略有一个公共方法——
execute()
方法,而模板可以定义一组公共方法以及一组子类必须实现的支持私有原语。这两种模式可以很容易地一起使用。 您可能有一个策略模式,其中多个实现属于使用模板模式实现的策略系列。
The template pattern is used when a particular operation has some invariant behavior(s) that can be defined in terms of other varying primitive behaviors. The abstract class defines the invariant behavior(s), while the implementing classes defined the dependent methods.
In a strategy, the behavior implementations are independent -- each implementing class defines the behavior and there is no code shared between them. Both are behavioral patterns and, as such, are consumed in much the same way by clients. Typically strategies have a single public method -- the
execute()
method, whereas templates may define a set of public methods as well as a set of supporting private primitives that subclasses must implement.The two patterns could easily be used together. You might have a strategy pattern where several implementations belong to a family of strategies implemented using a template pattern.
两者之间的主要区别在于具体算法的选择。
使用模板方法模式,这会在编译时通过模板子类化发生。 每个子类通过实现模板的抽象方法来提供不同的具体算法。 当客户端调用模板外部接口的方法时,模板根据需要调用其抽象方法(其内部接口)来调用算法。
相比之下,策略模式允许在运行时通过遏制选择算法。 具体算法由单独的类或函数实现,这些类或函数作为其构造函数或 setter 方法的参数传递给策略。 为此参数选择哪种算法可以根据程序的状态或输入动态变化。
总结:
The main difference between the two is when the concrete algorithm is chosen.
With the Template method pattern this happens at compile-time by subclassing the template. Each subclass provides a different concrete algorithm by implementing the template's abstract methods. When a client invokes methods of the template's external interface the template calls its abstract methods (its internal interface) as required to invoke the algorithm.
In contrast, the Strategy pattern allows an algorithm to be chosen at runtime by containment. The concrete algorithms are implemented by separate classes or functions which are passed to the strategy as a parameter to its constructor or to a setter method. Which algorithm is chosen for this parameter can vary dynamically based on the program's state or inputs.
In summary:
我认为两种模式的类图都显示了差异。
策略
将算法封装在类中
图像链接
模板方法
将算法的确切步骤推迟到子类
图像链接
I think the Class-Diagrams of both pattern are showing the differences.
Strategy
Encapsulates an algorithm inside a class
Link to image
Template Method
Defer the exact steps of an algorithm to a subclass
Link to Image
相似之处
策略和模板方法模式之间有很多相似之处。 策略和模板方法模式都可以用来满足开闭原则,使软件模块在不改变代码的情况下易于扩展。 两种模式都表示通用功能与该功能的详细实现的分离。 然而,它们在提供的粒度方面略有不同。
差异
以下是我在研究这两种模式时观察到的一些差异:
松散,而在模板方法中,两个模块更紧密
耦合。
也可以根据情况使用,不具体类
而在 Template 方法中主要使用抽象类或具体方法
使用类,不使用接口。
用接口表示,另一方面,使用模板方法
为了减少代码重复,样板代码定义在
基础框架或抽象类。 在模板方法中,甚至可以有一个具体的类
使用默认实现。
策略模式,然而,在Template方法中,只有一些东西
更改(算法的一部分),其余部分保持不变。 在模板方法中,不变步骤在抽象基类中实现,而
变体步骤要么给出默认实现,要么不给出
完全实施。 在模板方法中,组件设计器
规定了算法所需的步骤以及算法的顺序
步骤,但允许组件客户端扩展或替换某些
这些步骤的数量。
图片取自 bitesized 博客。
Similarities
Strategy and Template method patterns have a lot of similarities between them. Both Strategy and Template method patterns can be used for satisfying the Open-Closed Principle and making the software module easy to extend without changing its code. Both patterns represent separation of generic functionality from the detailed implementation of that functionality. However, they differ a little in terms of granularity they offer.
Differences
Here are some of the differences I have observed while studying these two patterns:
loose whereas in Template Method, the two modules are more tightly
coupled.
also be used depending on the situation, and concrete class is not
used whereas in Template method mostly abstract class or concrete
class is used, interface is not used.
represented in terms of an interface, on the other hand, Template method is used
for reducing code duplication and the boilerplate code is defined in
base framework or abstract class. In Template Method, there can even be a concrete class
with default implementation.
Strategy pattern, however, in Template method, only some things
change (parts of algorithm) and rest of the things remain unchanged. In Template Method, the invariant steps are implemented in an abstract base class, while the
variant steps are either given a default implementation, or no
implementation at all. In Template method, the component designer
mandates the required steps of an algorithm, and the ordering of the
steps, but allows the component client to extend or replace some
number of these steps.
Image is taken from the bitesized blog.
您可能指的是模板方法模式。
你是对的,他们的需求非常相似。
我想说,如果您有一个“模板”算法,其中定义了子类覆盖这些步骤以更改某些细节的步骤,那么最好使用模板方法。
在策略的情况下,您需要创建一个接口,并且使用委托而不是继承。 我想说这是一个更强大的模式,并且可能更好地符合 DIP - 依赖倒置原则。 它更强大,因为您清楚地定义了策略的新抽象 - 一种做某事的方法,这不适用于模板方法。 所以,如果这个抽象有意义——就使用它。 然而,使用模板方法可以在简单的情况下给你带来更简单的设计,这也很重要。
考虑哪些词更适合:你有模板算法吗? 或者这里的关键是你有一个策略的抽象 - 做某事的新方法
模板方法的示例:
在这里,你从 application 继承并替换 init、run 和 did 上将要完成的操作。
策略示例:
在这里,当编写比较器时,您不会从数组继承。 数组将比较算法委托给比较器。
You probably mean template method pattern.
You are right, they serve very similar needs.
I would say it is better to use template method in cases when you have a "template" algorithm having defined steps where subclasses override these steps to change some details.
In case of strategy, you need to create an interface, and instead of inheritance you are using delegation. I would say it is a bit more powerful pattern and maybe better in accordance to DIP - dependency inversion principles. It is more powerful because you clearly define a new abstraction of strategy - a way of doing something, which does not apply to template method. So, if this abstraction makes sense - use it. However, using template method may give you simpler designs in simple cases, which is also important.
Consider which words fit better: do you have a template algorithm? Or is the key thing here that you have an abstraction of strategy - new way of doing something
Example of a template method:
Here you inherit from application and substitute what exactly will be done on init, run and done.
Example of a strategy:
Here, when writing a comparer, you do not inherit from an array. Array delegates the comparison algorithm to a comparer.
继承与聚合(is-a 与 has-a)。 这是实现同一目标的两种方法。
此问题显示了选择之间的一些权衡:继承与聚合
Inheritance versus aggregation (is-a versus has-a). It's two ways to achieve the same goal.
This question shows some of trade-offs between choices: Inheritance vs. Aggregation
两者非常相似,并且客户端代码都以相似的方式使用它们。 与上面最流行的答案不同,两者都允许在运行时选择算法。
两者之间的区别在于,虽然策略模式允许不同的实现使用完全不同的方式来实现所需的结果,但模板方法模式指定了一个总体算法( “模板”方法)用于实现结果——留给具体实现(子类)的唯一选择是所述模板方法的某些细节。 这是通过让模板方法调用一个或多个由子类覆盖(即实现)的抽象方法来完成的,这与模板方法本身不是抽象的并且不被子类覆盖。
客户端代码使用抽象类类型的引用/指针来调用模板方法,该引用/指针指向具体子类之一的实例,该实例可以在运行时确定,就像使用策略模式时一样。
Both are very similar, and both are consumed by the client code in similar ways. Unlike what the most popular answer above says, both allow algorithm selection at run-time.
The difference between the two is that while the strategy pattern allows different implementations to use completely different ways of the achieving the desired outcome, the template method pattern specifies an overarching algorithm (the "template" method) which is be used to achieve the result -- the only choice left to the specific implementations (sub-classes) are certain details of the said template method. This is done by having the the template method make call(s) to one or more abstract methods which are overridden (i.e. implemented) by the sub-classes, unlike the template method which itself is not abstract and not overridden by the sub-classes.
The client code makes a call to the template method using a reference/pointer of the abstract class type pointing to an instance of one of the concrete sub classes which can be determined at run time just like while using the Strategy Pattern.
模板方法:
策略:
看看模板方法和策略文章以便更好地理解。
相关文章:
JDK 中的模板设计模式,找不到定义要按顺序执行的方法集的方法
策略模式的现实示例
Template Method:
Strategy:
Have a look at Template method and Strategy articles for better understanding.
Related posts:
Template design pattern in JDK, could not find a method defining set of methods to be executed in order
Real World Example of the Strategy Pattern
不,它们不一定以相同的方式消费。 “模板方法”模式是一种为未来的实现者提供“指导”的方式。 您告诉他们,“所有 Person 对象都必须有一个社会安全号码”(这是一个简单的例子,但它正确地表达了这个想法)。
策略模式允许切换多种可能的实现。 它(通常)不是通过继承来实现的,而是让调用者传入所需的实现。 一个示例可能是允许为 ShippingCalculator 提供几种不同的税费计算方式之一(NoSalesTax 实现,或许还有 PercentageBasedSalesTax 实现)。
因此,有时,客户端实际上会告诉对象使用哪种策略。 如
但客户端永远不会对基于模板方法的对象执行此操作。 事实上,客户端甚至可能不知道对象是基于模板方法的。 模板方法模式中的那些抽象方法甚至可能受到保护,在这种情况下,客户端甚至不知道它们的存在。
No, they are not necessarily consumed in the same way. The "template method" pattern is a way of providing "guidance" to future implementers. You are telling them, "All Person objects must have a Social Security Number" (that's a trivial example but it gets the idea across correctly).
The strategy pattern allows multiple possible implementations to be switched in and out. It is not (usually) implemented through inheritance, but instead by letting the caller pass in the desired implementation. An example might be allowing a ShippingCalculator to be provided with one of several different ways of calculating taxes (a NoSalesTax implementation, and a PercentageBasedSalesTax implementation perhaps).
So, sometimes, the client will actually tell the object which strategy to use. As in
But the client would never do that for an object that was based on Template Method. In fact, the client might not even know an object is based on Template Method. Those abstract methods in the Template Method pattern might even be protected, in which case the client wouldn't even know they exist.
我建议您阅读此文章。 它解释了真实案例的差异。
引自文章
I would suggest you to read this article. It explains the differences on a real case example.
Quote from the article
在策略模式中,子类正在运行并控制算法。 这里的代码在子类中重复。 该算法以及如何实现它的知识分布在许多类中。
在模板模式中,基类有算法。 它最大化了子类之间的重用。 由于算法位于一处,因此基类会保护它。
In strategy pattern subclasses are running the show and they control the algorithm. Here code is duplicated across the subclasses. The knowledge of the algorithm and how to implement it is distributed over many classes.
In template pattern, base class has algorithm. It maximizes the reuse among the subclasses. Since algorithm lies in one place, base class protects it.
模板模式与策略模式类似。 这两种模式在范围和方法上有所不同。
策略用于允许调用者改变整个算法,例如如何计算不同类型的税,而模板方法用于改变算法中的步骤。 因此,策略的粒度更粗。 该模板允许对操作序列进行更细粒度的控制,但允许这些细节的实现发生变化。
另一个主要区别是策略使用委托,而模板方法使用继承。 在 Strategy 中,算法被委托给主题将引用的另一个 xxxStrategy 类,但使用 Template,您可以对基类进行子类化并重写方法以进行更改。
来自 http://cyruscrypt.blogspot.com/2005/07/ template-vs-strategy-patterns.html
The Template pattern is similar to the Strategy pattern. These two patterns differ in scope and in methodology.
Strategy is used to allow callers to vary an entire algorithm, like how to calculate different types of tax, while Template Method is used to vary steps in an algorithm. Because of this, Strategy is more coarsely grained. The Template allows finer-grained controls in the sequent of operations, and yet allows the implementations of these details to vary.
The other main difference is that Strategy uses delegation while Template Method uses inheritance. In Strategy, the algorithm is delegated to the another xxxStrategy class that the subject will have a reference to, but with Template you subclass the base and override methods to make changes.
from http://cyruscrypt.blogspot.com/2005/07/template-vs-strategy-patterns.html
模板模式:
模板方法是让子类重新定义算法的某些步骤,而不改变基类中定义的算法的主要结构和步骤。
模板模式通常使用继承,因此可以在基类中提供算法的通用实现,子类可以根据需要选择覆盖该实现。
请注意,在上面的代码中,go() 算法步骤始终是相同的,但子类可能会定义不同的方法来执行特定步骤。
策略模式:
策略模式是让客户端在运行时选择具体的算法实现。 所有算法都是孤立且独立的,但实现一个公共接口,并且不存在在算法内定义特定步骤的概念。
如需完整源代码,请查看我的 github 存储库。
Template Pattern:
Template method is about letting subclasses redefine certain steps of the algorithm, without changing the main structure and steps of the algorithm, defined in the base class.
Template pattern usually uses inheritance, so a generic implementation of algorithms can be provided in the base class, which the subclass might choose to override if needed.
Note in the above code, the go() algorithm steps will always be the same, but the subclasses might define a different recipe for performing a particular step.
Strategy Pattern:
Strategy pattern is about letting client selects concrete algorithms implementation at runtime. All algorithms are isolated and independent, but implement a common interface, and there is no notion of defining particular steps within the algorithm.
For full source code, check out my github repository.
它们都是实现相同结果的不同技术,所以问题是何时使用哪种。
Both of them are different techniques to achieve the same result, so question is to use which when.
策略作为接口公开,模板方法作为抽象类公开。 这通常在框架中大量使用。
例如
Spring框架的MessageSource类是解析消息的策略接口。 客户端使用该接口的特定实现(策略)。
以及同一接口AbstractMessageSource的抽象实现,它具有解析消息的公共实现,并公开resolveCode()抽象方法,以便子类可以按照自己的方式实现它们。 AbstractMessageSource 是模板方法的一个示例。
http:// /docs.spring.io/spring/docs/4.1.7.RELEASE/javadoc-api/org/springframework/context/support/AbstractMessageSource.html
Strategy is exposed as an Interface and template method as the Abstract Class. This is typically used a lot in frameworks.
e.g.
Spring framework's MessageSource class is a strategy interface for resolving messages. Client uses particular implementation (strategy) of this interface.
And the abstract implementation of the same interface AbstractMessageSource, which has common implementation of resolving messages and exposes resolveCode() abstract method so that sub-classes can implement them in their ways. AbstractMessageSource is an example of template method.
http://docs.spring.io/spring/docs/4.1.7.RELEASE/javadoc-api/org/springframework/context/support/AbstractMessageSource.html
在此设计模式的模板方法中,子类可以覆盖一个或多个算法步骤,以允许不同的行为,同时确保仍然遵循总体算法(Wiki)。
模式名称模板方法意味着它是什么。 假设我们有一个方法CalculateSomething()并且我们想要模板化这个方法。 该方法将在基类中声明为非虚方法。 说方法看起来像这样。
}
Step1和Step2方法的实现可以由派生类给出。
在策略模式中,基类没有提供任何实现(这就是为什么基类在类图中实际上是一个接口的原因)。
经典的例子是排序。 根据需要排序的对象数量,创建适当的算法类(合并、冒泡、快速等),并将整个算法封装在每个类中。
现在我们可以将排序实现为模板方法吗? 当然可以,但是您不会发现太多/任何共性可以被抽象出来并放置在基本实现中。 所以它违背了模板方法模式的目的。
In the template method of this design pattern, one or more algorithm steps can be overridden by subclasses to allow differing behaviors while ensuring that the overarching algorithm is still followed(Wiki).
The pattern name Template method means what it is. Say we have a method CalculateSomething() and we want to template this method. This method will be declared in the base class a non virtual method. Say the method looks like this.
}
Step1 and Step2 method implementation can be given by derived classes.
In Strategy Pattern there is no implementation provided by the base (This is the reason why the base is really an interface in the class diagram)
The classic example is sorting. Based on the number of objects needs to be sorted the appropriate algorithm class(merge, bubble, quick etc.) is created and the entire algorithm is encapsulated in each class.
Now can we implement the sorting as a template method? Certainly you can, but you wont find much/any commonality to be abstracted out and placed in the base implementation. So it defeats the purpose of template method pattern.
我认为主要的区别在于,使用模板,您需要一个可以执行某些操作的算法,但是假设在该算法的中间您想要运行不同的行为,以便您可以发送接口的实现,以便在运行时动态地生成该算法。
但通过策略,您实际上拥有完全不同的算法执行,而不仅仅是算法的变体,然后您选择要运行的算法,但模板您只有一个带有变体的算法。
最后,您可以根据需要实施并使用模板作为策略,反之亦然,但我看到了区别。
I think the major difference is that with template you need an algorithm that does something, but lets say in the middle of that algorithm you want to run different behaviours so you can send an implementation of an interface to make that algorithm dynamically in runtime.
but with strategy you have actually totally different algorithms executions no just a variant of the algorithm, then you chose which algo to run, but template you have just one algo with variants.
At the end you can implement as you want and use template as strategy and viceversa but I see a difference.
模板方法模式擅长阐明整体算法步骤,而策略模式适合灵活性和可重用性,因此您可以根据需要将策略组合在一起,例如:jdk8中的许多功能接口,例如
Comparator.reversed().thenComparing (Comparator)
是策略的一个角色。模板方法模式是焦点更高内聚,而策略模式是与上下文对象松散耦合以分离关注点。
策略很容易维护,因为上下文不了解具体策略,无论何时上下文中主要算法发生变化都不会影响策略。 另一方面,如果改变了抽象模板类中算法的骨架,可能会影响其子类的升级。
Template method pattern is good at clarify the overall algorithm steps, however Strategy pattern is suitable for flexibility and reusibility, so you can combine strageties together if you need, e.g: many functional interfaces in jdk8 like
Comparator.reversed().thenComparing(Comparator)
is a role of strategy.Template method pattern is focus more high cohesion, but Strategy pattern is loosely coupled with context object for separating concerns.
Strategy is easy to maintain since the context has no knowlege about concrete strategies, whenever the mainly algorithm is changed in context doesn't affect the strategies. On the other hand, if you changed the sckeleton of the algorithm in abstract template class maybe affect its subclasses to upgrade.