This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 JTableAbstractTableModel 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 ]
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.
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.
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.
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.
Rob van Oostveen
Joined: Aug 14, 2006
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.
RvO [ August 24, 2006: Message edited by: Rob van Oostveen ]
Joined: Jul 15, 2006
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.