File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Is it right way to implement the 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 "Is it right way to implement the MVC?" Watch "Is it right way to implement the MVC?" New topic
Author

Is it right way to implement the MVC?

Zee Ho
Ranch Hand

Joined: Jul 20, 2004
Posts: 128
The "Design Patterns" book (GOF) : "MVC decouples views and models by establishing a subscribe/notify protocol between them. A view must ensure that its appearance reflects the state of the model. Whenever the model's data changes, the model notifies views that depend on it. In response, each view gets an opportunity to update itself. This approach lets you attach multiple views to a model to provide different presentations. You can also create new views for a model without rewriting it


is it right that :

1. In veiw, it should focus on how to show the data, report the status of the component to controller and nothing else. maybe some hook methods to add listener to component.

2. In the controller, It should assign the listener to appropriate component, call the proper method to get info from view and transfer that to the right method in the model.

3. Model should process the logic of handling the data, (eg, the how to match the search criteria) and also it is the one to hold the connection with the database in the MVC pattern.

is it right?


SCJP 1.4<br />SCWCD 1.3<br />SCJD<br />SCBCD<br />IBM Xml Cert in progress
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
You're on the right track. Keep in mind that even though MVC is a well-known framework there are many ways to implement it (some better than others). The view handles the creation of controls and sometimes has a reference directly to the model (see Strategy pattern) or gets a reference to the model as needed when something changes in the model (see Observer pattern) the controller handles the events from the view. The model handles only data.

If you want to help ensure that you are separating the Model, View and Controller is to create interfaces in-addition to the concrete classes. So you would have the following:

Interface - ModelIF: View and Controller would only reference this interface. Contains all methods needed by View and Controller.

Class - Model: (implements ModelIF) no classes should reference this directly so that you could easily change the model just by creating a new class that implements the ModelIF interface.

Interface - ControllerIF: View would only reference this interface. Contains all methods needed by View.

Class - Controller: (implements ControllerIF) no classes should reference this directly so that you could easily change the controller just by creating a new class that implements the ControllerIF interface.

Interface - ViewIF: Controller would only reference this interface. Contains all methods needed by Controller.

Class - View: (implements ViewIF) no classes should reference this directly so that you could easily change the view just by creating a new class that implements the ViewIF interface.

By using the interfaces, your system becomes very flexible and expandable. Though there is always a cost - that being maintainablity. Keep in mind that if use this approach - any changes you make to the concrete classes that involve method signature changes (additions, modifications or deletions) require the same changes in the corresponding interface classes.

Also, get a copy of the Head First Design Patterns book. The GOF is a must have, but is not very easy reading. HF Design Patterns actually has practical examples in Java - a must have.

Anyone else have another approach on this?
[ April 08, 2005: Message edited by: Terry McKee ]
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Same approach, but I just really wanted to second your recommendation for HF Design Patterns! It is much easier to read than GOF, and the examples are in java instead of c++.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Zee Ho
Ranch Hand

Joined: Jul 20, 2004
Posts: 128
then is it right to call model.fireTableDataChanged in the UI after data changes applied? or should be in Model itself?


In the Model
public void search() {
//perform the search logic
view.changesApplied(DATACHANGED);
}

In the UI
public void changesApplied(final int notificationType) {
switch(notificationType) {
case DATACHANGED : model.fireTableDataChanged();
case CLEAR: comboBox.setSelectedItem("And");
}
}

In my thought if we really separate the view and model, we should pass the search result to the view (as view.changesApplied(DATACHANGED, data)) and let it update itself base on the those data, but this is more cumbersome, since I extends the AbstractTableModel, then I decide to use such way, more convenient but seems dirty. is it still a good practice of MVC?
Zee Ho
Ranch Hand

Joined: Jul 20, 2004
Posts: 128
and where we place the code to handle the action that don't effect data? for example, I wan to make a context menu on my JTable. if I place those code into the controller, then I should notify the view through the controller which is not the MVC way. implement it in the model seems also not appropriate, since it doesn't change the data, really confuse about it.



In one Controller I hold a reference to anther controller, is it still valid? since I cannot find anywhere else to place this code fragment
[ April 09, 2005: Message edited by: Zee Ho ]
Zee Ho
Ranch Hand

Joined: Jul 20, 2004
Posts: 128
call up, call up!!
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
Implementing the MVC pattern effectively is more cumbersome than making direct calls from the view to the model, but I believe it is worth the effort. It's my opinion that the fireXXX method really should be called by the model class. The view should only be acting on changes in the model and the data itself.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
There is also a free, downloadable Java-based version of Design Patterns. The FAQ has a link to it. This freebie book is decent though rife with syntax and spelling errors. However, for those who may not have the spare change to buy yet another book, the free download is a great place to start.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it right way to implement the MVC?