wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Question about MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Question about MVC" Watch "Question about MVC" New topic
Author

Question about MVC

George Lawniczak
Greenhorn

Joined: Aug 12, 2002
Posts: 27
I am trying to write my controller class. The constructor of the controller gets passed in the associated frame object(which implements the FBNFrame interface), as well as the connection to the database.
In the controller I utilize the Command pattern to pass AbstractActions to the frame. My thought is that I define the FBNFrame interface with the following methods:
SetSearchAction(AbstractAction);
SetBookFlightAction(AbstractAction);
I then leave it to the implementation of the frame window to figure out where to assign these actions (Button, menu item etc.). In this way the controller does not need any knowledge of the action controls the frame window is using. This makes it easy to swap out Frames without breaking the controller as long as the new frame implements FBNFrame.
Where it gets tricky is in getting the data from the GUI to the controller. I could implement ComboBoxModel to pass to the frame to use to select Origins and Destinations. But this would require the knowledge that indeed the frame is using Combo Boxes, which is not good IMO.
The other alternative that I can think of is to add the following methods to the FBNFrame interface for the frame to implement:
String getOrigin();
String getDestination();
String getCarrier();
The controller would call these functions on the frame reference just before calling the search event. At least in this way, the controller doesn't care how the information gets there. But it seems that it would require a bit more maintenance to keep up should new search columns need to be added to the frame.
Just wondering if there are any other ways to approach this problem.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

Where it gets tricky is in getting the data from the GUI to the controller. I could implement ComboBoxModel to pass to the frame to use to select Origins and Destinations. But this would require the knowledge that indeed the frame is using Combo Boxes, which is not good IMO.
The other alternative that I can think of is to add the following methods to the FBNFrame interface for the frame to implement:
String getOrigin();
String getDestination();
String getCarrier();
The controller would call these functions on the frame reference just before calling the search event. At least in this way, the controller doesn't care how the information gets there. But it seems that it would require a bit more maintenance to keep up should new search columns need to be added to the frame.
Just wondering if there are any other ways to approach this problem.

Take a look at this MVC thread
My personal preference is to set the origin, destination, and carrier in the model by calling the set() methods from the GUI, using the model reference. Subsequently, the controller can get these values from the model. Although this couples the view and the model somewhat, I prefer this to other approaches where controller knows about the view. But it seems that there are as many different implementations and solutions as there are people in this forum.
Eugene.
George Lawniczak
Greenhorn

Joined: Aug 12, 2002
Posts: 27
Eugene,
From reading the thread you had indicated (thanks by the way), I came to the conclusion that the hook method is best. The fact that I use an interface will keep the frames in line to make sure they are implemented.
Another thought I had is to create an instance of a HashMap in the Controller, and pass its reference to the View for it to update the various values the Controller needs. This might be a more flexible solution that won't require a proliferation of "hook" methods in the View. When a new field is required, simply add a "put" statement to add the new key,value to the Map, and the controller will have access to it.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

Another thought I had is to create an instance of a HashMap in the Controller, and pass its reference to the View for it to update the various values the Controller needs. This might be a more flexible solution that won't require a proliferation of "hook" methods in the View. When a new field is required, simply add a "put" statement to add the new key,value to the Map, and the controller will have access to it.

Yeah, but then if the GUI control changes (say from combobox to textbox), the code in the controller will also have to change (from control.getSelectedItem().toString() to control.getText()). Or perhaps you were thinking of recording the actual values in the Map, but then the controller will have to know how to retreive them by key. But in both cases, this task seems more appropriate for the model, not the controller. I have yet to find a good solution to this, -- no matter how you try it, 2 of the 3 MVC components are coupled too tightly.
Eugene.
[ August 15, 2002: Message edited by: Eugene Kononov ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about MVC