MVC pattern: which is better? For views or controllers to create and reference the other?
We are making a rather large Swing application which has to implement the MVC pattern. The application currently looks like this:
There are quite a few views. They are created in a hierarchical manner where one main view contains (and creates) several views which all contain their own set of sub-views, etc. Each of these views retrieve information from the model independently from the other views, by calling the models static methods when necessary.
There are also quite a few controllers which are all totally separated from each other. Each controller belongs to a view. Each view creates its own controller, and adds the controller as a listener to user input. The controllers receive events from the views and then modify the model through the models static methods. When the views dispatch events which do not affect the model, but only affect the views, the views take care of these events themselves - without informing the controllers about the events. That is, the controllers are totally unaware of the views, and the controllers purpose is only taking care of the manipulation of the model. |EDIT: the controllers are currently attachments to their views; they only contain logic for event-handling. That is, the controllers are not components themselves, and do not contain components. They are implemented in the same manner as the following example: MVC example |
The model in the application is very passive, and does not even have listeners (it represents a database). It receives updates from the controllers.
In this example, the views own the controllers. Would it be better in the general case if the controllers owned and created the views, and if one let the views be unaware of the controllers, instead of the opposite? In that case, why? How would this be designed? If not, is there a better design in which the controllers are still unaware of the views? Or perhaps, is the best design neither of them?
EDIT:
As stated in the Original MVC definition:
the line "The View takes responsibility for establishing this intercommunication..." seems to indicate that the view creates the controller, or at least has the initial reference to the controller, and not vice versa.
So this is at least a possible way to do it (it is a valid MVC pattern), but the main question remains; which is better, and how would the best design look like? Especially when dealing with many controllers that are closely related to their respecive views?
EDIT: A开发者_如何转开发nother example of a view which references a controller: Oracles example
As seen in this outline, the controller has the model and the view(s). Any sub-views are managed by the respective parent view. Sub-views may forward events to the parent as discussed here. There's a simple example here with more links.
精彩评论