What is MVC?
MVC stands for Model, View, Controller.
It is a Software Architectural Pattern that is used to make it easy to develop and maintain applications by organizing code in a logical manner.
The goal is to abstract the display of information from the user’s interaction, and vice-versa.
An Analogy (The Beauty Pageant)
“Imagine you are an audience member of a beauty pageant to determine the next Ms. MVC”
The Beauty Pageant
“Think of MVC as how a beauty pageant would be organized.”
Model: The ideal image of our contestants, a model who helps the contestants out.
View: The stage that the pageant will be shown on
Controller: The director which tells the contestants where to go and what to do.
The Model
“Think of the model as last year’s winner of the beauty pageant. She is stunningly beautiful, her figure is perfect, she is wearing the nicest clothes, and she is opinionated. All of the contestants in the beauty pageant are striving to match her beauty, and she gives guidance to the contestants.”
The model represents the data and logic of our application. It speaks directly with the database and to the controller to let the database know exactly what it should do.
The model is responsible for for storing, retrieving, and updating data in the database and performs any necessary calculations or processing.
In terms of our analogy, we can think of the model as a literal supermodel. Her beauty is enough to command the contestants to want to look like her. She is there to help the contestants in the beauty pageant by telling them how they should look (schema) and what they should do.
The View
“Think of the view as the stage of our beauty pageant. The stage displays our contestants for the audience to see and judge. In a software application, the view is the user interface that our users interact with and experience.”
The view is responsible for rendering the data from the model in a specific format. It is the UI of our application and is what the user sees and ultimately interacts with when coupled with the controller.
In terms of our analogy, we can think of the view as the stage where the beauty pageant is being shown. The view is what is shown to the audience and everything that happens behind the stage is abstracted from the audience.
The Controller
“Think of the controller in MVC as the director of the beauty pageant. The controller tells contestants what to do and how they should be prsented in the view, based off of data from the model.”
The controller is responsbile for rendering the data from the model in a specific format. It is the intermediary between the view and the model. It receives requests from the view, retrieves or updates data in the model, and then sends the data back to the view.
In terms of our analogy, think of the controller as the director of the beauty pageant, who is telling all of the contestants where to go and when. Behind the scenes, the supermodel will tell our contestants what to do and how to look when being prompted by the controller. In a web application, the controller would see that the users are requesting the data of a particular user, and then retrieve that data from the model to be rendered by the view.
Another Analogy (The Video Game)
If you’re still feeling lost after reading through my anology, try looking at it from a videogame perspective.
The model in MVC is like the avatar or character of the game. The model represents the data and logic of the game, like each character’s health, inventory, and special abilities.
The view in MVC is like the screen that your game is displayed on. It shows you what your character is doing and what the world looks like. You can see your character’s action and environment, but need the controller to interact with the model directly.
The controller in MVC is like the controller you use to play the game is like the controller in MVC. It enables you to interact with the character and environments being shown on the view.
Still Stuck?
These are the two analogies that have helped me solidify MVC in my brain. If you’re still stuck, try making your own analogy to describe MVC to something that is relevant in your life! It’s a great way to learn and form new connections in the brain.