Hey guys in this post, we will discuss the Java MVC design patterns with an example. The idea behind this architectural design pattern is to seperate the Presentation Layer from the Business Layer.
MVC stands for Model, View and Controller, it is an architectural design pattern of separation of concerns within a software application. If you take any Java Enterprise application, that is divided into 3 layers, Model, View and Controller.
Watch the video
The Model contains the data or the information that the application required. It also represents the state of our application. Model contains the business logic of our entire application, it performs all the operations that our application is required. For example, if we are creating an application for calculator then the Model contains the information about adding the numbers, subtracting the numbers, multiplying the numbers or dividing the number. Let’s take another example, if we are creating customer dashboard, then it contains the information about connecting to the database, reading the data, writing the data and other database operations. Model contains the java objects that can do certain business logic.
Model layer is completely independent from the Presentation Layer. It never communicate with the Presentation Layer directly. It only takes the information from the Controller and updates the Model by performing business logic, and send the information back to the Controller.
Note that, Model never manages the control flow of the application neither it never takes information directly from the client.
Presentation Layer is the one that contains the information about what the user sees. It is also completely independent from the Business Layer. It takes the information from the Controller and display it in the browser in such a way that the user is expected. It contains the Model data to be displayed in the browser.
There are so many view template engines available that supports Java such as JSP, JSF, Velocity, Freemaker and Thymeleaf.
Controller is the one that sits between the Presentation Layer and the Business Layer. It manages the control flow of the application and it accepts the HTTP requests. It takes the information from the HTTP request and send it to the Model for further processing and once the Model sends the information back to the Controller, its the job of the Controller to prepare the data or information and send it to the Presentation Layer.
Note that Controller does not contain any business logic of our application, neither it not contains the database connections. It only manages the control flow of the application.
By using MVC in our application, if any errors/bugs comes, then you can fix it in particular layer without touching the other layers. Also it helps in the parallel programming meaning if one developer is working on the Presentation Layer, other developer can work on the Business Layer because these layers are independent on each other.
Alright, that’s all about MVC architectural design pattern. If you like this post, share it with your friends and colleagues.