• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: MVC questions

 
Jonathan Liu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From book Java� 2 Developer Exam Cram� 2 (Exam CX-310-252A and CX-310-027)
it states this:

The following describes how the MVC pattern was used in this application:
* Model--SuperBowlTableModel, which extends AbstractTableModel.
* View--Swing GUI, including menus, buttons, combo boxes, and a JTable.
* Controller--Event methods and JTable-triggered code.
The JTable is set up to allow only row selection. Cell editing is forbidden to avoid confusion. Tool tips and a status bar keep the user informed.

To my understanding, JTable has already used MVC pattern and I wonder if I just create a class which extends AbstractTableModel, and combine View and Controller in a single class, is this still an acceptable "framework" ?
From thread:Confused with MVC ,Eugene Kononov gave an example how to implement MVC. Following his aproach, I found it more cumbersome than I was expecting.
For example, View has to expose every parameters Controller needs , which doesn't seem so elegant, like this:

and I need to think about how to deal with exceptions in Controller and show messages in View.
If follow the Java� 2 Developer Exam Cram� 2's instructions and example is enough to pass the exam, I would like to choose a simple way.
Your advices highly needed.
Thank you
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eugene Kononov gave an example how to implement MVC. Following his aproach, I found it more cumbersome than I was expecting.

Yeah Eugene really likes the cucumbersomes.
Actually the exact method that Eugene states is one of the best MVC models. Yes JTable uses MVC in its design, but that doesn't help you not write MVC code for your entire client.
In the code you posted, I would put all the code that is in the ActionPerformed method into a seperate method that gets called from the ActionPerformed.
For example

Now you just have a seperate method searchData() in your controller. This is called the Hook pattern. You created hooks in your Swing view and then any controller can hook into the view any way it wants.
Mark
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Indeed, simplicity and readability are as important flexibility and resusability. Sometimes these four things don't move in the same direction when you make a change in your design. In regards to MVC, you can do a few things to make it simpler. For example, you can combine View and Controller into one entity, to avoid passing of data from one to another. The price you pay is the tight coupling between the two, -- it becomes more difficult to add a view, or to change the existing code in the View and the Controller without affecting each other.
Just as it is in many other things, moderation is called for. You know, both democrats and the republicans seem like decent folks, but if you push the former all the way to the left, or push the latter all the way to the right, you end up with communists or fascists, respectively.
In MVC, probably almost anything will go for SCJD, as long as you understand clearly the motivations and limitations of your design and explain it to the grader. The particular flavor of MVC probably matter less than the consistency and clarity of your approach.
 
Jonathan Liu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both very much!
 
Jonathan Liu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark, thank you for pointing out the Hook pattern, but use this kind of method, does it make your code more difficult to read ? Or would you please make some suggestions about this? If a junor programmer like me don't know much about MVC before, he may take a long time to understand your code
Eugene, if I "combine View and Controller into one entity", could this still be called a "Framework" which is stated in the requirements
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic