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

Is there a clean way to use the Builder pattern to build a multi-level tree?

It seems like the builder pattern is good if you're making some linear chain of things (java's StringBuilder) or creating an object with many properties (PizzaBuilder). Can it be extended to build a a tree without specifying possibly confusing node…
marathon
  • 7,881
  • 17
  • 74
  • 137
5
votes
2 answers

How to efficiently create and use the builder pattern

On our last project we ended up with a shared test fixture for our unit tests which gave a lot of problems. So on our current project I've looked into the builder pattern. We run our unit tests in memory on the development machines and against the…
Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
5
votes
2 answers

Joshua Bloch's Builder pattern and PMD warnings

I have written a class using Joshua Bloch's Builder pattern, which is similar to this Pizza example: public class Pizza { private int size; private boolean cheese; private boolean pepperoni; private boolean bacon; public static class…
chance
  • 6,307
  • 13
  • 46
  • 70
5
votes
1 answer

How to use builder pattern and inheritance with same attributes

In fact, I would like to ask if my approach is correct, as maybe I should not use builder pattern right here. I currently have the following class CsvItem: public class CsvItem { private CsvItemGroup group; private CsvItemEntity entity; …
5
votes
1 answer

How to have multiple builder pattern in a single builder class?

I have two types of payload coming from upstream: It's either PayloadA or PayloadB. PayloadA containes these many fields: createTimestamp sentTimestamp schemaId eventId producerId guid deviceId langId sessionId PayloadB containes these many…
john
  • 11,311
  • 40
  • 131
  • 251
5
votes
2 answers

Is it possible to hide variables from lambda's closure?

I'm trying to crate a type-safe groovy-style builder in Kotlin like it's described here. The problem is visibility of lambda receivers in nested lambdas. Here is a simple example. html { head(id = "head1") body() { head(id =…
Alexey Pomelov
  • 196
  • 1
  • 9
5
votes
4 answers

Builder Pattern useful with Setter-methods?

So I have a webproject with Hybris, Spring and so on. I have some classes, which are autogenerated. Let's say I have one modelclass, which is autogenerated and inherits from another class some methods, to set fields. When writing Unit-tests, is it…
user5417542
  • 3,146
  • 6
  • 29
  • 50
5
votes
2 answers

How should I approach wrapping the Composite pattern into the Builder pattern?

Lets say that I have a Composite set up as follows: public abstract class Element { //position, size, etc. //element methods //setters/getters } public class SimpleElement1 extends Element { //... } public class SimpleElement2…
Nebojša
  • 51
  • 1
  • 2
4
votes
1 answer

Is there more to the builder pattern than what named optional parameters offer?

Head First Design Patterns only briefly describes the builder pattern in the appendix, without dedicating to it as much space as to other patterns. Design Patterns: Elements of Reusable Object-Oriented Software treats it just like other design…
4
votes
1 answer

When using the builder pattern in C++, is it advisable for the setters to return a reference to the builder object?

I am thinking of using the builder pattern in C++ unit tests, to streamline the creation of input data for the code being tested. In Java the common idiom seems to be to have the setters of the builder class return the (reference to) the builder…
Dima
  • 38,860
  • 14
  • 75
  • 115
4
votes
1 answer

Unit testing Builder pattern with Moq

I'm using the builder pattern to generate viewmodels for the controller and when I was trying to unit test my controller I found myself unable to do so. Moq complains. Not sure whether it's a Moq limitation or my own limitation and ignorance. This…
Yag
  • 546
  • 2
  • 7
  • 12
4
votes
2 answers

How can I build an object using a varying number of command line arguments?

I use the builder pattern to make an object. The problem is that I have to get arguments from command line. I want to pass one to six arguments and, based on the arguments amount, call the relevant methods. For example, if I pass 3 args, the program…
Alxa
  • 79
  • 1
  • 2
  • 11
4
votes
2 answers

Should Builder.build() return a default state?

Using the Builder pattern there is always the question whether the fields do have a default value or not? I cannot find a reliable source where that's clearly defined... The problem is readability: What is Car.Builder().build() returning? I always…
mbo
  • 4,611
  • 2
  • 34
  • 54
4
votes
2 answers

How to enforce order of setting parameters through Builder Pattern

Recently, I was asked in an interview to implement Builder Pattern in such a way that user cannot set a parameter B unless parameter A is set. I suggested to pass A as a required parameter when constructing the Builder but he was not satisfied with…
rakku
  • 73
  • 6
4
votes
2 answers

lombok - @Builder pattern in multiple shots

I use @Builder of lombok project, so consider I have this example: @Builder public class Client { private @Getter @Setter Integer id; private @Getter @Setter String name; } Which is equivalent to: public class Client { private…
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1 2
3
15 16