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

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

问题描述

我对设计模式非常陌生,并且对流畅的界面之间的区别感到困惑和建造者模式.

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

我理解流畅接口的概念.但是构建器模式有点令人困惑.我无法理解在 Builder 模式中使用 Director.

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.

我可以同时使用 Builder 模式和 Fluent Interface 吗?如果是这样,那么我应该如何与导演和混凝土建造者一起工作?

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.

使用来自 GoF 的 Builder 的 UML 序列图进行

Edit with UML sequence diagram for Builder from GoF:

序列与导演图"

推荐答案

Fluent 界面背后的理念是,可以通过将多个属性用点连接来将多个属性应用于对象,而不必每次都重新指定对象.构建器模式背后的想法是,非共享可变对象通常比非共享不可变对象更容易使用,但与共享可变对象相比,共享不可变对象更容易推理.因此,代码可以使用一个易于使用的可变对象来生成所需实例的模型",然后使用它来创建一个易于共享的、包含相同数据的不可变对象.

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:

  • 让实例的每个成员返回一个应用了适当更改的新实例.
  • 通过让每个成员改变调用它的实例并返回它.
  • 让每个成员返回一个轻量级补丁对象的实例,该对象包含指向正在修改的事物或前一个补丁的链接.

最后一种样式需要采取一些措施来应用所有补丁,但是如果被修改的对象很大并且需要进行很多更改,它可以最大限度地减少所需的复制量.

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天全站免登陆