You’re a builder, right?

Asked

Viewed 1,022 times

5

I’d like to understand why this class has two builders and why one of them has everything in it this and not separated as in the other. This changes something?

Normal builder:

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:

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

3 answers

7


A builder it’s not that different from a common method. Just as methods can be burdened, constructors can also. So 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 who does everything he needs. The second only delegates to the first how to do the construction. Then you do not need to pass the balance if it is a default value, draws without it and the secondary builder takes care of passing the balance to you.

This is not necessary in languages that have arguments with default value, which is not the case with Java, at least yet.

There is a special syntax to show that you are calling a constructor and not just any method. It is calling the method this. This way the compiler can treat it differently. So the secondary constructor doesn’t build the object, the primary will. But it is he who will deliver the result, if it is he who was invoked at the instantiation of the object.

This syntax is required because there can be nonconstructor methods with the same class name.

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

this(correntista, senha, 0);

in that case it is the same as

Conta(correntista, senha, 0);

which cannot be called inside a constructor correctly every time (there may be ambiguity).

I put in the Github for future reference.

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

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

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

  • So every time I call the constructor "simple", I’m passing on his information to the constructor "compound"?

  • In this example yes, but not necessarily.

  • How can I know that the simple builder is passing to the compound or not? (where it is evident that?)

  • I don’t know, because this terminology is wrong, I don’t know what it means. With a wrong premise, it gets complicated to answer anything.

  • "SIMPLE CONSTRUCTOR": public Conta(Correntista correntista, String senha) {
 this(correntista, senha, 0);
} "COMPOUND 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;
}

  • There is nothing simple or compound there, both are builders, are methods. One is short and the other is bigger. You want to know that you are going through what?

  • It’s hard... An example: if I delete the bigger constructor, will the smaller one work normally? If not, I want to know if the information captured for the short builder, are going to the long.

  • No, if you erase something, it stops working, if something depends on it, it stops working too. I suggest starting from the beginning, understanding each concept in a structured way, then the understanding of things will come easily. The question there is not even of constructor. Search on overloading method (overload of methods).

Show 3 more comments

2

Hello, using various constructor methods enables you to build objects in many ways. That way, once you have 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 argument a current object and password:

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

Thus, once you have a more "complete" constructor method that already does all the necessary assignments, just call this constructor more "complete" within 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 takes three arguments. As the responsible for assigning parameters to the attributes of the goal is the first constructor, the second constructor method only uses one this, and passes the whole responsibility to the first constructor method.

  • If I add one more attribute to the "composite constructor" signature, I have to add it to the "simple constructor""?

  • 1

    Yes, so that the simple constructor uses the correct compound constructor signature when calling it. Also note that if you miss an attribute in the class you can create a new simple constructor that assigns the new attribute and then call the compound constructor to assign the rest of the attributes. So you will always be saving code writing.

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 without initial balance or with initial balance. When creating an account with an initial balance:

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

By calling the new Conta the class will be invoked Bill and within it a constructor will be called. 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 the this and puts an initial balance 0. Thus saving code and time.

It doesn’t change anything, it was only done to save time and tailor the call, you can read a little more about polymorphism here

  • So if I want to, create the method with this this, he will still pass the information to the top builder?

Browser other questions tagged

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