MVC Architecture in 5 minutes: a tutorial for beginners
Tags: mvc, architecture, beginners, design
Published: 2020-05-29 17:57:55

*This article was written by Erin Doherty and was originally published at [Educative.io](https://www.educative.io/blog/mvc-tutorial).*

Model View Controller is a predictable [software design pattern](https://www.educative.io/blog/how-to-design-a-web-application-software-architecture-101) that can be used across many frameworks with many programming languages, commonly Python, Ruby, PHP, JavaScript, and more. It is popularly used to design web applications and mobile apps.

This blog post defines the concept of a Model-View-Controller (MVC) software design pattern and does a basic example Model-View-Controller in JavaScript/HTML/CSS. 

Today we will cover:
* What is MVC architecture?
* Benefits of MVC
* Web App as MVC
* Separation of Concerns 
* Resources 


<br> 

## What is MVC Architecture? 

The architecture components of the [MVC pattern](https://www.educative.io/edpresso/what-is-the-mvc-architecture) are designed to handle different aspects of an application in development. The MVC design pattern serves to separate the presentation layer from the business logic. 

Why do developers care about MVC? MVC is popular in app and web development, and it’s one of the most widely used software design patterns for app and web development.
The Model View Controller design pattern separates concerns into one of 3 buckets: 
<ul><li>Model</li><li>View</li><li>Controller</li></ul>


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xzo10y3gsi2wjt99lvfw.png)

The Model View Controller architectural pattern separates concerns into one of 3 buckets:
<ol>
<li> <b>Model:</b> stores & manages data.

Often a [database](https://www.educative.io/blog/database-design-tutorial), in our quick example we’ll use local web storage on a browser to illustrate the concept.</li>

<li> <b>View:</b> Graphical User Interface

The view is a visual representation of the data- like a chart, diagram, table, form.

The view contains all functionality that directly interacts with the user - like clicking a button, or an enter event.

 </li>
<li> <b>Controller:</b> Brains of the application.

The controller connects the model and view. The controller converts inputs from the view to demands to retrieve/update data in the model. 

The controller receives input from view, uses logic to translate the input to a demand for the model, the model grabs the data, the controller passes data from the model back to the view for the user to see in a nice display.
</li>
</ol>

<br>

<h2>Benefits of MVC</h2>

<ul><li>Traditionally used for Graphical user interfaces (GUIs)</li><li>Popular in web applications</li><li>MVC responsibilities are divided between the client & server, compatible with web application architecture</li><li>MVC is helpful design pattern when planning development
<li>Separation of Concerns: that code is divided based on function to either the model, view, or controller bucket</li>
<li> Works well with Ruby on Rails
</li>
<li>Loosely Coupled</li><li>Removes unnecessary dependencies</li><li>Reuseable without modification</li><li>	MVC makes model classes reusable without modification</li><li>	Code reuse</li><li>Extendable code</li><li>High Cohesion</li><li>Easier to maintain or modify</li><li>Supports Multiple views</li><li>Each part can be tested independently (Model, view, controller)</li>
</ul>

<br>

<h2>Web App as a MVC</h2>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fex9lq3bzvxw5kr1n9ej.png)

You can think of a web application as a Model View Controller design. MVC is popular in web applications, one of the reasons is because responsibilities are divided between the client & server. 

<br>

<h2>Separation of Concerns</h2>
MVC design allows for Separation of Concerns - dividing the logic up between the 3 buckets so that each bucket can act independently. 

The model, view, and controller don't depend on each other. Why does this matter? Generally, software is worked on by teams - a team might have a designer, engineer, and database architect. Separation of concerns means each team member can work on their piece at the same time because logic has been separated into buckets. Separation of concerns is also great for maintenance - developers can fix a bug in one piece of code, without having to check out the other pieces of code.

<h3>Loosely Coupled</h3>
Loosely coupled means that each piece: the model, view, and controller, act independently of each other. 

Developers can modify one of the pieces, and the other 2 pieces should keep working and not require modifications.
When designing MVC software – the logic in each of the three buckets is independent. Everything in View acts independently of the model – and vice versa, the view won’t have any logic dependent on the model. 

Making independent models and views makes code organization simple and easy to understand and keeps maintenance easier. Programmers can fix a bug in the view without changing the model code.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wrrloz5qskr5odu1fwdx.png)

The picture above shows what happens in an MVC web app when a user clicks a button, from the perspective of the user sitting at the computer, looking at their browser. The same sequence of events is shown below in a software diagram - thanks to Apple here.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/87zgi3brtq6abz5yby7e.png)

<br>

## Resources
Now that you have a sense of what MVC, keep the learning going with these useful resources. 
* [What is the MVC Architecture](https://www.educative.io/edpresso/what-is-the-mvc-architecture) (article) 
* [MVC Explained](https://www.educative.io/edpresso/mvc-explained) (article) 
* [Web Application Software & Architecture 101](https://www.educative.io/courses/web-application-software-architecture-101) (course)