Is it mandatory to put the same attributes in different constructors?

6

I have these builders:

// 1st Builder - Create a bike with a new frame

public Motociclo (String marca, String modelo, float peso, int robustez) { 
    //Nivel 2
    this.marca = validarMarca(marca);
    this.modelo = validarModelo(modelo);
    this.quadro = new Quadro(peso, robustez);
}

//2º Construtor - Criar mota com um quadro existente
public Motociclo (String marca, String modelo, Quadro quadro) { 
    //Nivel 2
    this.marca = validarMarca(marca);
    this.modelo = validarModelo(modelo);
    this.quadro = quadro;
}

//3º Construtor - Criar mota com um novo quadro, que tem cor
public Motociclo (String marca, String modelo, float peso, int robustez, Cor cor) {
    //Nivel 3
    this.marca = validarMarca(marca);
    this.modelo = validarModelo(modelo);
    this.quadro = new Quadro (peso, robustez, cor);
}

//4º Construtor - Criar mota com um quadro existente e um numero de quilometros
public Motociclo (String marca, String modelo, int quilometragem, int numeroRevisoes, Quadro quadro) {
    //Nivel 4
    this.marca = validarMarca(marca);
    this.modelo = validarModelo(modelo);
    this.quilometragem = quilometragem; //se for uma mota e segunda-mão, os quilometros não seram 0.
    this.numeroRevisoes = 0;
    this.quadro = quadro;

When we create a new attribute, for example mileage, is it mandatory to place it in other builders previously created or is it optional?

It is that in the solution to this exercise each time my teacher creates a new attribute, he places it in all the constructors.

    
asked by anonymous 05.12.2018 / 19:59

3 answers

8

It's the usual answer: it depends.

Before we start I have to show some answers to use the correct terms here:

Then you need to understand What is a builder for? . Only then can we analyze whether or not to place parameters when adding a new field.

Is this data mandatory on all objects of this type? If they are, they do not even have to think. If they are not then probably they should be added after the instance was created. But not always, it might be worth having convenience builders.

If you mess around in classes you may be fixing headaches to keep up with every application.

Software engineering is not following formulas, it is understanding the problem and doing what is best suited for that case. Without understanding the problem, without analyzing the requirements it is not possible to say whether or not to put a new parameter. Not knowing how likely this class will be consumed can not make up what to create just for convenience.

The profile of this code implies that they are not all that much needed, but I can not guarantee it. It is strange that each constructor has a different set of parameters almost completely isolated. I do not know, this looks like some conceptual error, after all, each object can be very different from the other, it does not make sense, so maybe the problem is not just the constructor, but every class has problems. With a better description of the problem I could speak more properly.

Of course, the teacher may be wanting only to pass the constructor mechanism, without paying attention to the correct concept. But I do not like this, I see that people start learning the wrong concept and never fix it anymore. I'm just speculating about the real situation.

Mandatory is not, but I do not know if it makes sense in this case. There has to be a reason to create such different builders.

It does not answer what was asked because the AP wants to know of heterogeneous constructors, not constructor chaining with the same parameters, but it would be nice to take a look at Gustavo Fragoso's answer that it is possible to avoid code duplication. And it's not just to write less, it's to stay more DRY .

    
05.12.2018 / 20:15
6

It has a very common practice that is used in these situations: use the keyword this() , which represents the constructor of the class itself, and fill the most generic constructor with default values. See the example for clarity:

public Motociclo (String marca, String modelo, float peso, int robustez) { 
    // Preenche o construtor mais genérico com valores padrão
    this(marca, modelo, peso, 0, 0, robustez, null, Color.BLACK);
}

public Motociclo (String marca, String modelo, Quadro quadro) { 
    // Preenche o construtor mais genérico com valores padrão
}

public Motociclo (String marca, String modelo, float peso, int robustez, Cor cor) {
    // Preenche o construtor mais genérico com valores padrão    
}

public Motociclo (String marca, String modelo, int quilometragem, int numeroRevisoes, Quadro quadro) {
    // Preenche o construtor mais genérico com valores padrão
}

public Motociclo (String marca, String modelo, float peso, int quilometragem, int numeroRevisoes, int robustez, Quadro quadro, Cor cor) {
    this.marca = marca;
    this.modelo = modelo;
    this.peso = peso;
    this.quilometragem = quilometragem;
    this.numeroRevisoes = revisoes;
    this.robustez = robustez;
    this.quadro = quadro;
    this.cor = cor;
}

Java will know which constructor to use and its values will not be unfilled, which avoids unwanted NPE.

    
05.12.2018 / 20:19
3

It is optional. It is only required when you put it as a parameter. You can have an empty constructor and assign values to the attributes by calling the variable + set method. Example:

Motociclo m = new Motociclo()
m.setMarca("Honda");
m.setModelo("CB500F");

etc ...

    
05.12.2018 / 20:03