StringBuilder 和构建器模式

发布于 2024-10-21 01:55:40 字数 246 浏览 5 评论 0原文

我是设计模式的新手,所以我有一个关于构建器模式的问题。 今天听说Builder Pattern和Java、C#中的类StringBuilder不同。我知道 Builder 模式的主要目标是通过几个步骤创建复杂的对象...我认为这是用它的方法 Append 创建 StringBuilder ...所以它是我很难找到区别...

你能告诉我真的有什么区别吗?如果有的话...那是什么:)?

I'm new in Design Patterns so I have one question about the Builder Pattern.
Today I heard that Builder Pattern is different from the class StringBuilder in Java, C#. I know that main goal of the Builder Pattern is to create complex objects in few steps...I think that this is making StringBuilder with it's method Append...So it's hardly for me to find the difference...

Could you tell me is there really any difference and if it is...what's it :)?

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

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

发布评论

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

评论(3

人疚 2024-10-28 01:55:40

听起来您可能会混淆 StringBuilder 类和构建器设计模式。它们实际上是两种截然不同的想法。

StringBuilderJava.NET 允许更高效的字符串操作:(来自 MSDN)

String 对象是不可变的。每一个
当你使用其中一种方法时
System.String类,你新建一个
内存中的字符串对象,其中
需要重新分配空间
那个新对象。在以下情况下
你需要重复执行
对字符串的修改,
与创建相关的开销
new String 对象的成本可能很高。这
System.Text.StringBuilder类可以是
当你想修改字符串时使用
无需创建新对象。为了
例如,使用 StringBuilder 类
可以提高性能时
将许多字符串连接在一起
一个循环。

另一方面,构建器模式是一种设计模式,它是一组类和/或接口旨在组织复杂的代码:

构建器模式是一个软件
设计模式。其目的是
构造的抽象步骤
对象使之不同
这些步骤的实施可以
构建不同的表示
对象。通常,构建器模式是
用于构建产品
对于复合图案,结构
模式。

StringBuilderAppend 方法只是向现有字符串添加更多字符。没有创建新的对象(构建器模式的基本功能),并且不涉及设计模式。这是对单个对象实例的单个方法调用。

It sounds like you might be confusing the class StringBuilder and the Builder Design Pattern. They are actually two very different ideas.

StringBuilder is a class in Java and .NET that allows more performant string operations: (From MSDN)

The String object is immutable. Every
time you use one of the methods in the
System.String class, you create a new
string object in memory, which
requires a new allocation of space for
that new object. In situations where
you need to perform repeated
modifications to a string, the
overhead associated with creating a
new String object can be costly. The
System.Text.StringBuilder class can be
used when you want to modify a string
without creating a new object. For
example, using the StringBuilder class
can boost performance when
concatenating many strings together in
a loop.

The Builder Pattern on the other hand is a design pattern which is a set of classes and/or interfaces meant to organize complex code:

The builder pattern is a software
design pattern. The intention is to
abstract steps of construction of
objects so that different
implementations of these steps can
construct different representations of
objects. Often, the builder pattern is
used to build products in accordance
to the composite pattern, a structural
pattern.

The Append method of StringBuilder simply adds more characters to the existing string. There are no new objects created (basic function of the builder pattern) and there is no design pattern involved. It's a single method call to a single object instance.

生死何惧 2024-10-28 01:55:40
  1. 目的不同。字符串生成器有助于字符串构造,生成器模式将对象的构造与其表示分离。
  2. 结构不同。 String Builder 有两个协作者,Builder 模式有四个协作者。

ad 1) String 的创建没有抽象。例如,树结构是抽象的,可以用对象来表示,也可以用XML文件(实现)来表示。

ad 2) 当使用 StringBuilder 对象时,有两个协作者:

  1. Client
  2. StringBuilder

当使用 Builder 模式时,有四个协作者:

  1. Director - 它向 Abstract Builder 说明构建步骤是什么(如果是树结构,步骤是:创建根、添加child, ...)
  2. Abstract Builder - 是 Director 使用的抽象。
  3. Concrete Builder - 是 Abstract Builder 的具体实现(可以是 XMLBuilder、ObjectBuilder,...)
  4. Client - 对 Director< 说/code> 启动构建。
  1. Different purpose. String Builder helps with String Construction, Builder pattern separates the construction of an object from its representation.
  2. Different structure. String Builder has two collaborators, Builder pattern has four collaborators.

ad 1) There is no abstraction in creation of String. E.g., Tree structure is abstraction, which can be represented by objects or by XML file (implementation).

ad 2) When using StringBuilder object, there are two collaborators:

  1. Client
  2. StringBuilder

When using Builder Pattern, there are four collaborators:

  1. Director - it says to Abstract Builder what are the construction steps (In case of tree structure, steps are: create root, add child, ...)
  2. Abstract Builder - is abstraction used by Director.
  3. Concrete Builder - is concrete implementation of Abstract Builder (Could be XMLBuilder, ObjectBuilder,...)
  4. Client - says to Director to initiate the construction.
轻拂→两袖风尘 2024-10-28 01:55:40

分析文档,我可以说 StringBuilder 没有使用相应的模式。它使用复合模式。构建器模式不会“手动一步一步”或循环地收集对象。它有现成的解决方案。

Analyzing documentation, I can say that StringBuilder does not use corresponding pattern. It use Composite pattern. Builder pattern does not gather object "manually by your hands step-by-step" or in loop. It has ready-made solutions.

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