*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes confused with MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "confused with MVC " Watch "confused with MVC " New topic
Author

confused with MVC

lydie prevost
Ranch Hand

Joined: Mar 14, 2002
Posts: 32
Hi everybody,
Can somebody give me a good reference to help me understand MVC.
It seems that there are different qya that people implement MVC and I am very confused.
I read the ver good post on MVC by Mark Spritzler where the Contrller controls and the view does not know about the controller. The view has to reference to the controller but hook methods. On the other hand I saw a lot of description of MVC where the view has one reference to the controller and one to the model.
Same thing with the main method. Some instantiate a view, while others instantiate a controller. Is it all the same???
Each time I read something it seems different.
I just want to know who holds a reference to who and is the main creating a view or a controller?

If anybody has some good reference to it I wouls really appreciate I am stuck trying to get it
Thank you
- Lydie
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 356
hello,

the way things are being instantiated starting from the main method differ a lot, and MVC itself makes no direct statement about the right way there directly.

About the question who has a reference to whom general oop principles like loose coupling often answer a part of the question about how to do it. The view for example is often connected to the model using an observer pattern (the view observing the model). In swing this observer-observable connection is often pre-implemented, so that you don't have to do it by yourself. To connect a JTable table to a TableModel model for example you just have to call: table.setModel(model) once. So the model would actually have a reference to the view in this case, but that's usually hidden in the implementation of the superclass AbstractTableModel! So there might be no explicit reference to the associated JTable in your table model, because this reference is a private reference in the superclass AbstractTableModel (for example inside a private List).
For the connection of the view to the controller you can often rely on the awt listener system. But who instantiates the controller class, and who adds it as a listener to your Component? You have some freedom of choice there, but in most cases it is good NOT to keep an implicit reference to the controller in your view. Some of the behaviour that is sometimes associated with the controller is pre-implemented in swing components, for example copy & paste in a JTextComponent.
The controller needs a reference to the model in the typical case. There you have a lot of freedom of choice again. One common approach is to have a singleton as the "main model" which again knows more about other parts of the model in the application.

An acceptable example: The main method is in some "main model" class and instantiates an instance of the class there. The constructor of this model class then instantiates a view and calls some add...Listener methods on it. The controller for the argument of those methods is being instantiated before.
Or the view instantiates its controller class once to add the listeners, but then forgets about it (though the java foundation classes you use may keep such a reference, invisibly to you).

Remember loose coupling and strict seperation of tasks. If you know about servlets and JSP think about how your application would look like if it would be a webapp (makes even sense with the SCJD assignments). If your design is good then the model would not have to change at all (almost), your controller would become one or more servlets and your view would be a JSP or html-page.

Conan


Kai Witte's business website Kai Witte's private homepage
lydie prevost
Ranch Hand

Joined: Mar 14, 2002
Posts: 32
Thank you very much. I have now to read carefully and digest it. There are a lot of informations.
I may come back and ask more questions but for now, I need to study this.
Thanks
- Lydie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused with MVC