Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model, View & Controller - what should create what?

According to good programming practices, at the beginning of the program runtime, which of the Controller, Model and View components should be created first and which of them should create the other two?

I mean, should the main function first create the controller, then the controller should create both the model and the view and make itself known to them somehow?

Or should I rather begin with creating the view, which, before displaying itself, would initialise the controller, which would create the model?

Or maybe the model should come first? Or they all should be created in the main function in parallel? What's the right way of implementing MVC?

edit: I'm interested in a general answer, though currently I'm working with Java Swing and Windows Phone 7.

like image 416
lampak Avatar asked Dec 04 '11 15:12

lampak


People also ask

What is meant by model view?

An MVD is a diagram that is used for defining how and what components will be used for an information exchange.

How does model and view work?

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.

What is model view in software engineering?

A view model or viewpoints framework in systems engineering, software engineering, and enterprise engineering is a framework which defines a coherent set of views to be used in the construction of a system architecture, software architecture, or enterprise architecture.

What is SAP model view?

Model-View-Controller (MVC) concept is used in SAP UI5 development to keep the application data separate from the user interactions. This allows you to develop the web applications and make changes to the applications independently. Model-View-Controller plays a different role in UI development −


2 Answers

I'd say that Controller and Model objects could be created by main function while View should be created by Controller (possibly based on Model data).

Controller should be created by application main function (working trhead? whatever) whenever new request comes to application. A request may contain some serialized data which could be deserialized by main thread to create and fill new Model object which is in turn passed to Controller for further processing. When controller finishes processing it may (or may not) create a View to pass processing results to client.

Also a Model class could be created by Controller to serve data processing needs (save data to database etc.) or to serve as a base for return View.

Summarizing:

  1. Controller is always created by main function
  2. Model can be created by main function or Controller (maybe also by View? Depends on MVC implementation)
  3. View should be created by Controller (possibly based on data from Model).
like image 147
Sergey Kudriavtsev Avatar answered Oct 13 '22 02:10

Sergey Kudriavtsev


I think each of them can be created individually.

  • If the controller is responsible for the creation of the model, this would mean you can't have a model without having a controller, and that there always is a one-to-one mapping between a model and a controller. For example for a website you could have a controller for the regular data, and one for an xml version of this data (although typically this is the same controller and you just specify the protocol you want to use).
  • If the controller is responsible for the creation of the view, you would end with a one-to-one mapping between controller and view. The controller just handles an incoming request, performs some logic, and provides an answer. It shouldn't care who's asking for the data. For example for a web application you can have an RSS feed and a HTML page, both using the same controller. Another example is your typical Rails application, where one controller maps to multiple views (an index view, a show view, an edit view, ... )

It is however someone's responsibility to tie them all together, but that would be your main application and neither one of those components

like image 26
Robin Avatar answered Oct 13 '22 00:10

Robin