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
10
votes
2 answers

How to use default value in the builder pattern if that value is not passed and also make thread safe?

I am trying to use Builder Pattern for my class.. Below is my Builder class which I have built by following Joshua Bloch version as he showed in Effective Java, 2nd Edition. Our customer will mostly pass userId, clientId always but other fields are…
AKIWEB
  • 19,008
  • 67
  • 180
  • 294
10
votes
1 answer

Can I configure myBatis to create an instance of a class using the Builder Pattern?

I have a (Java) class with many instance fields (many of which are optional). I would like all fields (thus class) to be immutable. So, I would like to use the Builder Pattern for constructing instances of the class. Can I configure myBatis to…
James
  • 2,876
  • 18
  • 72
  • 116
9
votes
3 answers

Is this a valid Java implementation of an immutable class and the Builder pattern?

The Builder implements Cloneable and overrides clone() and instead of copying every field of the builder, the immutable class keeps a private clone of the builder. This makes it easy to return a new builder and create slightly modified copies of an…
Aksel
  • 533
  • 4
  • 12
8
votes
4 answers

How to make a builder for a Kotlin data class with many immutable properties

I have a Kotlin data class that I am constructing with many immutable properties, which are being fetched from separate SQL queries. If I want to construct the data class using the builder pattern, how do I do this without making those properties…
Brian Voter
  • 131
  • 1
  • 5
8
votes
2 answers

Java Builder generator problem

In a project of mine I have two packages full of DTOs, POJOs with just getters and setters. While it's important that they are simple java beans (e.g. because Apache CXF uses them to create Web Service XSDs etc.), it's also awful and error-prone to…
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
8
votes
2 answers

Making Bloch's builder pattern thread-safe: Rechecking necessary in enclosing constructor if NEVER invalid?

I have recently learned Joshua Bloch's builder pattern for creating objects with many optional fields. I've been using something like it for years, but never used an inner-class until Bloch's book suggested it to me. I love it. I understand that…
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
8
votes
3 answers

What is the difference between Builder Pattern and Flyweight Pattern?

What is the difference between Builder Pattern and Flyweight Pattern in terms of usage, as both of them deals with large number of objects?
user366312
  • 16,949
  • 65
  • 235
  • 452
7
votes
1 answer

Why is Builder pattern better than a Constructor with arguments in the Class's object being created?

Why can we not the different build steps within the constructor itself. if the build steps take arguments why can't they be provided as arguments to constructor and utilized within constructor to create the object. AFAIK, in Builder pattern, the…
user855
  • 19,048
  • 38
  • 98
  • 162
7
votes
2 answers

Builder Pattern: which variant is preferred?

I was going through Effective Java book , and creating notes for my future reference , i came across Builder Pattern. Well i understood what it is and how its suppose to be used.In the process i created a two example variations of the builder…
Sudhakar
  • 4,823
  • 2
  • 35
  • 42
6
votes
0 answers

Using lombok's @Builder with inheritance and Jackson

I am trying to use lombok's @Builder with inheritance and Jackson. I was building things from https://reinhard.codes/2015/09/16/lomboks-builder-annotation-and-inheritance/, https://gist.github.com/pcarrier/14d3a8e249d804cfbdee and Builder pattern…
user2131376
  • 81
  • 1
  • 3
  • 8
6
votes
3 answers

Understanding Builder Pattern in C#

Definition of Builder Pattern: The Builder pattern separates the specification of a complex object from its actual construction. The same construction process can create different representations. well i have a code sample of Builder Pattern like…
Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
6
votes
1 answer

When using a Builder Pattern why shouldn't I reuse the builder-object to access the object configuration?

when using a Builder Pattern why shouldn't I reuse the builder-object to access the object configuration? For example: Normal way: ObjectA(ObjectBuilder b) { this.a = b.getA(); } public Object getA(){ return this.a; } But why can't I just…
A.Non
  • 71
  • 1
  • 3
6
votes
4 answers

What would be considered good examples of implementing the builder pattern when used in the development of a GUI?

I am a complete newbie when it comes to the use of factory classes and methods, patterns, etc - in fact I first learned of them here on Stackoverflow when browsing Java related questions :-) In response to a previous question of mine it was…
The Thing
  • 625
  • 1
  • 9
  • 23
6
votes
4 answers

Builder Pattern : Why does the Director construct the object?

I am learning the Builder Pattern In the above link (Java example), I noticed that the Builder offers interface to construct multiple components. Along with invoking them, we call the getProduct() as well. The point I don't understand is that why…
phanin
  • 5,327
  • 5
  • 32
  • 50
6
votes
1 answer

Best way to handle object's fields validation => Either / Try (scala 2.10) / ValidationNEL (scalaz)

Let's assume an object constructed using a builder pattern. This builder pattern would contain a build method focusing on fields validation and then on conversion to the targeted type. This validation could be implemented…
Mik378
  • 21,881
  • 15
  • 82
  • 180
1
2
3
15 16