0

I have a program that allows users to open multiple databases, select tables from them, and then select the columns in the tables. So my GUI looks something like this:

enter image description here

If you click the Select Tables button, you get this pop up:

enter image description here

My MainView contains the menu bar, the toolbar, the JPanel that contains a CardLayout for switching between databases. I have a MainController for the main frame that controls the MainView and the MainModel. The MainModel has a method that creates a connection to an existing database:

public void connectDatabase() {
    DatabaseModel dbModel = model.connectDB();
    DatabaseView dbView;
    DatabaseController dbController;

    if (dbModel != null) {
        dbView = new DatabaseView(dbModel.getName());
        dbController = new DatabaseController(dbModel, dbView);

        view.addDBchoice(dbModel.getName(), dbView);
        dbControllers.add(dbController);
    }
}

A DatabaseModel is just a representation of the database. It contains the Connection, a static list of all its tables' names (Strings), and a dynamic list of Table objects (details below).

The DatabaseView contains a Select Tables button, and a JTabbedPane (details below).

Now, once a Database connection is created, the user will be able to select which tables from that database would they like to open. The DatabaseController is the one that can open the JFrame (shown above) that allows the user to select tables they want. After selecting the tables, this code inside my DatabaseController creates Table models, views, and controllers, for each of the selected tables:

for(int i = 0; i < indices.size(); i ++) {
    tableModel = model.getTableModel(selectedValues.get(indices.get(i)).toString());
    tableView = new TableView();
    tableController = new TableController(tableModel, tableView);
    tableView.addController(tableController);
    model.selectTable(tableModel);
    view.addTableTab(tableView);
}

Each TableView (JPanel that contains a dual list box) is added to the JTabbedPane (as shown above), and each TableModel is added to the dynamic list of Table objects I mentioned above. A TableModel is just a representation of a table. It contains the names of its columns.

Now, my main question is: is this the correct way to implement MVC in Java?

For example, in my program, the DatabaseController controls the TableControllers, and later on, I will have ColumnController, ColumnModel, and ColumnView classes. A TableController then can control the ColumnControllers. Is this a good idea?

Also, should the parameter for View objects be its Controller? If that's the case then should the Controller only have the Model as a parameter?

Any other suggestions/criticisms/opinions are welcome.

Wabbage
  • 437
  • 3
  • 6
  • 18
  • 4
    Yes and no. Your views should be focused on a single task each having their own controller and model. When I say "view", I don't mean window, I mean each individual component group, which has a specific responsibility, like showing a list of the tables and the view responsible for showing the meta data of a table and the view responsible for showing the contents, each are responsible for a specific job and represent a individual mvc. You then begin grouping theses specific mcv's into larger, modular mcv's, building up the ui and its functionality – MadProgrammer Dec 29 '15 at 21:43
  • 3
    You should also be focusing on using interfaces over implementation, this means you focus on the details of the contracts between the components of the mvc over its physical implementation, making easier to change the implementations and the more flexible – MadProgrammer Dec 29 '15 at 21:46
  • 2
    Deciding what to pass to each layer can cause a lot of stress, in a "traditional" mvc, the model doesn't know about the view and the view doesn't know about the model, they are bridged by the controller. Obviously this isn't the way Swing works, where the model is shared between the view and controller, thus can sometimes make it difficult to implement a pure mvc in Swing. Each approach has pros and cons, I tend to mix them a little, containing the swing based components into a self contained view, then using the views interface, define what the view inputs/outputs as well as events – MadProgrammer Dec 29 '15 at 21:53
  • 2
    I've done a few decisions on the subject which might help, have a look at [this](http://stackoverflow.com/questions/31576623/how-mvc-work-with-java-swing-gui/31576899#31576899), [this](http://stackoverflow.com/questions/26789848/implementing-the-controller-part-of-mvc-in-java-swing/26790064#26790064), [this](http://stackoverflow.com/questions/31281196/actionperformed-updatemodel-object-in-mvc-and-gridbaglayout/31281349#31281349), [this](http://stackoverflow.com/questions/31602113/listener-placement-adhering-to-the-traditional-non-mediator-mvc-pattern/31604919#31604919) for starters – MadProgrammer Dec 29 '15 at 21:57
  • 1
    And a extended [example](http://stackoverflow.com/questions/26517856/java-and-gui-where-do-actionlisteners-belong-according-to-mvc-pattern/26518274#26518274) which demonstrates much of what I've discusses above – MadProgrammer Dec 29 '15 at 22:34
  • Thank you for the response. Would it be idea to have a single object that contains the controller, view, and model so it's easier to access? Like in my program, if I'm trying to get the name of a table from a controller, I call the table object's controller from the database controller, and then the table controller calls the table model. Should controllers be talking to each other? Or should models do that? – Wabbage Dec 30 '15 at 14:45
  • 1
    In a traditional MVC implementation, your controller would have a reference to the model and the view. You would then, where needed, maintain references to one or more controllers – MadProgrammer Dec 30 '15 at 20:19
  • Yes, ` new Controller(View v, Model m)` is how I instantiate my controllers. But if I want to access a Table model, do I use the Database controller to access the Table model? Or the Database controller calls the Table Controller? Then the Table controller calls the Table model? – Wabbage Dec 30 '15 at 20:34
  • 1
    You shouldn't be accessing the table model directly from any class other the the containing controller (or view depending on how you set it up), you should have setters which allows one controller to talk to another directly or indirectly (via some kind of listener API, which is probably preferable) – MadProgrammer Dec 30 '15 at 20:56
  • Let's say I have a list of Table objects that exist in the database. Is the list supposed to make up of Table models or Table controllers? And would it be the Database controller or model containing the list? – Wabbage Dec 31 '15 at 17:30
  • 1
    The list of tables is a model, the mechanism by which they are created would come from a factory/manager whose only responsibility is to manage the datastore – MadProgrammer Dec 31 '15 at 19:59
  • Could you give me a quick description of which factory are you talking about? Or is it just a general design factory pattern? – Wabbage Jan 05 '16 at 14:28
  • Just a general concept of a factory – MadProgrammer Jan 05 '16 at 20:32

0 Answers0