流畅的界面和Builder模式有什么区别? [英] What is the difference between a fluent interface and the Builder pattern?

查看:127
本文介绍了流畅的界面和Builder模式有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常新的设计模式,并且在流畅的界面和Builder模式。



我了解流畅界面的概念。但是建筑师模式有点混乱。我不明白在Builder模式中使用Director。



我可以一起使用Builder模式和Fluent界面吗?如果是这样,那么我应该如何使用Director和具体的构建器来实现?



我的问题是不是关于构建器模式的优点。但是这个问题的目的是要了解构建器模式和流畅界面之间的关系。






使用UML序列图编辑助洗剂从GoF的:



解决方案

Fluent界面背后的想法是可以将多个属性应用于一个对象通过连接它们与点,而不必每次重新指定对象。构建器模式背后的想法是,非共享可变对象通常比未共享的不可变对象更易于使用,但是对于共享的可变对象而言,比共享可变对象更容易理解。因此,代码可以使用易于工作的可变对象来产生所需实例的模型,然后使用它来创建一个易于共享的不可变对象,该对象保存相同的数据。 >

这两个想法可以一起工作,但有些正交。请注意,流畅的界面至少有三种方式可以工作:通过使实例的每个成员返回一个适用的更改应用的新实例,方法是让每个成员对被调用的实例进行mutate并返回,或者通过每个成员返回一个轻量级补丁对象的实例,该对象持有要修改的内容或前一个补丁的链接。最后一个样式需要采取一些措施来应用所有的修补程序,但如果被修改的对象很大,需要进行许多更改,则可以最大限度地减少所需的复制数量。


I'm very new to design patterns and am having trouble with the difference between fluent interfaces and the Builder pattern.

I understand the concept of fluent interfaces. But the builder pattern is a little confusing. I cannot understand the use of a Director in the Builder pattern.

Can I use the Builder pattern and Fluent Interface together? If so, then how should I do so with a Director and a concrete builder?

My question is not about advantages of builder pattern. But the aim of this question is to know the relation between builder pattern and fluent interface.


Edit with UML sequence diagram for Builder from GoF:

解决方案

The idea behind a Fluent interface is that one can apply multiple properties to an object by connecting them with dots, without having to respecify the object each time. The idea behind the builder pattern is that unshared mutable objects are often easier to work with than unshared immutable ones, but it's much easier to reason about shared immutable objects than shared mutable ones. Thus, code can use an an easy-to-work-with mutable object to produce a "model" of a desired instance, and then use that to make an easy-to-share immutable object that holds the same data.

The two ideas can work well together, but are somewhat orthogonal. Note that there are at least three ways a fluent interface can work: by having each member of an instance return a new instance with the appropriate change applied, by having each member mutate the instance upon which it is invoked and return that, or by having each member return an instance of a lightweight patch object which holds a link to either the thing being modified or the previous patch. The last style requires that some action be taken to apply all the patches, but if the object being modified is large and many changes are necessary, it can minimize the amount of copying that's required.

这篇关于流畅的界面和Builder模式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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