posted 20 years ago
One of the situations where MVC is appropriate is when there are multiple views possible for the same model.
Yes, that's the intent, and you are right, the example that you are referring to misses completely on how MVC can resolve the communications between the model and the multiple views.
Let 's assume we modified the Controller code so that it keeps track of all Views it controls. Now, the Controller gets a bookRecord call from somewhere. In the example you provided, this is triggered by a push on a button. Which button? From which View?
OK, here is how it typically works. The views that are interested in a particular model subsribe themselves as the listeneres to that model, normally by implementing a listener interface. The model mainains a list of subscribers (views), and when the state of the model changes, the model broadcasts the changes to all listeners. For example:
In the "dumb" version of the model, it broadcasts all the changes to all the views, indiscriminantly, and it is up to the views to interpret the changes when the notification is received. In the "smart" version of the model, it has some itelligence to know which views are interested in which particular changes, so the model may send notifications selectively. The advantage of the former model is that when you add a view, the model doesn't need to change. The advantage of the latter model is that you reduce the (presumably) expensive notifications. In either version of the model, it doesn't know which particular button (or any other visual control) triggered the event, and this is precisely the intent of MVC, -- no matter how many GUI elements you add or change in your views, the model should not change, -- it simply doesn't know anything about the contents or functionality of the views.
How does the user action results in the change in the model? Well, that's the job of a controller. The controller listens for user actions (such as clicking the button), and maps these actions into calls to the model. Again, the idea is that when you add or change the view, the model doesn't need to change.
I think it is simpler that the original action that triggered all this is first handled in the View itself (getting the input data) and passing these data to the Controller.
Yes, that's one approach. But here is the disadvantage, -- your view is now acting as a controller, taking the responsibility to determine which pieces of data are needed to act on a user action. That is, your view is now smarter than it could be, and this may contaminate your MVC. The alternative is to let the view be as dumb as possible, -- all it should know is how to render the data in its widgets. When the user motions the intent, the controller takes over, requests the additional data items from the view if neccessary (by calling the public methods of the View), and invokes the appropriate method of the Model.
Now concerning error messages (or information messages for that matter), the same problem occurs: one View may want to display an error pop-up, another View may want to display a message in the status bar.
This could be handled by calling a method showErrorMessage (or showInfoMessage) on the originating View from the Controller.
Again, we have a problem. Who should have the knowledge of what contstitutes an error? If it is a violation of some business rule, it would be logical to think that it should be contained in the Model. When the Model detects an error, it may broadcast the notification to the views, which will display it to the user. However, if we are talking about exceptions that are thrown from the model, the controller is probably a better candidate to catch these errors and to select the views where the error should be displayed.
And Philippe, join the club, I'm struggling too with this pattern.
Over the years, it has come up several times, and no matter what I did, I was never really happy about the way it was done (either by myself or by someone else).
I struggled with MVC myself, until I realized that the intent of MVC is exactly the same as any other pattern, -- to break the comlexity into several loosely coupled pieces, each piece having a nnarrow and well defined set of responsibilities. Granted, MVC is a bit more complex than other patterns (one reason is that MVC itself is a combination of several patterns), but again, the idea is the same.
Perhaps the most important thing is not to try to be "compliant" with any particular version of MVC, but instead to be consistent with the motivation of MVC. If your code is easy to read, extend, and maintain, no one will give a damn if it is a real MVC or not, -- everyone will be happy, including you, your fellow programmers who work with your code, and definitely your grader, too.
[ November 15, 2003: Message edited by: Eugene Kononov ]