sarat
sarat

Reputation: 11120

Model View Controller

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?

Upvotes: 1

Views: 996

Answers (2)

TimW
TimW

Reputation: 8447

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.
...

Upvotes: 3

Gerrie Schenck
Gerrie Schenck

Reputation: 22368

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

Upvotes: 2

Related Questions