构建器模式和模板方法之间的差异(构建器与模板) [英] Differences between builder pattern and template method (builder vs template)

查看:245
本文介绍了构建器模式和模板方法之间的差异(构建器与模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模板模式在基类中提供算法,其派生类中的步骤可以被修改。
在Builder模式下,具体的构建者会公开构建从Director类调用的产品的方法。



我明白,使用这些模式。模板模式是改变模板中一个或多个步骤的行为模式,而构建器模式是创建模式。



除了上述差异外,还有其他差异吗?



充当模板模式的基础模板。具体的建设者在模板模式中采用派生类作为替代步骤?



有人可以澄清一点。谢谢。



我是指 http: //www.dofactory.com/Patterns/Patterns.aspx

解决方案

模板方法真的只是一种方法定义每个子类必须定义的一些方法。



构建器模式用于构造更复杂的对象。





如果我们使用模板方法模式,我们必须为每个单独的汽车组合创建一个类,或者使用一些令人讨厌的继承层次结构。很多这些方法也将包含重复的代码。



使用构建模式,我们可以改为使用不同的部分,并将它们组合成一个完整的车。因此,如果需要,我们可以为每个型号重新使用引擎,我们也可以定制汽车的每一部分。


Template pattern provides algorithm in the base class whose steps can be modified in the derived class. In Builder pattern, concrete builder exposes methods for building a product which are called from the Director class.

I understand there is difference with respect to the purpose of using these patterns. Template pattern is a behavioral pattern which changes one or more steps in a template whereas builder pattern is a Creational pattern.

Apart from the above said difference, are there any other differences?

Isn't director in builder pattern acting as a base template in template pattern. The concrete builders are acting like derived classes in the template pattern with substitutable steps?

Can someone please clarify. Thank you.

I am referring http://www.dofactory.com/Patterns/Patterns.aspx

解决方案

Template method is really just a way to define some methods that each subclass must define.

The builder pattern is used to construct a more complex object.

Lets say that we want to construct different Saab (car brand) models. Each model has different engines, lights etc.

If we were to use the template method pattern we had to create one class for each single combination of cars or use some nasty inheritance hierarchies. A lot of those methods would also contain duplicate code.

With the build pattern we can instead take different parts and compose those together to a complete car. Hence we can reuse a engine for every model if needed and we can also customize each part of the car.

这篇关于构建器模式和模板方法之间的差异(构建器与模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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