MVC Pattern: Decoupling your Applications
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 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.
Some of the advantages of using the MVC pattern are:
Of course, nothing is perfect in life, and MVC ain’t an exception. Some disadvantages of this pattern are:
Controller Java JSP Model Model View Controller MVC MVP MVVM Servlets View Web Development
Subscribe to our newsletter to be the first to know when new content is added
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.