All Questions
Tagged with builder-pattern java
16 questions
5
votes
6
answers
2k
views
Is it a good idea to return a Builder from a Factory?
I would want to have a builder for creating a complex object, for example Employee.
I would want to be able to create my objects in two ways:
Case 1. Get Employee with default values
Case 2. Get ...
4
votes
2
answers
874
views
Is it a code smell to have two different implementations of the builder design pattern, for the same model?
Shoutout to David Arno for teaching me about the builder design pattern via this thread!
I have since used that pattern althroughout the code base to abstract out creating models from data stores, ...
1
vote
2
answers
1k
views
Is it wrong to extend an inner static class in java? (Builder pattern)
I'm working on a java project for the university. The project is a card game in which you travel around a 2D map and fight against some enemies. My part consists of creating the deck and the cards.
I ...
2
votes
2
answers
2k
views
Creational design pattern that allows configuration of objects
With the factory pattern we abstract the creation of objects. But what if we need a specific configuration of an object that depends on the calling context?
Example:
So I have a Builder pattern for ...
0
votes
0
answers
75
views
How to cope with terminal operations in a fluent API?
I am trying to polish my data-mapping library and struggle with my fluent API design as it feels clumsy in some basic use-cases.
The library focuses on defining mapping objects from one type into ...
0
votes
2
answers
754
views
Java - difference between constructor and calling the object multiple times [duplicate]
I read through a code example on github
and instead of initializing the object using a constructor, they made every setter return the object itself to call it over and over again
See, constructors
...
37
votes
9
answers
18k
views
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 ...
2
votes
2
answers
2k
views
Is it an antipattern to introduce complexity into a builder?
I've looked at various definitions of the builder pattern and whilst there's varying definitions, they tend to be focused on the broad definition of incremental construction. However, it seems that ...
2
votes
1
answer
7k
views
Builder with constructor or factory method?
Let's say I have a class Dot with a builder:
public class Dot {
private final Double x;
private final Double y;
private final Color color;
private Dot(Double x, Double y, Color color)...
5
votes
3
answers
2k
views
Is it a bad practice to use an object as a Builder's only field instead of mimicking the class fields?
I see this sometimes:
class SomeClass {
Object param1, param2, param3, param4;
private SomeClass(){}
static class Builder {
SomeClass someClassInstance = new SomeClass();
// ...
3
votes
1
answer
5k
views
How to handle "conditional fields" in Java?
I've run into several situations where a POJO where whether a field value is meaningful depends on the value of another field. An example, using Lombok (which we try to use to avoid boilerplate):
@...
5
votes
1
answer
15k
views
Is mixing Builders and Factory pattern a good idea?
I have an object Carconstructed using builder pattern.
Issue was a lot of code redundancy.
Car.Builder builder = new Car.Builder("Mercedes");
builder.numDoors(carConfig.getNumDoors()
...
3
votes
1
answer
253
views
Is Pairing a bloated interface with an Enum a good idea?
At work we have an interface that is getting bloated. The interface is designed to be easily implemented by an immutable object. So it looks something like this:
//there is no behavior here, just ...
14
votes
5
answers
11k
views
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 ...
23
votes
5
answers
2k
views
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 ...