What goes into the "Controller" in "MVC"?

Posted by P72endragon on Stack Overflow See other posts from Stack Overflow or by P72endragon
Published on 2009-06-19T00:01:26Z Indexed on 2010/04/17 14:23 UTC
Read the original article Hit count: 138

Filed under:
|
|

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!

© Stack Overflow or respective owner

Related posts about mvc

Related posts about java