MVC: Cardinality relationships of Views and Controllers [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this questionIn a generic sense of the MVC, is the relationship of the View and Controllers generally expected to be M:1? That is, many views will use the same controller? But a view will not use many different controllers?
Or, should I be able to swap any view with any controller and have everything work? I see a rather tight dependency between the two at this time and so this wouldn't work with my current layout...
I'm trying to design something for a class project, and I'm not sure how to organize / d开发者_如何学运维esign my views and controllers.
Update: The answers I've received thus far have been helpful but not definitive. Lets expand my question a bit. In retrospect, an important aspect is that the model can change (strategy pattern*) In one instance, the model may create a database. In another, it may read from the database. My original design goal was to get a uniform (albeit simple) view in place that would be able to deal with all of the models.
*I read (here) that the Controller can be looked at as a Strategy Pattern implementation. My model will be implemented in a similar but separate fashion.
Here's a quick (incomplete) class diagram of the concept (given the updated info):
Class Diagram of my MVC implementation concept http://theopensourceu.com/wp-content/uploads/2010/02/MVC-2334703.png
A view does and should not know anything about controllers. In this regard, I would say, the relationship is non-existent.
A view receives a model and displays it. Who prepared this model is in principle irrelevant.
Of course, in practice boundaries are sometimes melt. For some reason like simplifying code dependencies, a view sometimes gets to know something about what controller burned the fire. But if you're talking about pure conceptual discussion, then no, there is no direct connection between a controller and a view. Rather, they communicate via intermediary, that is a model.
ControllerX
--------------------------------
Model1 Model2 Model3
| | |
View1 View2 View3
The same view can in principle be outputted by several controllers. If a controller is able to instantiate a model, then it is pretty much sifficient.
My own take on the MVC structure is this:
The controller - the most important part. If you only had to use one piece of MVC, it would be this. I have my controllers call data from models and then pass the data (and any other bits I need) into a view (or nested views) which is then sent to the user.
That said, one controller could use many models and views - but not the other way around.
精彩评论