Doubt about MVC + DAO in JAVA

1

Good morning, I wanted to ask about how "Compulsory" it is to use a controller for each view and NOT a controller for all views.

My problem is that I was doing a practice where they asked us to implement the MVC + DAO design patterns in Java and I was doing it by creating only one controller for all the views.

I wanted to know if what I did goes against the MVC pattern.

In general my project ( Netbeans ) stayed like this:

  •   

    controller

         

    Controller.java

  •   

    model

         

    Main.java (The one that sends the controller to call)

         

    Conexion.java (connection to the bd)

         

    Cliente.java

         

    ClientDAO.java

         

    Venta.java

         

    VentasDAO.java

         

    IMetodos.java (This was an interface that contains crud methods that return a booleano )

  •   

    view

         

    InsertView

         

    ...

         

    ...

And another doubt, the class Conexion.java and Main.java if they go in that package?

    
asked by Member 27.11.2018 в 19:49
source

1 answer

4

If you are implementing a design pattern such as MVC ( Model-View-Controller ) or DAO ( Data Access Object ), it is best to follow its specifications because they are considered good practices that include good code structure, maintenance, among others.

  

How "Compulsory" is to use a controller for each view and NOT a controller for all views

It is not mandatory but it is desirable since it is better to aspire to have high cohesion and low coupling between classes, that is, that each class has a certain function and only that is dedicated.

  

The class Conexion.java and Main.java if they go in that package?

They can work properly where they are but it would be advisable to move them to a better location according to their objective. In the case of the Main.java class, it usually goes to the root of your project and Conexion.java in a package such as config .

For example:

com
 +- example
     +- myapplication
         +- Main.java
         |
         +- config
         |   +- Conexion.java
         +- dao
         |   +- ...
         +- controller
         |   +- ...
         +- model
         |   +- ...
         +- view
         |   +- ...

There are different ways in which you can organize your packages in Java, including a package by functionality / characteristic or by layer each with its respective consequences.

It might help to read the following Project Package Organization ( English ) to know the advantages and disadvantages of each orientation.

If you have questions about the organization of classes you can review the package definition ( package ) that Java provides in its documentation which can help you get an idea of how to organize your application: (< em> Translation )

  

A package is a namespace that organizes a set of classes   and related interfaces. Conceptually, you may think that   Packages are similar to different folders on your computer. Can   keep the HTML pages in one folder, the images in another and the   scripts or applications in another. Because the software written in the   Java programming language can be composed of hundreds or   thousands of individual classes, it makes sense to keep things   organized by placing related classes and interfaces in packages.

Note: You must remember that these are recommendations that are usually made to maintain order in your project especially when many people are working on it and may vary depending on whether it is a small, large project or just for demonstrative purposes.

Finally I leave you these links where you can find more detailed information of the patterns you mention.

References

answered by 27.11.2018 / 20:48
source