HapInS Developers Blog

HapInSが提供するエンジニアリングの情報サイト

Let's learn MVC architecture

Hello, Everyone!

May be you all already known about MVC architecture or may be not. Let me share my understandings of MVC architecture.

First of all, Do you know what MVC stands for?

It stands for Model, View, and Controller.That is a well-known design pattern in the web development field.

How do we define MVC?

The MVC(Model-View-Controller) is a software design pattern commonly used for developing user interfaces. It recognizes that an application shall consist of data model, presentation information and control information. It separates an application into three main logical components as Model, View, and Controller.

Model

It is an object that corresponds to all the data-related logic that the user works with. Also, it can be specified as a business layer of application.

View

It is used for visualization the data that the model contains. It can be specified as an presentation layer of application.

Controller

It handles all the interactions and acts as the data flow into a model object and updates the view whenever data is changed.

So, What are the benefits of using MVC?

Let me mention some benefits of using MVC.

By using MVC,

1) Development process becomes faster.

2) As MVC architecture allows easy modification of the entire application, the application becomes more understandable.

3) As there are multiple levels properly written in the application, debugging process and testing is easier.

Well, Let’s see MVC implementation process with Java!

At first, we need to understand generally how MVC works with java programming. In Java, Model contains java classes, the View used to display the data and Controller contains servlets.

1) The browser (client) sends request to the Controller on the server side.

2) The Controller calls the Model to collect the requested data.

3) Then the Controller transfers the retrieved data to the View.

4) Finally, returns the result to the browser (client) by the View.

Implementation of MVC with Java programming

Let's create the following three classes to implement MVC architecture in Java.

1)Student Class, will perform as a Model component. As the Model component represents a business layer of application, it manipulates the business logic for application and also the state of application. The model object fetch and store the model state in the database.

Student.java

2)StudentView Class, will perform as a view component. As the View component represents a presentation layer of application, it sends the requested data to the client, that is fetched from Model component by Controller.

StudentView.java

3)StudentContoller Class, will perform a controller component. It acts as an interface between Model and View. The Controller gets the user requests from the View and processes them. Then, the requests are sent to Model for data processing. Once they are processed, the data is sent back to the Controller and then displayed on the View.

StudentContoller.java

Main Class

As a main class, it retrieves the student record from the method.And then it pushes those values in the Model. After that, it initializes as StudentView.java . When View is initialized, the StudentController.java is invoked and bind it to Student class and StudentView class. Finally, the updateView() method updates the student information to print in the console.

StudentMainMVC.java

Console Output:

In this way, we can understand about MVC architecture. I'll be happy if my sharing is helpful to you. Thank you so much for reading till the end. Have a nice day!

References:

MVC Wikipedia