I don’t understand what’s going on with this controller class, explain it to me?

Asked

Viewed 72 times

3

I really don’t get it, if someone could draw it for me,.

class DisciplinaController extends GenericController<Disciplina>{

    DisciplinaController() {
        super(Disciplina.class)
    }

}

2 answers

3

You can say very few things.

Is declaring the class DisciplinaController who inherits from GenericController<Disciplina> which is a class that actually implements the controller engine that will be used in this class. This is a class generic, that is, it allows working with various types of data verified and replaced in a static way. This means that in some places of it you have to say what kind of data is going to be manipulated and in the class, instead of putting a fixed type, you put a generic type in your writing, which is a kind of super variable, then when using the class it will be used with this type that is being passed in the <>.

Then there’s the statement of construction method. I know this because it has the same class name. It will be used to initialize the initial class data.

Within it is called the builder of the class he inherited (super() is a key word of the language that has this purpose), after all it needs to initialize the data of the other class. It is passing the class Disciplina as a parameter for it. In this case you are not initiating anything in the class itself, you may not need it at all, but you can’t tell just with this excerpt.

I don’t know if it’s the best way to learn, but it’s explained. If you have more specific questions, ask.

  • I think I got it...

2


In this class we are seeing the power of inheritance associated with the use of Generics :D. The Genericcontroller class, as the name suggests, is a generic controller. This means it probably has insertion, removal, update, and element listing methods, which are basic to many controllers.

When using Generics on it, the programmer is declaring that "it doesn’t matter" the type of element (or domain class) that will be passed. For any type of element, the mode of action will be the same. Classes that extend Genericcontroller specify the element that will be treated.

In the case of the Disciplinacontroller, the element is an object of the Disciplina class. The use of Generics is very cool, because it allows this simplification in the code, because generic methods are treated in the generic class. Only specific methods need to be treated in the classes inherited from the generic class.

For example, let’s assume that it was necessary to create a list methodDisciplines. This method would be implemented in the Disciplinacontroller class.

On the other hand, let’s say we need a basic controller for teachers. It would only be necessary to create a Professorcontroller as follows:

class ProfessorController extends GenericController<Professor>{
    ProfessorController() {
        super(Professor.class)
    }
}

Do you realize how much more productive you get? You detail that you want to work with the Teacher class, passing it on the constructor, and specifying the Generics in the inheritance (extends Genericcontroller). With this, you already "win" the implementation of the basic methods (insert, remove, update, lists) that are implemented in the Genericcontroller class. If it wasn’t for the use of Generics, you would have to implement the basic methods every time on all new controllers:

class ProfessorController extends GenericController<Professor>{
    ProfessorController() {
        super(Professor.class)
    }
    void inserir() {/*Implementação do método*/}
    void listar() {/*Implementação do método*/}
    void remover() {/*Implementação do método*/}
}

Greatly increasing the work.

  • @Alinegonz ask another question. It’s another matter. Even more now that you’ve accepted the answer

Browser other questions tagged

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