What goes into the “Controller” in “MVC”?

I think I understand the basic concepts of MVC – the Model contains the data and behaviour of the application, the View is responsible for displaying it to the user and the Controller deals with user input. What I’m uncertain about is exactly what goes in the Controller.

Lets say for example I have a fairly simple application (I’m specifically thinking Java, but I suppose the same principles apply elsewhere). I organise my code into 3 packages called app.model, app.view and app.controller.

Within the app.model package, I have a few classes that reflect the actual behaviour of the application. These extends Observable and use setChanged() and notifyObservers() to trigger the views to update when appropriate.

The app.view package has a class (or several classes for different types of display) that uses javax.swing components to handle the display. Some of these components need to feed back into the Model. If I understand correctly, the View shouldn’t have anything to do with the feedback – that should be dealt with by the Controller.

So what do I actually put in the Controller? Do I put the public void actionPerformed(ActionEvent e) in the View with just a call to a method in the Controller? If so, should any validation etc be done in the Controller? If so, how do I feedback error messages back to the View – should that go through the Model again, or should the Controller just send it straight back to View?

If the validation is done in the View, what do I put in the Controller?

Sorry for the long question, I just wanted to document my understanding of the process and hopefully someone can clarify this issue for me!

13 Answers
13

Leave a Comment