Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: Another example of a view which references a controller: Oracles example

like image 362
Datoraki Avatar asked May 08 '11 12:05

Datoraki


People also ask

What is the difference between model view and controller?

The model is responsible for managing the data of the application. It receives user input from the controller. The view renders presentation of the model in a particular format. The controller responds to the user input and performs interactions on the data model objects.

How model view and controller interact with each other?

First, the browser sends a request to the Controller. Then, the Controller interacts with the Model to send and receive data. The Controller then interacts with the View to render the data. The View is only concerned about how to present the information and not the final presentation.

Should controllers talk to each other?

Controllers can always talk directly to their Model. Controllers can also talk directly to their View. The Model and View should never speak to each other.

Does the model or the controller update the view?

The client only interacts with the view. The controller interacts with the view and indirectly with the client to update the model. The view does not directly cause modifications in the model, but all modification requests go through the controller.


1 Answers

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.

like image 141
trashgod Avatar answered Oct 14 '22 08:10

trashgod