构建一个对象

发布于 2024-10-05 18:36:18 字数 199 浏览 2 评论 0原文

我在这个博客中遇到了一种相当不寻常的构建类对象的方法: 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 技术交流群。

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

发布评论

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

评论(6

他不在意 2024-10-12 18:36:19

我在这个博客中遇到了一种相当不寻常的构建类对象的方法:

这是具有流畅界面构建器设计模式

正如您从文章中看到的,这两个想法是互补的并且经常一起使用(我见过一些人称之为“流畅的构建器”),以至于它们经常被混淆为相同的事情:

请注意,您可以在没有流畅界面的情况下使用构建器模式(例如具有简单设置器的构建器)。您还可以在不仅仅是构建器的更多上下文中使用流畅的接口思想(例如,提高一组具有许多参数和参数变化的重载方法的可读性)。

这是一个好方法吗?

这个“流畅的构建器”似乎被高度接受为“实现这一目标的好方法”(至少基于我见过的宣扬这一想法的文章和博客文章的数量)。

有什么好处?

每个想法都有其独特的优点/好处。例如,请参阅:

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/.

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:

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).

Is this a good way to do this?

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).

What are the benefits?

Each idea has its own distinct advantages/benefits. For example, see:

孤者何惧 2024-10-12 18:36:19

唯一的好处是可读性。顺便说一下,这是一个流畅界面的示例。

流畅的接口旨在提供一个 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.

戈亓 2024-10-12 18:36:19

Yes, this is what Joshua Bloch had in mind in Chapter 2 of his "Effective Java".

如歌彻婉言 2024-10-12 18:36:19

这是构建器模式的简单示例。它允许将创建复杂对象的算法与组成对象的部件及其组装方式分开。 GoF 将此模式的后果解释为:

  1. 它可以让您改变产品的内部表示。建造者
    对象提供了一个抽象
    用于构造的接口
    产品。该界面让
    构建器隐藏表示并
    产品的内部结构。它
    还隐藏了产品的获取方式
    组装。因为该产品是
    通过抽象构建
    界面,您所要做的就是
    改变产品的内部结构
    表示法定义了一种新的
    建设者。
  2. 它将构建和表示的代码隔离。建造者
    模式通过以下方式提高了模块化性
    封装复杂对象的方式
    被构建并表示。
    客户无需了解任何信息
    定义产品的类
    内部结构;这样的课程不
    出现在Builder的界面中。每个
    ConcreteBuilder包含所有代码
    创建并组装特定的
    一种产品。代码写好了
    一次;那么不同的客户可以
    重用它来构建产品变体
    来自同一组零件。
  3. 它可以让您更好地控制构建过程。不像
    构建的创造模式
    一键生成产品,Builder
    模式逐步构建产品
    步骤在用户的控制下
    构建器对象。仅当
    用户是否完成了产品
    从构建器中检索它。因此
    Builder 界面反映了
    构建产品的过程
    比其他创作模式更多。
    这使您可以更好地控制
    施工过程及结果
    的内部结构
    由此产生的产品。

此模式的一个真实示例是 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:

  1. It lets you vary a product's internal representation. The Builder
    object provides an abstract
    interface for constructing the
    product. The interface lets the
    builder hide the representation and
    internal structure of the product. It
    also hides how the product gets
    assembled. Because the product is
    constructed through an abstract
    interface, all you have to do to
    change the product's internal
    representation is define a new kind of
    builder.
  2. It isolates code for construction and representation. The Builder
    pattern improves modularity by
    encapsulating the way a complex object
    is constructed and represented.
    Clients needn't know anything about
    the classes that define the product's
    internal structure; such classes don't
    appear in Builder's interface. Each
    ConcreteBuilder contains all the code
    to create and assemble a particular
    kind of product. The code is written
    once; then different clients can
    reuse it to build Product variants
    from the same set of parts.
  3. It gives you finer control over the construction process. Unlike
    creational patterns that construct
    products in one shot, the Builder
    pattern constructs the product step by
    step under the control of the user of
    the builder object. Only when the
    product is finished does the user
    retrieve it from the builder. Hence
    the Builder interface reflects the
    process of constructing the product
    more than other creational patterns.
    This gives you finer control over the
    construction process and consequently
    the internal structure of the
    resulting product.

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 the Parser class which is initialized with a ProgramNodeBuilder. The Parser object notifies its ProgramNodeBuilder 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.

鲜血染红嫁衣 2024-10-12 18:36:19

我很高兴在这里发表我的第一篇 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.

窗影残 2024-10-12 18:36:19

我个人发现使用具有流畅界面的构建器非常有用且非常直观。向构建器发送消息时,您可以更好地表达您的意图。

我写了一个带有 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/

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