0

I'm currently trying to develop application using JavaFX and I'm stuck just at the beginning. I want to use MVC pattern in this implementation and also make this implementation as reusable as possible. Basic description of the app:

1) It uses configuration file in which all database connections parameters (passes, logins) and other parameters used by the app are stored.

2) It's all about configuring of workflow of other application so it has to store two database object to two different databases and make operations through them in databases.

3) The design assumes that there are few views, every one has it's own controller and different functionalities can be done through them but all can trigger operations on database or configuration file.

4) The goals is also to remember the state of previous view while changing between views during usage.

That's why I have this questions:

1) While I start the application I test the connections to the database and if successful I connect to them. Is it ok that I do this in the Main class or should I have some other class in which I store all these db objects in case of referring to them in this class? Is it ok that every controller will use an instance of the Main class just because it has to access these db objects (assuming I decided to not use other class in which I do all the connections)?

2) What is the role of the main class? Should it be used as a main view (as in MVC) and should it only launch the application and pass the further responsibility to, for example, LoginController/View...?

I would be greatefull for some clear answer which could enlighten me this problem a bit.

There are no stupid questions, they say... :D

zmaselke
  • 91
  • 11
  • Maybe see my answer to http://stackoverflow.com/questions/32464698/java-how-do-i-start-a-standalone-application-from-the-current-one-when-both-are, which is related to 2). This is really quite opinion-based, but my answers would be 1) you should use a DAO design pattern and factor the DB code into another class, and 2) the `Application` subclass should do minimal work and should basically just start the application. – James_D Jan 29 '16 at 16:37

1 Answers1

0

Your Main class, as pointed out by James_D, should literally be just that

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("ui_main.fxml"));

    stage.setScene(new Scene(root));
    stage.show();
}

Main class typically doesn't play any role in MVC. However, if you are not using any dependency injection frameworks, it is common for Main class to perform the wiring between Model, View and Controller.

Model is where all the (business / domain) logic must be located. If a controller needs to access a database, it will use an instance of Model. Model will typically comprise a set of other objects which provide the actual functionality, e.g. database access, network access. This allows maximum flexibility as functionality providing objects become separate modules and are reusable.

AlmasB
  • 3,377
  • 2
  • 15
  • 17