Questions tagged [builder-pattern]

55 questions
37
votes
9 answers

Why do we need a Builder class when implementing a Builder pattern?

I have seen many implementations of the Builder pattern (mainly in Java). All of them have an entity class (let's say a Person class), and a builder class PersonBuilder. The builder "stacks" a variety of fields and returns a new Person with the…
Boyan Kushlev
  • 483
  • 1
  • 5
  • 9
33
votes
1 answer

Is "StringBuilder" an application of the Builder Design Pattern?

Is the "Builder" pattern restricted to addressing the "telescoping constructor" anti-pattern, or can it be said to also address the more general problem of complicated creation of immutable objects? The StringBuilder class has the word "builder" in…
Mike Nakis
  • 32,003
  • 7
  • 76
  • 111
28
votes
4 answers

Do named arguments replace the builder pattern?

When using a language that supports named and optional arguments, does the builder pattern no longer have a practical use? Builder: new Builder(requiredA, requiredB).setOptionalA("optional").Build(); Optional/named arguments: new Object(requiredA,…
Paul Nikonowicz
  • 445
  • 4
  • 9
26
votes
3 answers

Constructor with tons of parameters vs builder pattern

It is well know that if your class have a constructor with many parameters, say more than 4, then it is most probably a code smell. You need to reconsider if the class satisfies SRP. But what if we build and object that depends on 10 or more…
Narek
  • 1,133
  • 1
  • 9
  • 19
23
votes
5 answers

Why would a type be coupled with its builder?

I've recently deleted a java answer of mine on Code Review, that started like this: private Person(PersonBuilder builder) { Stop. Red flag. A PersonBuilder would build a Person; it knows about a Person. The Person class shouldn't know anything…
Mathieu Guindon
  • 1,720
  • 16
  • 33
22
votes
8 answers

How can I promote the use of the Builder pattern in my team?

Our codebase is old and new programmers, like myself, quickly learn to do it the way it's done for the sake of uniformity. Thinking that we have to start somewhere, I took it upon myself to refactor a data holder class as such: Removed setter…
rath
  • 856
  • 8
  • 20
14
votes
5 answers

Is it strange for a Builder object to have getter methods?

I have a fairly complex immutable data type that I'm using a builder object to instantiate. Currently, I have a setup where I parse a file, setting various fields in my builder, and then build the object. However, the files I'm using are…
codebreaker
  • 1,694
  • 1
  • 18
  • 24
11
votes
3 answers

Is there any point in using builders and fluid interfaces with object initialisers?

In Java and C#, you can create an object with properties that can be set at initialisation by either defining a constructor with parameters, defining each property after constructing the object, or using the builder/fluid interface pattern. However,…
svbnet
  • 221
  • 2
  • 5
11
votes
4 answers

Java: How to implement a step builder for which the order of setters doesn't matter?

Edit: I'd like to point out that this question describes a theoretical problem, and I am aware that I can use constructor arguments for mandatory parameters, or throw a runtime exception if the API is used incorrectly. However, I am looking for a…
derabbink
  • 210
  • 2
  • 7
9
votes
1 answer

How should I handle incompatible configurations with the Builder pattern?

This is motivated by this answer to a separate question. The builder pattern is used to simplify complex initialization, especially with optional initialization parameters). But I don't know how to properly manage mutually exclusive…
kdbanman
  • 1,447
  • 13
  • 19
8
votes
3 answers

Why is the builder-pattern often implemented like this?

Often I see the implementation of the builder pattern (in Java) to be like this: public class Foo { private Foo(FooBuilder builder) { // get alle the parameters from the builder and apply them to this instance } public static…
Andy
  • 1,305
  • 7
  • 13
8
votes
3 answers

How to deal with constructors in large data classes

Several times now I have come across the situations where you have some kind of settings class that simply contains a mass of data. Often these classes are simply not valid without at least most of the data. However a constructor like this: public…
Thijser
  • 191
  • 1
  • 5
7
votes
3 answers

Design pattern: How to inject dependencies into a Command pattern

I am pretty new to programming languages and only have limited knowledge about design patterns, so I hope you can help me with the following problem: I have an application that operates on a group of different services. One functionality of the…
6
votes
1 answer

Approach for Constructing View Models in Complex MVVM Application

I'm struggling with the design in a WPF MVVM application. In a few courses I've taken, they say that having a lot of parameters in a constructor is a code smell, but they never address how to deal with it. In a recent project of mine we used…
6
votes
3 answers

Is the builder pattern applicable in domain driven design?

I asked a question on StackOverflow regarding how to 'best' use the Builder pattern for constructing a Value Object which could be constructed with or without optional parameters. One answer stated that: A builder is not part of the domain driven…
1
2 3 4