1

It has been suggested in a previous post that StringBuilder is an example of the Builder Design Pattern, however it is quite different from the version which is described by the gang of four, and it seems that the design pattern they describe is more like the following: http://www.oodesign.com/builder-pattern.html

Are these both examples of the Builder Design Pattern, and if they are, what is the common denominator between them?

As well what is the purpose of the diamond on the director? Does that imply a composite of one Concrete builder or many?

Community
  • 1
  • 1
aruuuuu
  • 1,605
  • 2
  • 22
  • 32

3 Answers3

1

Implementations may differ to one another, but in general, they follow the same approach.

As you may know, the Builder pattern can be juxtaposed with a medicine recipe - for example, the doctor can add more and more drug prescriptions in a single recipe and finally, when ready, give you the complete recipe.

When constructing an object with the Builder patter, we do something similar - invoke methods, that serve as instructions of how our object will be built and when we're ready we get the object with a terminal method, for example called build().

Another example of a Builder implementation is the java.util.stream.Stream class, which is part of the Stream API in Java 8.

It works in a very similar manner - you can invoke as much as non-terminal operations you nee on a Stream and finally get a built Stream, based on the list of provided criteria. For example:

List<String> names = Arrays.asList("John", "Peter", "Stephen");
//Let's build a Stream, which will give a list of the sizes of each of our names
List<Integer> namesSizes = names
                          .stream()
                          .map(name -> name.length())
                          .collect(Collectors.toList()); //terminal operation

More info:

Community
  • 1
  • 1
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • The "[Examples of the GoF design patterns](http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns)" is the very post i'm asking a question about... Specifically, are there two forms of the builder pattern? Because the builder example he gives in that post - and on the internet - it seems that many attribute a different implementation to the builder pattern and call it Builder – aruuuuu Nov 14 '14 at 14:30
  • As far as I know, in general, no - there's only one form of the Builder pattern. Another form may not meet the properties of the pattern - if the constructed object is not immutable, for example – Konstantin Yovkov Nov 14 '14 at 14:34
1

I had been quite puzzled by this too. As far as my understanding goes, a lot of "Builder design pattern" examples are based on Josh Bloch's effective Java Item#2; which can be considered as a simplification of GoF's Builder pattern. It looks like Bloch's implementation targets to introduce simplicity when there are way too many ways to construct a concrete object - or there are slightly different ways a concrete object represents itself and thus causes too many constructors with similar type of arguments - Example : with optional and mandatory attributes. (Anti-pattern of telescopic constructors are solved using Bloch's builder pattern)

GoF have another level of indirection to the whole creational process via "Director" to encapsulate knowledge regarding what steps are required to build a product - this creational algorithm remains common across the products - and an interface/base class to represent necessary builder interfaces (contracts) that knows how to do each step. Each builder implementation returns its own concrete product.

ring bearer
  • 20,383
  • 7
  • 59
  • 72
0

Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.

GOF Diagram (GOF builder) is just one of the examples who to implement idea of the Builder pattern.

Here is the concept important not implementation, because same idea can have different implementations and different diagrams.

Same is with singleton pattern. Idea is to restricts the instantiation of a class to one "single" instance. This can be achieved in multiple ways. GOF Diagram (GOF builder) is just one of the examples who to implement idea of the Builder pattern.

Here is the concept important not implementation, because same idea can have different implementations and different.

Same is with singleton pattern. Idea is to restricts the instantiation of a class to one "single" instance. This can be achieved in multiple ways.