tags:

views:

95

answers:

6

I have a model with a reference to the view, a controller with references to the model and view, and a decoupled view, with no references to the above.

The controller has all the listeners of the view, and takes care of events by calling methods on the model and view. The model calls methods on the view in some of it's methods.

Is this bad for maintenance and reuse and should all view methods be called only in the controller? Also, i haven't set up the model as the observable and the controller and view(s) as observators, as seen in some examples. Is this common practice to do so?

+1  A: 

Following the classic MVC pattern, a model would not have direct access to a view. However, indirect access - such as via an observer - would be helpful to alert the view to changes in the underlying data.

Justin Ethier
+2  A: 

I would avoid coupling directly from the model to the view, or at least only as very special circumstances (and even then, this might hint that the state being manipulated is not true model state, but presentation state.)

In general, the model uses listeners to notify of changes rather than a direct coupling to the view. The view can have a direct reference to the model, so that it can get the data to display. Although this may not be strictly necessary - all changes can be propagated from the model to the view as listener notifications, at the time the view needs it, but this is usually a bad idea, since it couples the rendering of the view with notifications from the model.

To sum up, the controller makes changes to the model, which notifies interested parties (including the view). The view has a reference to the model to retrieve data.

mdma
+1  A: 

The model should not know anything about the view. The view should know how to populate itself given a model.

If you model is going to change it might want to make the view know of any changes so the display can change.

Romain Hippeau
A: 

There are so many flavors of MVC, that i will decline to call any as "wrong".

See http://martinfowler.com/eaaDev/uiArchs.html

Look for one fitting flavor and go with it.

Markus Kull
A: 

There's a nice blog article at Sun.com: Java SE Application Design With MVC. It starts with explaining the "classic" MVC pattern where the view and model interacts directly with each other and then goes on with the newer "centralized controller" MVC pattern where the view and model can only communicate with each other through the controller (model is decoupled from view). It contains a Swing targeted example as well.

BalusC
A: 

On the desktop Swing is a good GUI library. Check out this article to see how it relates to MVC. For the enterprise, take a look at Martin Fowler's Model View Controller pattern (330) for web-based applications. If you don't have his book (Patterns of Enterprise Application Architecture), I highly recommend getting hold of a copy.

johnnieb