Model View Controller (MVC)
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.
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.
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
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.
- The user request information from the controller, or gives a user input from the View
- This input/request from the User is then given to the Controller, which will interpret the input.
- The Controller will then send interpreted data to the Model
- 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.
- The Model will then return this data to the Controller
- The Controller will take the new Model data and communicate it with the View
- 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.
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?
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).