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?

asked Jul 1, 2013 at 23:56
\$\endgroup\$

1 Answer 1

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.

answered Jul 2, 2013 at 7:09
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.