2
\$\begingroup\$

I have a basic swing application that I am trying to develop using MVC structure.

The principal class in the model connects to a derby database and retrieves values from columns and stores them in arrays.

My methods in the model (Connect(), Retrieve()) are public void. In runApp(), which is a SwingUtilities runnable, I am calling my model methods like this:

public static void runApp() {
    Model model = new Model();
    model.Connect(); //Connects to database
    model.Retrieve(); //Retrieves database column values from tables, creates arrays
    View view = new View(model);
    Controller controller = new Controller(view, model);
    view.setCalculateListener(controller);
}

I am trying to write this program so that everything is as less tightly coupled as possible. This code currently works as is, but I would like to do this better.

My question: How can I do this in a less coupled manner? Rather than having the methods being called here, is there a better way to do this?

\$\endgroup\$
2
\$\begingroup\$

For starters, consider making Connect() and Retrieve() non-public. runApp() doesn't seem to have a good reason to know about the operations that Model needs to perform to be usable here. Can the constructor (or, possibly more appropriately, a static factory method) hide those methods?

Introducing state to objects is an easy way to make things complicated. If a View or Controller were somehow able to be passed a Model that had been, say, Connect()ed but not Retrieve()d, what would happen? It's simpler to factor that possibility out when that is an option.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.