Questions tagged [builder-pattern]

Separate the construction of a complex object from its representation so that the same construction process can create different representations. This tag is a synonym of the more frequently used [builder]; please use that tag instead of this one.

This pattern allows a client object to construct a complex object by specifying only its type and content, being shielded from the details related to the object's representation. This way the construction process can be used to create different representations. The logic of this process is isolated from the actual steps used in creating the complex object, so the process can be used again to create a different object from the same set of simple objects as the first one.

Credit to OODesign Principle

This tag is a synonym of the more frequently used ; please use that tag instead of this one.

238 questions
822
votes
27 answers

What is the difference between Builder Design pattern and Factory Design pattern?

What is the difference between the Builder design pattern and the Factory design pattern? Which one is more advantageous and why ? How do I represent my findings as a graph if I want to test and compare/contrast these patterns ?
Penguen
  • 16,836
  • 42
  • 130
  • 205
140
votes
12 answers

Builder Pattern in Effective Java

I have recently started to read Effective Java by Joshua Bloch. I found the idea of the Builder pattern [Item 2 in the book] really interesting. I tried to implement it in my project but there were compilation errors. Following is in essence what I…
Swaranga Sarma
  • 13,055
  • 19
  • 60
  • 93
89
votes
12 answers

Builder pattern code generation in IntelliJ

Is there any way to automate writing Builder patterns in IntelliJ? For example, given this simple class: class Film { private String title; private int length; public void setTitle(String title) { this.title = title; } public…
Martyn
  • 16,432
  • 24
  • 71
  • 104
85
votes
7 answers

Builder pattern equivalent in Python

In Java, you can use the builder pattern to provide a more readable means to instantiating a class with many parameters. In the builder pattern, one constructs a configuration object with methods to set named attributes, and then uses it to…
name_masked
  • 9,544
  • 41
  • 118
  • 172
48
votes
10 answers

How to improve the builder pattern?

Motivation Recently I searched for a way to initialize a complex object without passing a lot of parameter to the constructor. I tried it with the builder pattern, but I don't like the fact, that I'm not able to check at compile time if I really set…
tangens
  • 39,095
  • 19
  • 120
  • 139
34
votes
9 answers

Builder pattern vs. config object

The builder pattern is popular to create immutable objects, but there is some programming overhead to create a builder. So I wonder why not simply using a config object. The usage of a builder would look like this: Product p =…
deamon
  • 89,107
  • 111
  • 320
  • 448
23
votes
10 answers

Where would you use a Builder Pattern instead of an Abstract Factory?

I've seen this question rise here and there a few times, but I never found and answer I was happy with. From Wikipedia: Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either…
Lino Rosa
  • 528
  • 4
  • 9
17
votes
1 answer

Builder pattern with nested objects

Hi I'm stuck with a problem. I want to implement the builder pattern to make creating my objects easier. The problem I face has to do with nested object. The object I would like to create has a list of other objects in it, and I don't really have an…
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
14
votes
7 answers

How to ensure that builder pattern is completed?

EDIT: I am not worried about being called in the wrong order since this is enforced through using multiple interfaces, I am just worried about the terminal method getting called at all. I am using a builder pattern to create permissions in our…
Sled
  • 18,541
  • 27
  • 119
  • 168
13
votes
2 answers

Automatic generation of immutable class and matching builder class

What tools/libraries exist that will take a struct and automatically generate an immutable wrapper and also a "builder" class for incrementally building new instances? Example input: struct Foo { public int apples; public int oranges; …
finnw
  • 47,861
  • 24
  • 143
  • 221
12
votes
3 answers

UnsupportedOperationException is thrown with Lombok Builder annotation

I am using Lombok for my project. My model looks like: @Builder @Data @AllArgsConstructor public class ScreenDefinitionDTO { @Singular private List screens; } I want to do next operation: String screenName =…
catch23
  • 17,519
  • 42
  • 144
  • 217
12
votes
1 answer

Should i use builder pattern in DTO?

This might be a pretty subjetive question, but i would to know some more opinions. I've built a Rest API service with Spring MVC, and i implemented the DTO-Domain-Entity pattern. I want to know what do you think about implementing the Builder…
jscherman
  • 5,839
  • 14
  • 46
  • 88
11
votes
4 answers

Java generic builder

Suppose I need some DerivedBuilder to extend some BaseBuilder. Base builder has some method like foo (which returns BaseBuilder). Derived builder has method bar. Method bar should be invoked after method foo. In order to do it I can override foo…
PepeHands
  • 1,368
  • 5
  • 20
  • 36
10
votes
4 answers

Set a value at most once with the builder pattern

Is there a standard practice in Java, while using the builder pattern, to ensure that a member variable is set at most once. I need to make sure that the setter is called 0 or 1 times but never more. I would like to throw a RuntimeException of some…
bigfy
  • 145
  • 2
  • 7
1
2 3
15 16