Two Laptop Bag*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes MVC question - where's the model? 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 "MVC question - where Watch "MVC question - where New topic
Author

MVC question - where's the model?

Daniel Bryant
Ranch Hand

Joined: Jul 15, 2006
Posts: 54
Hi all,

In my URLyBird assignment I have attempted to use the MVC design pattern for the GUI and connection to the DB.

I have designed a JFrame based GUI (which is the View) which displays the hotel room data (using a JTable and associated subclass of AbstractTableModel) and handles button presses by calling methods on a GUIController class (which is obviously the Controller).

I've tried to design my GUIController much like the example in Andrew's SCJD book, and therefore when a user books a room the controller calls the appropriate methods on the implementation of the DBAceess interface (the Data class), i.e. lock, update, unlock, and returns appropriate results or an error message to the View. Therefore this made me believe that the implementation of the DBAccess interface is the Model (as it manipulates the database) - accordingly in standalone mode the Model would be the Data class itself, and in Network mode the implementation of the RMI Remote Interface which communicate with the server would be the Model.

However, when a client wants to search for records the GUIController calls methods on the Data class and returns an instance of a subclassed AbstractTableModel containing all the Room data matching the query, and then the view uses this model to display the search results.

1. My main question is do you think the subclass of the AbstractTableModel is part of the View (as it is displayed in the view), or part of the Model (it is created by the controller and passed to the view, but it does contain "state", i.e. the room record data, and it also has business style methods that, for example, can be called by the JTable to display the background of each column in a different color depending on if the room is bookable)

2. A second question is that when a user books a room, the original room data that was in the JTable must be passed to the GUIController to allow it to check that the room data in the database is the same. Do you think it is acceptable that my view has to extract the record from that JTable AbstractTableModel and pass this an an argument to a GUIController method? (I can't simply pass the record number into the GUIController, because apart from creating an instance of the AbstractTableModel when a search is complete the GUIContoller does not know about (or maintain a reference) to the AbstractTableModel

I'm trying to write up my choices.txt and don't want to say something silly (such as getting the Model in the MVC pattern wrong ). Any wise words would be most appreciated...

Daniel
[ August 21, 2006: Message edited by: Daniel Bryant ]

SCJP 1.4,<br />SCJD (URLyBird 1.2.1, JDK 5.0)
Rob van Oostveen
Greenhorn

Joined: Aug 14, 2006
Posts: 21
I want to post a quick reply on your question regarding the MVC design. I'm preparing for the SCJD and reading into design patterns. If i'm not mistaken, the Model is the encapsulated data (object) passed from the Controller to the View. There should be no interaction between the View and the database objects directly. All interaction between the View and the data should be handled via the Controller only. And the Model is used for the communcation 'packaging' between those two.

And the way you handle the Model is (in my humble opinion) not the way it should be used. I suggest you thoroughly reread the MVC design pattern and reevaluate your design.

RvO
SCJP 1.4
Petr Hejl
Ranch Hand

Joined: Feb 26, 2006
Posts: 68
I'm really not sure, but I don't think the model si passed from controller to view as some kind of data encapsulation. I think that model can exist aside of view and controller, but changed exclusively by controller and used by view to present data.
Peter Jakobsen
Greenhorn

Joined: Jul 26, 2006
Posts: 10
I think that a totally clean cut MVC is rarely seen in real life. The TableModel is often an example of a mix of model and view code, depending on how you design your TableModels. TableModel does not need to have any state, if you want to have an clean cut between model and view, you should have your TableModel query the Model (of your MVC) rather than contain it�s own internal list of objects to display.


SCJP, SCJD
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by Peter Jakobsen:
I think that a totally clean cut MVC is rarely seen in real life.


Just a minor nitpick:

"I think that a totally clean cut MVC is NEVER seen in real life."

At least, I have yet to see one.

- Adam
Rob van Oostveen
Greenhorn

Joined: Aug 14, 2006
Posts: 21
I haven't subscribed to the SCJD exam yet, but my feeling is that Sun is not interested in what is used in real life. If a candidate chooses a certain design pattern he should implement it accordingly. Otherwise choose another design pattern..

I'm reading into the design patterns and it is a good start for the SCJD.

Cheers.

RvO
[ August 24, 2006: Message edited by: Rob van Oostveen ]
Daniel Bryant
Ranch Hand

Joined: Jul 15, 2006
Posts: 54
Hi everyone,

Thanks to all for your input, especially Peter

TableModel does not need to have any state, if you want to have an clean cut between model and view, you should have your TableModel query the Model (of your MVC) rather than contain it�s own internal list of objects to display.


I think this is what I was after - I'm going to have a think about my design, but the TableModel querying the Model would overcome my concerns about the level of dependency between the View and the Controller.

Best wishes,

Daniel
 
 
subject: MVC question - where's the model?