model-view-controllerumlarchitectural-patterns

MVC pattern in model with multiple classes


When applying the MVC pattern to a design with multiple classes do I need to create a model, view and controller for each of the relevant classes?

For example, for a design with a domain with UserAccount class, a MultimediaContent class, etc, would I need to design a UserAccountModel, UserAccountView, UserAccountController, and a MultimediaContentModel, MultimediaContentView, MultimediaContentController, etc?

I've looked for examples online but they all use a single class.


Solution

  • In the he original MVC as decribed by its inventor a:

    This is is a very high level description because each of these main "components" could be made of multiple classes. So it is fully up to you to decide of the best mapping.

    Other principles, such as the separation of concerns, would suggest to have different views for different model objects. So a UserAccount, and a UserAccountView is in general a sound approach. But you could still have combo views that refer to serveral different model objects at once.

    You will find many more flavors of MVC regarding the controller. The single controller monopolizing the user input and controlling all the views and commanding the domains, is no longer a reality, since many windowing system attach the controller to a window. So you'd probably have a swarm of controller, with an AppController and an additional controller for each view, e.g. UserAccountViewController, rather than a controller per domain object.

    Of course, in a very simple application, with a few relatively independent domain object, each having a single view, you could find the objects as you describe them.