20

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 :)?

Matt
  • 14,353
  • 5
  • 53
  • 65
  • Related post - [Is “StringBuilder” an application of the Builder Design Pattern?](https://softwareengineering.stackexchange.com/q/305504/236257) – RBT May 28 '18 at 12:35

3 Answers3

25

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.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • Can you quote any reference which says that new objects are created in midway when we're using Builder pattern? Or maybe I got your statement wrong. Kindly clarify. – gaurav jain Nov 10 '15 at 08:27
  • @gauravjain in the builder patern it all depends on the implementationof what you are building. Depends on the programmer. The string builder is already an implementation of a builder pattern and is designed specifically to NOT create intermediate objects to make string concatenationuch faster. Look up MsDN documents on the String builder class and you should see it spelled out in the first or second paragraph. – Paul Sasik Nov 10 '15 at 15:15
  • 1
    Hey Paul, I think you are not right, when saying that under Builder pattern and StringBuilder's implementation lie different ideas. Saying so sounds like pattern has only one implementation (or one type of implementation), that eliminates all flexibility patterns were designed for. I consider StringBuilder to be as valid Builder implementation as Bloch's Builder. Both are all about step-by-step object construction and there is no difference whether the purpose of step-by-step building is immutable object or performance optimization. – Ivan Nikolaychuk Sep 17 '16 at 10:15
8
  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.
jk_
  • 5,448
  • 5
  • 25
  • 23
  • +1 I like the idea of collaborators when discussing objects and their interactions in this way. You hear about client code, client/server relationships all the time but collaborators, that's new to me. Would you happen to have a link handy to an article that would expand on the idea? Thanks! – Paul Sasik Jun 21 '13 at 13:32
0

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.

Oleksandr H
  • 2,965
  • 10
  • 40
  • 58
  • 1
    I don't think it really fits the description of `Composite Pattern` so much as it fits the pattern--I don't know the name--of having a pair of associated immutable and mutable classes, such that an immutable object's state can be copied to a mutable object, and a mutable object's state can be copied to a new immutable object. Code may then efficiently manipulate an unshared mutable object to yield some desired state, and then ask it to produce a sharable immutable object encapsulating that same state. – supercat Apr 16 '14 at 19:36