Fluent API 与其他 API 有何不同?

发布于 2024-09-02 14:04:58 字数 173 浏览 5 评论 0原文

我在研究 DSL 时遇到了 Fluent API。
我对流畅的 API 进行了很多搜索...我可以得出的基本结论是,流畅的 API 使用方法链来使代码流畅。
但我无法理解 - 在面向对象的语言中,我们总是可以创建一个对象并可以调用与其相关的方法。那么流畅的 API 有何不同呢?流畅的 API 还添加了哪些其他功能?

I have come across fluent API's while studying DSLs.
I have searched a lot on fluent API's...the basic conclusion which I could draw was that fluent a API uses method chaining in order to make the code fluent.
But I cannot understand - in object oriented languages we can always create an object and can call the methods related to it. Then how is a fluent API different? What other features does a fluent API add?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度° 2024-09-09 14:04:58

使用流畅的接口,您可以编写返回调用该方法的对象(通常是 self 或 this )的方法,并将传统返回值作为该对象中的状态更改进行处理。如果你看看一些使用流畅接口的 Javascript 库,你会发现处理列表和空值变得更加容易,因为它们可以像处理单个对象一样处理。流畅接口的缺点是它们倾向于创建具有大量职责的整体上帝对象。

我不希望它们到处使用(因为上帝对象问题),但它们有时很好。

With a fluent interface you write methods that return the object that the method was invoked on (usually self or this) and handle traditional return values as a state change in that object. If you look at say some of the Javascript libraries that use a fluent interface it makes it far easier to deal with lists and nulls as they can be handled the same way you would a single object. The disadvantage of fluent interfaces is that they tend to create monolithic god objects that have a whole heap of responsibilities.

I wouldn't want them to be used everywhere (because of the god object problem) but they are nice from time to time.

花之痕靓丽 2024-09-09 14:04:58

您的问题已在 Martin Fowler 的 Fluent Interface 博文中得到解答。要点是,流畅 API 的流畅性来自于特定领域语言的领域,而不仅仅是方法链。

Your question is answered in the originating Fluent Interface blog post by Martin Fowler. The point is that the fluency in fluent API comes from the domain of a domain specific language, not only method chaining.

终遇你 2024-09-09 14:04:58

Fluent API 是一种指定模型配置的高级方法,除了数据注释无法实现的一些更高级配置之外,它涵盖了数据注释可以执行的所有操作。

Web API是一种编程接口/应用程序类型,提供软件应用程序之间的通信或交互。

Fluent API is an advanced way of specifying model configuration that covers everything that data annotations can do in addition to some more advanced configuration not possible with data annotations.

And Web API is a programming interface/application type that provides communication or interaction between software applications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文