模板方法和策略设计模式
这可能是一个新手问题,因为我是设计模式的新手,但我正在查看模板方法和策略 DP,它们看起来非常相似。 我可以阅读定义、检查 UML 并查看代码示例,但对我来说,策略模式似乎只是使用模板方法模式,但您只是碰巧将其传递到对象(即组合)中。
就此而言,模板方法似乎只是基本的 OO 继承。
我是否遗漏了他们差异的一些关键方面? 我是否遗漏了模板方法的某些内容,使其不仅仅是基本继承?
注意:之前有一篇关于此的文章(672083),但更多的是关于何时使用它,这有助于我了解它有点多,但我想要我对模式本身的正确想法。
This is probably a newbie question since I'm new to design patterns but I was looking at the Template Method and Strategy DP's and they seem very similar. I can read the definitions, examine the UML's and check out code examples but to me it seem like the Strategy pattern is just using the Template Method pattern but you just happen to passing it into and object (i.e. composition).
And for that matter the Template Method seems like that is just basic OO inheritance.
Am I missing some key aspect to their differences? Am I missing something about the Template Method that makes it more that just basic inheritance?
Note: There is a previous post on this (672083) but its more on when to use it, which kind of helps me get it a bit more but I want valid my thoughts on the patterns themselves.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这基本上都归结为语义。 策略模式允许您将特定的算法/过程(策略)传递给另一个对象并使用它。 模板方法允许您覆盖算法的特定方面,同时仍然保持算法的某些方面相同(保持顺序相同,并且始终在开始和结束时完成一些事情,例如......“模板”)而继承是在数据模型中对“IS-A”关系进行建模的一种方法。
当然,模板方法最容易使用继承来实现(尽管您也可以轻松地使用组合,尤其是当您拥有函子时),并且策略模式通常也是模板方法,但在语法相似的地方,含义却截然不同。
It basically all comes down to semantics. The strategy pattern allows you to pass in a particular algorithm/procedure (the strategy) to another object and that will use it. The template method allows you to override particular aspects of an algorithm while still keeping certain aspects of it the same (keep the order the same, and have things that are always done at the start and end for example... the 'template') while inheritance is a way of modelling 'IS-A' relationships in data models.
Certainly, template methods are most easily implemented using inheritance (although you could just as easily use composition, especially once you have functors), and strategy patterns are frequently also template methods but where the syntax is similar the meanings are vastly different.
策略 设计模式
提供了一种交换对象算法的方法
在运行时动态
(通过对象组合)。
例如,在订单处理系统中计算价格。
以不同的方式计算价格,
可以支持不同的定价算法
这样就可以在运行时动态选择(注入)和交换要使用的算法。
模板方法
设计模式
提供了一种方法
在编译时静态地重新定义类行为的某些部分
(通过子类化)。
例如,设计可重用的应用程序(框架)。
应用程序实现行为的公共(不变)部分
以便应用程序的用户可以编写子类来重新定义
不同的部件以满足他们的需求。
但是子类编写者不应该能够改变不变的部分
行为或行为的结构
(不变部分和变化部分的结构)。
The Strategy design pattern
provides a way to exchange the algorithm of an object
dynamically at run-time
(via object composition).
For example, calculating prices in an order processing system.
To calculate prices in different ways,
different pricing algorithms can be supported
so that which algorithm to use can be selected (injected) and exchanged dynamically at run-time.
The Template Method
design pattern
provides a way to
redefine some parts of the behavior of a class statically at compile-time
(via subclassing).
For example, designing reusable applications (frameworks).
The application implements the common (invariant) parts of the behavior
so that users of the application can write subclasses to redefine
the variant parts to suit their needs.
But subclass writers should neither be able to change the invariant parts of
the behavior nor the behavior's structure
(the structure of invariant and variant parts).