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

Asked

Viewed 425 times

6

I have these builders:

//1st Constructor - Create 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, it is mandatory to put it in the other builders previously created or is optional?

Is that in the solution of this exercise every time my teacher creates a new attribute he places it on all builders.

3 answers

8


It’s the usual answer: it depends.

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

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

Are these data mandatory on all objects of this type? If they are, you have nothing to think about. If they are not then they should probably be added after the instance has been created. But not always, it might be worth having builders of convenience.

If you mess around too much in classes you may be fixing headaches for maintenance on every application.

Software engineering is not following formulas, it is understanding the problem and doing what is most suitable for that case. Without understanding the problem, without analyzing the requirements it is not possible to state whether or not to put a new parameter. Without knowing how this class is likely to be consumed, it is not possible to say whether what should be created just for convenience.

The profile of this code suggests that they are not all so necessary, but I can’t guarantee it. It is strange that each constructor has a different set of parameters almost completely isolated. I do not know, it seems some conceptual error, after all each object can be very different from the other, starts to make no sense, so maybe the problem is not only the constructor, but every class have problems. With a better description of the problem I could talk with more property.

Of course, the teacher may only want to pass the constructor mechanism, without paying attention to the correct concept. But I don’t like it, I see that people start learning the wrong concept and never fix it. I’m just speculating on the real situation.

It’s not mandatory, but I don’t know if it makes sense in this case. You have to have a reason to create such different builders.

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

6

Has a very common practice that is used in these situations: use the keyword this(), that represents the constructor of the class itself, and fill in the most generic constructor with default values. See the example to make it clearer:

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 filled in, which avoids unwanted NPE.

  • 1

    Just as nomenclature, this technique has a name: chaining constructor (chaining of builders).

  • 1

    In this case polymorphism has no part in the subject. It is more because of the "overloading".

4

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

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

etc....

  • So in my case it is mandatory because I have mileage attributes declared as variable of my right class??

  • Not exactly. You can create a motorcycle with one of the builders you already have and then assign value to the other attributes. Ex: Motociclo m = new Motociclo("Marca X", "Modelo Y", quadro); m.setQuilometragem(12000)

  • 1

    @Luisamaro has no relationship between constructor and class attributes. You can either have a constructor without any attribute (which, by the way, is called a standard constructor) or one with all attributes. What you are going to order in this case is your need. There is no right or wrong here.

Browser other questions tagged

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