When to use the Builder-based model object builder instead of passing parameters?

Asked

Viewed 225 times

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:

  1. 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);
        }

    }

}
  1. 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.

  • 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.

  • 1

    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.

  • 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.

  • 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.

  • 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.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.