构建一个对象
我在这个博客中遇到了一种相当不寻常的构建类对象的方法: http://marchwicki.pl/blog/ 2010/11/以优雅的方式建造一个pojo/。这是一个好方法吗?有什么好处?
I came across a rather unusual way to build an object of a class in this blog :
http://marchwicki.pl/blog/2010/11/building-a-pojo-in-an-elegant-way/. Is this a good way to do this. What are the benefits?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是具有流畅界面的构建器设计模式。
正如您从文章中看到的,这两个想法是互补的并且经常一起使用(我见过一些人称之为“流畅的构建器”),以至于它们经常被混淆为相同的事情:
“四人帮”中讨论了构建器模式设计模式书
Joshua Bloch 在他的书中将构建器称为第 2 条有效的 Java
Fluent 接口是一种 API 风格,它使用上下文和方法链来改进代码的可读性。
http://www.martinfowler.com/bliki/FluentInterface.html
请注意,您可以在没有流畅界面的情况下使用构建器模式(例如具有简单设置器的构建器)。您还可以在不仅仅是构建器的更多上下文中使用流畅的接口思想(例如,提高一组具有许多参数和参数变化的重载方法的可读性)。
这个“流畅的构建器”似乎被高度接受为“实现这一目标的好方法”(至少基于我见过的宣扬这一想法的文章和博客文章的数量)。
每个想法都有其独特的优点/好处。例如,请参阅:
Builder - 您何时会使用生成器模式?
流畅的界面 - 什么是流畅的界面?
This is the builder design pattern with a fluent interface.
As you see from the article, the two ideas are complementary and often used together (which I've seen some call a "fluent builder"), so much so, that they are often confused as the same thing:
The builder pattern abstracts and simplifies the construction of a complex object into simpler steps.
The builder pattern is discussed in the "Gang of Four"'s Design Patterns book
Joshua Bloch talks about builders as Item 2 in his book Effective Java
A fluent interface is an API-style that uses contexts and method chaining to improve the readability of code.
http://www.martinfowler.com/bliki/FluentInterface.html
Note that you can use the builder pattern without a fluent interface (e.g. a builder with simple setters). You can also use the fluent interface idea in more contexts than just builders (e.g. to improve the readability of a set of overloaded methods with many parameters and parameter variations).
This "fluent builder" seems highly accepted as "a good way to do this" (at least based on the number of articles and blog posts I've seen proselytizing the idea).
Each idea has its own distinct advantages/benefits. For example, see:
Builder - When would you use the Builder Pattern?
Fluent Interface - What's a fluent interface?
唯一的好处是可读性。顺便说一下,这是一个流畅界面的示例。
流畅的接口旨在提供一个 API,该 API 在使用时会比标准 OOP API 提供的代码更具可读性。
The only benefit is readability. By the way, this is an example of a fluent interface.
A fluent interface seeks to provide an API that, when used, produces more readable code than a standard OOP API tends to provide.
是的,这就是Joshua Bloch在他的“Effective Java”的第 2 章。
Yes, this is what Joshua Bloch had in mind in Chapter 2 of his "Effective Java".
这是构建器模式的简单示例。它允许将创建复杂对象的算法与组成对象的部件及其组装方式分开。 GoF 将此模式的后果解释为:
此模式的一个真实示例是 Smalltalk-80 编译器子系统中的
ProgramNodeBuilder
类。源代码由使用ProgramNodeBuilder
初始化的Parser
类的对象进行解析。Parser
对象每次识别语法结构时都会通知其ProgramNodeBuilder
对象。当解析器完成后,它会向构建器询问它构建的解析树并将其返回给客户端。This is a simple example of the Builder pattern. It allows the separation of the algorithm for creating a complex object from the parts that make up the object and the way they are assembled. GoF explains the consequences of this pattern as:
A real-world example of this pattern is the
ProgramNodeBuilder
class in the Smalltalk-80 compiler sub-system. Source code is parsed by an object of theParser
class which is initialized with aProgramNodeBuilder
. TheParser
object notifies itsProgramNodeBuilder
object each time it recognizes a syntactic construct. When the parser is done, it asks the builder for the parse tree it built and returns it to the client.我很高兴在这里发表我的第一篇 stackoverflow 文章 - 因为您正在讨论我的文章:-)
我认为这种构造(构建器和流畅接口的利用:流畅构建器)极大地提高了代码的可读性。更重要的是(不讨论设计模式)当我们需要不可变对象时它证明了它的价值。简单地说,通过将构造函数设为私有并删除所有设置器,我们获得了一个非常强大的工具,可以以优雅、可读且强大的方式构建不可变对象。最后,整篇文章应该更多地是关于如何智能地使用模板来生成可重复的代码 - 但我非常喜欢这个讨论。
I'm quite happy to do my first stackoverflow post here - cause you are discussing my article :-)
I think this construction (leverage of both builder and fluent interface: fluent builder) greatly improves code readability. What is more (and not getting into discussion around design patterns) it proves its value when we need immutable objects. Simply, by making constructor private and removing all the setters, we gain a very powerful tool for an elegant and readable, yet powerful way of building immutable objects. Finally, the whole post was supposed to be more about smart usage of templates to generate repeatable code - but I pretty like the discussion.
我个人发现使用具有流畅界面的构建器非常有用且非常直观。向构建器发送消息时,您可以更好地表达您的意图。
我写了一个带有 Fluent Interface 的 Builder 的小例子,希望它有所帮助。
http:// jpereira.eu/2011/10/12/fluid-interfaces-while-trying-to-make-sense-of-prototype-pattern/
I personally find using a Builder with a Fluent Interface very useful and very intuitive. You can express better your intents when sending messages to the builder.
I wrote a little example of a Builder with Fluent Interface, hope it helps.
http://jpereira.eu/2011/10/12/fluent-interfaces-while-trying-to-make-sense-of-prototype-pattern/