The development of applications involves a set of tasks that may touch multiple concerns. Let’s see an example.
Mario wants to create his new web application to manage subscriptions to an event information service. He wants to start by creating a page to receive the user’s data and register it into a database, as he has some newbie experience with Java, he has chosen JSP (Java Server Pages) as the technology to build the entire system.
The JSP page he is about to write will contain the form to receive the subscription will validate the received data, connect to a database to persist this information and show a message to the user indicating that is now subscribed to the service, all in one single page, why to bother writing more than that right?
Later, as the application grows he will add a page to see the list of all the subscribers, another to register a new event on the database.
Each new functionality will follow the exact same one-single page approach, and all of them are connecting to the same database system.
The business is good and now Mario is in need of giving to the whole page a brand new style, so he hires a web designer to refresh the page’s layout. There is only one problem… this designer doesn’t understand the java code in the JSP pages (and hates Java, like any web designer)
So, to solve this issue, Mario hires a Java developer who suggests him to rewrite the entire application separating the presentation from the Java code, and Mario is now confused about what to do.
MVC: What is it?
MVC is a software design pattern that aims for separating concerns in an application. It stands for Model – View – Controller after the “elements” composing this pattern.
MVC is not new, it was first introduced back in the 1970s but became popular among Java developers only until the earlies 2000s, and is currently widely used and implemented in frameworks like Spring, Angular, Laravel, among many others
There are also a lot of variants that have evolved from MVC, e.g MVVM (Model-View ViewModel), MVA (Model – View – Adapter), MVP (Model – View – Presenter).
When implementing a solution following the MVC pattern we’ll divide our application into 3 main “sections”:
When using MVC, the model is not supposed to contain any logic in how to present the information, the view shouldn’t know how to manipulate data, only how to present it or access it. And the controller must not contain any presentation or data manipulation logic, it is only in charge of coordination between the model and the view.
This helps us to have a proper separation of concerns and a loose coupling between components of an application.
Advantages and Disadvantages
Some of the advantages of using the MVC pattern are:
- Separations of concerns
- Loose coupling
- Reuse of code
- Ease of maintenance
Of course, nothing is perfect in life, and MVC ain’t an exception. Some disadvantages of this pattern are:
- Difficult code navigability
- Steep learning curve
- Difficult Multi-artifact consistency