59
votes
Why do we need a Builder class when implementing a Builder pattern?
Why use/provide a builder class:
To make immutable objects — the benefit you've identified already. Useful if the construction takes multiple steps. FWIW, immutability should be seen ...
34
votes
Accepted
Why do we need a Builder class when implementing a Builder pattern?
It's so you can be immutable AND simulate named parameters at the same time.
Person p = personBuilder
.name("Arthur Dent")
.age(42)
.build()
;
That keeps your mitts off the ...
21
votes
Why do we need a Builder class when implementing a Builder pattern?
One reason would be to ensure that all of the passed-in data follows business rules.
Your example doesn't take this into consideration, but let's say that someone passed in an empty string, or a ...
16
votes
Accepted
Is the builder pattern applicable in domain driven design?
After looking at your other question I don't believe you are talking about the gang of fours builder pattern. I think you're talking about Josh Bloch's builder pattern.
It lets you write code like ...
9
votes
Accepted
Design pattern to force client of a class to call a method
You can use builder pattern. The "WithX" stuff is not really main purpose of builder and is just syntax sugar to make some usage simpler.
Your code, using builder, could very well look like this :
...
9
votes
Builder pattern: How to verify required fields before runtime
If you actually need all 10 parameters, put them all in the constructor. 10 parameters in the constructors is a little weird and annoying, but far less annoying than weird designs or chasing down bugs....
9
votes
Is it a good idea to return a Builder from a Factory?
As always, you can do what is best suited to your domain and concrete problem.
In any case, this looks like mixing a concrete factory with the Bloch builder pattern. In this case, I do not see the ...
7
votes
Accepted
Builder pattern: How to verify required fields before runtime
Yes
In the builder, you are free to define the return types of the methods. It is often the builder itself, but it doesn't have to be. You can define additional interfaces.
E.g
IBuilderMail ...
6
votes
Accepted
Is it an antipattern to introduce complexity into a builder?
Short answer: I like it, and I make builders with the same characteristics. I don't think this is an anti-pattern at all.
Details:
Can hide details of the type being created
If the details are ...
6
votes
Is the builder pattern applicable in domain driven design?
I don't think that DDD and the builder pattern are mutually exclusive, however the builder is just a utility to help construct the domain model.
If your Domain object and your Builder are separate ...
6
votes
Accepted
Design pattern: How to inject dependencies into a Command pattern
Before answering your question, one should firstly know the goal developers are trying to achieve with the command pattern in the first place. More often than not, the pattern's purpose is to decouple ...
6
votes
Is it a good idea to return a Builder from a Factory?
A Factory normally returns different implementations of a class/interface, while a builder gives a fluent interface for configuring a single type.
So if you factory returns a builder you might expect ...
5
votes
Why do we need a Builder class when implementing a Builder pattern?
A little different angle on this from what I see in other answers.
The withFoo approach here is problematic because they behave like setters but are defined in a way that make it appear the class ...
5
votes
Accepted
Builder Pattern - Must it be part of The same Class?
Do I need the Builder to be inside my complex class blueprint, or Could I actually have it outside?
No, you don't need it to be inside. I personally dislike using classes as namespaces and always put ...
4
votes
When should the builder design pattern be used?
What I haven't seen mentioned in the answers above is the following.
Using the builder pattern makes for cleaner code.
Cleaner code
Say you've created a library which exposes a POJO that's ...
4
votes
Accepted
what is an empty method and how are they used?
An empty method is just a method, which is to say a function that's part of a class, that doesn't do anything other than perhaps returning some default value.
The author defines that in C++ could be ...
4
votes
Why do we need a Builder class when implementing a Builder pattern?
Re-use builder object
As others have mentioned, immutability and verifying the business logic of all fields to validate the object are the major reasons for a separate builder object.
However re-...
4
votes
Builder Pattern: Is it acceptable to use "passing-by-reference" on Director methods?
The Other Builder Pattern
First, let me just briefly mention that there's a different pattern under the same name: Joshua Bloch's Builder pattern described in Effective Java (see the excerpt here). ...
4
votes
Accepted
GoF Builder Pattern Applicability
This is a misunderstanding of the builder pattern. The whole purpose of the pattern is to decouple the building process from the concrete classes that are built. Let's have a second reading:
The ...
4
votes
Accepted
GOF class diagram for Builder pattern appears to contradict its corresponding sequence diagram
The GOF is pre-UML. It uses a variant of OMT, a notation invented by J.Rumbaugh, before it was merged into UML:
The current UML terminology is "association". OMT made a subtle difference ...
4
votes
Is it a good idea to return a Builder from a Factory?
With a Bloch Builder you get the default values like this:
//Get a fully configured Employee with default values
Employee employee = employeeBuilder.build();
If that blows up, it doesn't offer ...
3
votes
Designing a builder as a compile-time state machine
The only meaningful way I can think of "the builder with internal state" is something I've read long time ago in this blog post.
The idea is similar to proposals in comments which can achieved with ...
3
votes
Accepted
Best methods to account for large block of variables?
If I got your right, you have a bunch of unstructured data to be passed around, and since you now see this will cause issues, you try to give it some local structure just for calling one method. Of ...
3
votes
Builder Pattern - Must it be part of The same Class?
Since the class's builder does not make sense in separation from the class it builds, there is a widespread practice to make the builder a nested static class within the target class:
class Foo {
//...
3
votes
Can a GoF Builder implementation be considered valid without an explicit getProduct()?
The builder pattern is meant to be a nicer way to construct an object that requires many or complex dependencies. It is only at the end of the builder that the object is created. If an exception gets ...
3
votes
Accepted
Is it a code smell to have two different implementations of the builder design pattern, for the same model?
Your question does not really contain "two different implementations of the same builder pattern". Instead, it describes two common, but actually different uses of the name "Builder ...
3
votes
Accepted
How to efficiently build objects without default constructors
In this way, we are guaranteed to have valid/intended values for each field once an object of Ball is created.
Stating that there can be no default ball does not inherently limit the existence of a ...
3
votes
Is the process of building a model from a dictionary necessarily the builder pattern?
Your approach to constructing the PhoneModel from the rawPhoneData dictionary does share some similarities with the builder design pattern, but there are some key differences.
Builder Design Pattern:
...
3
votes
Accepted
Is it a good idea to return a Builder from a Factory?
This design decision hinges on whether you need separate derived types of employees. If Employee is an interface or abstract type, there are behavioral differences between the types of employees, and ...
2
votes
Accepted
Builder with constructor or factory method?
Builder is a class internal to Dot. For me this tells me that Builder is strongly coupled with Dot and that Builder is a utility of Dot for some objective, in this case object construction.
This is ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
builder-pattern × 61design-patterns × 42
java × 16
c# × 9
object-oriented × 9
factory × 7
design × 5
object-oriented-design × 5
constructors × 4
class-design × 3
factory-method × 3
c++ × 2
api-design × 2
teamwork × 2
architecture × 1
php × 1
python × 1
domain-driven-design × 1
web-development × 1
.net × 1
data-structures × 1
interfaces × 1
dependency-injection × 1
uml × 1
asp.net × 1