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

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

问题描述

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

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 实际上只是一个美化的构造器,而构建器模式只是一种方式构建一个类似于构造函数的对象".

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)

对比

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

构建对象图

类似于大量参数"场景,我认为构建器最引人注目的场景是构建复杂的对象图.这个问题中的其他答案是指伸缩反模式.这种情况(构建复杂的对象图)可能会导致伸缩",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,而 Sequence 依赖于 Stage.PipelineBuilder 不仅可以为 Pipeline 的构造函数提供一个很好的包装器,还可以为构造函数 SequenceStage 提供一个很好的包装器>,允许您从单个 Builder 界面组成复杂的 Pipeline.

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() {...}
            }
        )
    )
)

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