• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is it right way to implement the MVC?

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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++.
 
Zee Ho
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
call up, call up!!
 
Terry McKee
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Yeah, but is it art? What do you think tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic