views:

161

answers:

2

Hello,

I've implemented a Model-View-Controller pattern in my application. It's not a web application but MVC is suitable for it.

all updates to the Model are now routed through controller. Even the updates from the view is also send to controller and it will be routed to model. (I've state classes in between model and controller for decision making according to application mode). In most of the pattern, I am seeing like view directly updating the model. Is this valid in the context of this pattern?

+2  A: 

A view should never access the model directly, always via a controller, so you're doing it right in my opinion.

Gerrie Schenck
+3  A: 

The view can be an observer of the model.
The model can have access to the view via the observer interface. When the model changes it can notify the view or the controller. It is the Observer pattern that decouples the model from the view and controller.

Model-View-Controller

...

The passive model is employed when one controller manipulates the model exclusively. The controller modifies the model and then informs the view that the model has changed and should be refreshed (see Figure 2). The model in this scenario is completely independent of the view and the controller, which means that there is no means for the model to report changes in its state.
...

The active model is used when the model changes state without the controller's involvement. This can happen when other sources are changing the data and the changes must be reflected in the views. Consider a stock-ticker display. You receive stock data from an external source and want to update the views (for example, a ticker band and an alert window) when the stock data changes. Because only the model detects changes to its internal state when they occur, the model must notify the views to refresh the display.
...

TimW
Thanks for you rreply. In my case, the model is not fully active or passive. the model is updated when the controller updates the model (through state classes which is doing the decision making) and the viewers are notified by the model. Also, model is updated during startup by reading the default parameters from the configuration xml files. In that case also the viewer is notofied. Except this, all my updatse are controlled through controller.
Sarath