Model View Controller (MVC)

From Coder Merlin
Revision as of 10:59, 18 August 2021 by Akshat-sharma (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Introduction[edit]

The main idea surrounding the model, view, controller (MVC) is designating a distinct purpose to different parts of your code. It can be thought of as a method of organization. The MVC is split into three parts, the Model, View and Controller. MVCs are primarily used for user interaction (UI) and interaction layers.

Model[edit]

Collection of tasks that contain the functionality of a program. This section can be seen as the brains of the code as it contains all the data, logic and rules of the application. Ex. When the button is pressed, the model does something in response to the input and sends the output back to the controller.

View[edit]

Contains the user interface. This section is primarily focused on the visuals that a user sees, such as the font or color that appears on the screen. Additionally, functions that define the way users use the app are stored here, and the information requests sent by the user are transferred from the View to the Model by the Controller. Ex. The look of a button

Controller[edit]

The Controller is the primary connection between the Model and View. While the View focuses on presenting and taking information to and from the user, and the Model manipulates the data, rules and logic of the application, the Controller is the middleman that takes the input from the View and updates the Model. The Controller basically acts as a “conduit” or a connection between the View and the Model, and interprets the input from the View to and sends data to the Model.

MVC Process.png

MVC Process[edit]

  1. The user request information from the controller, or gives a user input from the View
  2. This input/request from the User is then given to the Controller, which will interpret the input.
  3. The Controller will then send interpreted data to the Model
  4. The Model will manipulate the data with the logic,rules and framework of the program, and is the only part of the MVC that can access the database.
  5. The Model will then return this data to the Controller
  6. The Controller will take the new Model data and communicate it with the View
  7. The View will take the new data and update view objects for the user in the user interface.
  • Note that the Controller will never directly manipulate the data or the view objects.

History[edit]

The MVC was originally developed in the 1970s by Trygve Reenskaug, but a general concept wasn’t available until an article was published in 1988 called The Journal of Object Technology. The MVC has evolved since then, making several different variants -

  • Hierarchical model-view-controller (HMVC)
  • Model-View-Adapter (MVA)
  • Model-View-Presenter (MVP)
  • Model-View-View-Model (MVVM)

The MVC methodology was popularized through NeXT’s WebObjects in 1996, originally utilized for Objective-C. It was later popularized again by Java developers in the early 2000s.

Why use MVC?[edit]

Using a Model-View-Controller framework comes with several advantages. To start, this method involves separating the development of an application into three core components. This allows for more efficient collaboration as multiple people can work on different components at the same time. Furthermore, this method of development allows for faster transitions when changing a program. For example, changing the layout of buttons in the UI will only affect the View code, rather than having to edit the whole program. Finally, the use of this framework is fairly straightforward when it relates to the human thought process. It provides a simple way of planning and structuring a program that even an inexperienced programmer could understand. With all this said, there are some disadvantages that need to be considered. The MVC Architecture has multiple components and therefore increases the complexity of the program and may result in some inefficient transfers of data. It’s also not the best idea to apply this when creating a small program, as it is typically only efficient when applied by a large team of programmers to “parallel program” (work on multiple parts of the code at the same time).

Key Concepts[edit]

Key ConceptsKeyConceptsIcon.png

Model View Controller

A design pattern that is used to organize code

Model

Holds raw data / framework of program

View

User interface

Controller

Communication between the Model and View Receives user input and model output

Input

Information given to the application

Output

Information that the application produces


References[edit]