Builder builder?

5

I would like to understand why this class has two constructors and why one of them has everything inside this and not separate as in the other. Does that change anything?

Normal constructor:

public Conta(Correntista correntista, String senha, double saldo) {
    Random gerador = new Random();
    this.numeroDaConta = gerador.nextInt(9000)+1000;
    this.correntista = correntista;
    this.senha = senha;
    this.saldo = saldo;
}

Builder builder:

public Conta(Correntista correntista, String senha) {
    this(correntista, senha, 0);
}
    
asked by anonymous 24.06.2017 / 17:36

3 answers

7

A builder is not so different from a common method . Just as methods can be overloaded builders can too. Then you can have methods with the same name and different signatures . This makes it another method. This is useful because each can have a convenience.

In your case the first would be a complete builder that does everything it needs. The second only delegates to the first how to do the construction. So you do not need to pass the balance if it is a default value, call without it and the secondary constructor will take care of the balance for you.

This is not necessary in languages that have standard arguments, which is not the case in Java.

There is a special syntax to show that you are calling a constructor, not some method. You call the this method. This way the compiler can treat differently. Then the secondary constructor does not construct the object, the primary constructor will. But it is he who will deliver the result, if it was invoked in the instantiation of the object.

This syntax is required because there may be non-constructor methods with the same class name.

Note that this in the other constructor is used to disambiguate the local variable or parameter of the object attribute . So it's not all within the same this , it's the same syntax for different mechanisms.

this(correntista, senha, 0);

In this case it is the same as

Conta(correntista, senha, 0);

It can not be called inside a constructor correctly at all times (there may be ambiguity).

Someone would think that they could have made the same code in both builders. But this violates the DRY and can create maintenance difficulties. This is not to save line of code, has important semantic purpose. There is no gain in time in this.

Understand that this is even a type of polymorphism, but we usually call it overload, because the understanding of polymorphism usually refers to something else. This terminology is somewhat ambiguous, overhead is not.

I will not comment on the other problems of this method that is not the focus of the question and for an exercise is ok.

    
24.06.2017 / 18:50
2

Hello, using several constructor methods allows you to build objects in many ways. That way, once you've declared this constructor:

public Conta(Correntista correntista, String senha, double saldo) {
  Random gerador = new Random();
  this.numeroDaConta = gerador.nextInt(9000)+1000;
  this.correntista = correntista;
  this.senha = senha;
  this.saldo = saldo;
}

And you feel the need to create objects of type Account without passing a balance, you can declare a constructor that only receives as an argument a correntista object and the password:

public Conta(Correntista correntista, String senha) {
    this(correntista, senha, 0);
}

In this way, once you have a more "complete" constructor method that already makes all the necessary assignments, just call this more "complete" constructor inside the new constructor, passing as argument the attribute that the new constructor does not receive, in this case the balance.

When you call in the second constructor method:

this(correntista, senha, 0);

Indirectly you are calling the first constructor method that gets three arguments. As the responsible for parameter assignments to goal attributes is the first constructor, the second constructor method only uses this, and passes the entire responsibility to the first constructor method.

    
24.06.2017 / 19:24
1

Hello, What was done in the above code is called polymorphism. This is an overload of methods.

Well let's say in the code above you want to create an account, and this account can be either created with no opening balance or with opening balance. When creating an account with opening balance:

Conta novaConta = new Conta(new Correntista(),"teste",30.30);

Calling new Conta will call the Account class and a constructor will be called inside it. At that moment, the method that best fits the parameters of the call will be executed. In this case the first code you posted.

In case of creating a new account without the balance, the programmer could have done the method like this:

public Conta(Correntista correntista, String senha) {
    Random gerador = new Random();
    this.numeroDaConta = gerador.nextInt(9000)+1000;
    this.correntista = correntista;
    this.senha = senha;
    this.saldo = 0;
}

But to save line of code and time what it does is, it calls the other constructor with argument that has balance through this and puts a starting balance 0. Thus saving code and time.

Nothing changes, just made to save time and adjust the call, you can read a bit more about polymorphism here

    
24.06.2017 / 18:16