Questions tagged [builder]

An object creation software design pattern, one of the Gang of Four's creational design patterns.

The Builder design pattern separates the construction of a complex object from its representation.

It has two common variations:

  • The pattern as outlined in the book "Design Patterns" by the "Gang of Four" (Gamma, Helm, et al) (an example can be found on the Wikipedia page)
  • The pattern as outlined in the book "Effective Java" by Joshua Bloch (Example)

The latter is in many ways a consolidation of the participants in the GoF pattern. For example, the director is typically the product, and there is typically no "abstract builder", only a single concrete builder.

Builder is usually very helpful in solving the problem of telescoping constructors by reducing the combinations of constructors required for optional construction-time fields.

This is one of the Gang of Four's creational , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".

1895 questions
17
votes
3 answers

Real-world examples of the Builder pattern

I would like to see how is Builder pattern used in real world applications/APIs. The examples I found are all pizzas, cakes, cars et cetera (plus the parser example from the GoF book). Could you please tell me about some usages of this patten in…
Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
17
votes
1 answer

Complex queries with JPA Criteria builder

Can someone suggest me how to build up the following query using JPA Criteria builder API? SELECT id,status,created_at from transactions where status='1' and currency='USD' and appId='123' order by id It's better if I can find a solution which…
Aina Ari
  • 273
  • 2
  • 3
  • 6
17
votes
5 answers

disadvantages of builder design pattern

What would be the disadvantages of using the builder design pattern. Are there any?? edit - I want to know whether there is any bad consequence of using builder design pattern? As in the GOF book, they have mentioned the good and bad consequences of…
agrawalankur
  • 309
  • 2
  • 3
  • 15
17
votes
1 answer

Eclipse - What is exactly a Builder?

I don't understand what is exactly a builder in CDT, and what is the relationship with the "C/C++ Build" content. I set up SCons in the "C/C++ Build" confuguration. It does work : I made two configurations (release and debug), and my SCons scripts…
Oodini
  • 1,092
  • 6
  • 11
  • 22
16
votes
3 answers

Differences between builder pattern and template method (builder vs template)

Template pattern provides algorithm in the base class whose steps can be modified in the derived class. In Builder pattern, concrete builder exposes methods for building a product which are called from the Director class. I understand there is…
Sandeep G B
  • 3,957
  • 4
  • 26
  • 43
16
votes
2 answers

FindBugs detecter for NonNull Lombok builder attributes

I have a lot of classes with @NonNull fields using Lombok builders. @Builder class SomeObject { @NonNull String mandatoryField1; @NonNull String mandatoryField2; Integer optionalField; ... } However, this gives the caller the option…
John Bupit
  • 10,406
  • 8
  • 39
  • 75
16
votes
2 answers

Ruby XML::Builder with hyphen in Element name

I'm trying to generate some XML using XML::Builder, but my element names need to have hyphens in it. When I try I get undefined methods, with the element name being truncated at the hyphen xml.instruct! xml.update-manifest do xml.latest-id…
aussiegeek
  • 2,930
  • 3
  • 23
  • 29
16
votes
1 answer

Builder pattern with inheritance

I want to represent an web service URL request as an object, and found that there are lots of common parameters that could be "bubbled up" in an inheritance hierarchy. A request could have lots of parameters, some mandatory and other optional, for…
Bruno Kim
  • 2,300
  • 4
  • 17
  • 27
15
votes
3 answers

Java Builder pattern and a "deep" object hierarchy

What is the best practice for using Builder pattern in "deep" object hierarchies? To elaborate, I explored the idea of applying the Builder pattern as proposed by Joshua Bloch, to my XML binding code (I am using SimpleXML but this question would…
curioustechizen
  • 10,572
  • 10
  • 61
  • 110
15
votes
5 answers

Builder pattern: making sure the object is fully built

If for example I have a builder set up so I can create objects like so: Node node = NodeBuilder() .withName(someName) .withDescription(someDesc) .withData(someData) .build(); How can I make sure that…
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
15
votes
4 answers

Java best way to implement builder pattern

Which of the following is the better approach to implement the builder pattern? 1) Using the object to build instead of all its properties in the builder (and create it in the builder constructor): public class Person { private String…
raxell
  • 677
  • 6
  • 19
15
votes
6 answers

Setters AND ( not OR or VS ) builder patterns

I have a situation where I use a builder pattern for constructing an object. Best example to give is the pizza code public class Pizza { private int size; private boolean cheese; private boolean pepperoni; private boolean bacon; public…
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
15
votes
7 answers

Example of Builder Pattern in Java API?

Joshua Bloch's Effective Java describes a Builder Pattern that can be used to build objects with several optionally customizable parameters. The naming convention he suggests for the Builder functions, which "simulates named optional parameters as…
Fostah
  • 11,398
  • 10
  • 46
  • 55
14
votes
4 answers

JavaWorld on OO: Getters/Setters vs Builder

Background: I found this article on JavaWorld, where Allen Holub explains an alternative to Getters/Setters that maintains the principle that the implementation of an object should be hidden (his example code can also be found below). It is…
wen
  • 3,782
  • 9
  • 34
  • 54
14
votes
3 answers

Factoring out repeated constructor calls in template-builder pattern

Consider the following builder-like class, which ultimately allows me to construct an object with both certain (runtime) values for member variables, as well as embedding some behavior which is carried by several (compile-time) types. The same build…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386