构造器模式和构造器之间的区别 [英] Difference between builder pattern and constructor

查看:136
本文介绍了构造器模式和构造器之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构建器模式只是一种构建类似于构造器功能的对象的方法,那么为什么要使用构建器模式而不是普通的旧构造器?

The builder pattern is just a way to build an object similar to what a constructor does, so why use a builder pattern instead of plain old constructors?

推荐答案

我同意您的观点,即 Builder 实际上只是一个光荣的构造函数,而 builder模式只是一种建立对象的方法类似于构造函数所做的事情。

I agree with your view that a Builder is really just a glorified constructor, and that the "builder pattern is just a way to build an object similar to what a constructor does".

但是,在某些情况下,构造对象的复杂性使 Builder 引人注目。

However, here are a few of scenarios where the complexity of constructing an object makes the use of a Builder compelling.

在Java中, StringBuilder 在一段时间内,或者在一个复杂的过程中构建一个字符串。例如,如果服务器正在通过套接字与客户端通信,并且希望将某些客户端响应附加到字符串,而不是其他字符串,并可能删除以前附加的某些响应,则 StringBuilder 类可以这样做。在客户端/服务器会话结束时,服务器可以调用 StringBuilder#toString 来获取构建的 String

In Java, StringBuilder is commonly used when building a string over a period of time, or rather, within a complex procedure. For instance, if a server is communicating with a client over a socket, and wants to append some client responses to the string, but not others, and perhaps remove certain responses that were previously appended,the StringBuilder class can be used to do so. At the end of the client/server session, the server can invoke StringBuilder#toString to get the built String.

如果构造函数具有数十个参数,则可能会使代码更易读或易于理解

If a constructor has dozens of parameters, it may make the code more readable or easy to maintain to use a builder.

例如

new Foo(1,2,3,4,5,6,7,8,9,10,11,12)

vs。

Foo.newBuilder()
   .bar(1)
   .bar(2)
   .quux(3)
   ...
   .build()



构造对象图



类似于很多参数方案,我认为构建器最引人注目的方案是构造复杂的对象图时。此问题中的其他答案涉及可伸缩反模式。这种情况(构建复杂的对象图)可能导致 telescoping, Builder 可以帮助解决此问题。

Constructing object graphs

Similar to the "lots of parameters" scenario, I think that the scenario where a builder is most compelling is when constructing a complex object graph. The other answers in this question refer to the telescoping anti-pattern. This scenario (building a complex object graph) can lead to "telescoping", which the Builder helps resolve.

例如,假设您有一个面向对象的管道接口,其中 Pipeline 取决于 Sequence 取决于阶段 PipelineBuilder 不仅可以为 Pipeline 的构造函数提供一个很好的包装,而且可以为<$ c $的构造函数提供一个包装c>序列和舞台,可让您从单个 Builder 接口。

For instance, imagine you have an object-oriented pipeline interface, where a Pipeline depends on Sequence which depends on Stage. A PipelineBuilder would not only provide a nice wrapper around the constructor of Pipeline, but also around the constructors Sequence and Stage, allowing you to compose a complex Pipeline from a single Builder interface.

而不是像这样伸缩构造器:

Instead of telescoping constructors like so:

new Pipeline(
    new Sequence(
        new Stage(
            new StageFunction() {
                public function execute() {...}
            }
        ),
        new Stage(
            new StageFunction() {
                public function execute() {...}
            }
        )
    )
)

A PipelineBuilder 将允许您折叠望远镜。

A PipelineBuilder would allow you to "collapse" the telescope.

Pipeline.newBuilder()
    .sequence()
        .stage(new StageFunction () {
            public function execute() {...}
        })
        .stage(new StageFunction () {
           public function execute() {...}
        })
    .build()

(即使我使用缩进方式也可以反映伸缩构造函数,这仅仅是装饰性的,而不是结构性的。)

(Even though I have used indentation in a way that is reflective of the telescoping constructors, this is merely cosmetic, as opposed to structural.)

这篇关于构造器模式和构造器之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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