0
When you have a Builder
for creating a template object the method build()
is responsible for creating this object, in many examples and materials I have seen this creation made in two ways:
- Using the model class constructor passing all values to properties as separate parameters.
public final class Cidade {
private Integer id;
private Integer codigo;
private String descricao;
private Cidade(final Integer codigo, final String descricao) {
this.codigo = codigo;
this.descricao = descricao;
}
public Integer getId() {
return id;
}
public Integer getCodigo() {
return codigo;
}
public String getDescricao() {
return descricao;
}
public static class Builder {
private Integer codigo;
private String descricao;
public static Builder create() {
return new Builder();
}
public Builder codigo(final Integer codigo) {
this.codigo = codigo;
return this;
}
public Builder descricao(final String descricao) {
this.descricao = descricao;
return this;
}
public Cidade build() {
return new Cidade(this.codigo, this.descricao);
}
}
}
- Using the model class constructor passing itself
builder
as a parameter and the properties in the constructor are fed from it.
public final class Cidade {
private Integer id;
private Integer codigo;
private String descricao;
private Cidade(final Builder builder) {
this.codigo = builder.codigo;
this.descricao = builder.descricao;
}
public Integer getId() {
return id;
}
public Integer getCodigo() {
return codigo;
}
public String getDescricao() {
return descricao;
}
public static class Builder {
private Integer codigo;
private String descricao;
public static Builder create() {
return new Builder();
}
public Builder codigo(final Integer codigo) {
this.codigo = codigo;
return this;
}
public Builder descricao(final String descricao) {
this.descricao = descricao;
return this;
}
public Cidade build() {
return new Cidade(this);
}
}
}
For me it is not clear and I would like to know when to use the constructor of the model object based on a Builder instead of passing parameters to the same?
"Better" only makes sense if there are comparison parameters. Each case is a case.
– Oralista de Sistemas
I changed the approach of the question @Renan, "better" really does not exist, my intention is to generate a discussion about when to use one or another type of approach, since this is still not clear to me.
– Lucas Boeing Scarduelli
Lucas, the OS is not directed to discussions. There is even a reason for the closure of questions, called "Mainly based on opinions". If you want to know which method to use, I suggest researching each one’s purpose - apparently you are studying Design Patterns (Design Patterns). But ultimately, the best teacher is the experience - use both in personal exercises and see which brings you the most benefits in each situation. While you’re doing this, if you have a problem that’s stalling your progress, here’s the place to ask yourself.
– Oralista de Sistemas
Ah, if you change the context of the question to "when should I use the X standard"... or better yet, "why does the X standard bring benefits over the Y standard", I believe people will see your question with better eyes around here.
– Oralista de Sistemas
Related: What good is a builder?
– rray
I think his question is valid. It has how to show the "pros" and "cons" of these approaches without going into the details of "opinion-based". Just put several examples and tell based on the experience which was the best option the developer found.
– Gabriel Katakura
It is the same thing as asking about which is the correct convention for JS. The community has a convention, but it doesn’t have a Baseship like MS that dictates what the C#convention is. After all, the community convention is the dominant opinion-based convention, but there are still a lot of people who answer these questions here in the OS and not the negative question.
– Gabriel Katakura