Model-View-Controller in JAVA

4 min readMay 20, 2021

MVC Pattern stands for Model-View-Controller Pattern in Java. MVC was first described way back in 1979. Initially introduced by Microsoft and then later on implemented in many other technologies. In the Web Development field, MVC is the most talked-about designing patterns in the web programming world. This architecture was initially included with two major web development frameworks — Struts and Ruby On Rails.

It is a three-tier design pattern. All the units of the application can be parted and can be kept separate to be used anytime, anywhere. adoptIt is almost like an architectural pattern and never for complete application. MVC always is relating to the layer of interaction of an application. Model maintains the state of every visual component. View renders the visual components on the physical appearance device (e.g. monitor). Controller handles the reactions between the users and reacts respectively.

All the design models based on this architecture follow this pattern of design and while designing the software segregation of application layer from the user interface is done. Swing uses Model-View-Controller Architecture as well. The name only suggests that this Model-View-Controller design pattern will be having three layers, which are:

  • Model — Showing the business layer of the application.
  • View — Describing the presentation of the application.
  • Controller — Handling the flow of the application.

Model :
States data for each component.
Different for each component.
Examples :
Scrollbar : Max and Min values, current position, width of the thumb.
Menu : Simple list of items.
Model data is never dependent on visual representation.
— It contains only the core application data, it will never have explanation of how to present the data to a user.

View :
Indicates how component is painted on the screen.
Can vary between platform’s look and feel.
— The view is aware of the way to access the model’s data, but the meaning of the same is unknown or how and what the user can do to manipulate it.

Controller :
The Controller only resides between the view and the model.
This dictates how component interacts with events.
Many forms of events such as mouse clicks, keyboard, focus, repaint.
— Controller decides how component reacts to any particular event.

Swing uses MVC Architecture as well. It uses simplified model of the MVC Design : Model-Delegate.

Combines View and Controller into a single element as UI Delegate.

Model : Maintains information.
UI-Delegate : Draws and reacts to the events that propagate through the component.

Implementation of MVC in JAVA :
To construct a web application based on this pattern of design, the creation of-

  • Course Class, it will be Model Layer representation.
  • Course View Class, it will define View Layer.
  • Course Controller Class, it will act as Controller Layer.

Here is the elaboration of the above elements-

The Model Layer Implementation

The Model Layer objects retrieve and store the state of the model in a database. Now, let’s create a model using Course Class.

package MyPackage;
public class Course {
private String CName;
private String CId;
private String CCat;

public String getId() {
return CId;

public void setId(String id) {
this.CId = id;

public String getName() {
return CName;

public void setName(String name) {
this.CName = name;

public String getCategory() {
return CCat;

public void setCategory(String category) {
this.CCat = category;


The View Layer Implementation

Representation of output of the application or the user interface is carried out or represented by this layer of the MVC Design Pattern. Let’s create a view using CourseView Class.

package MyPackage;

public class CourseView {
public void printCourseDetails(String CName, String CId, String CCat){
System.out.println(“Course Details: “);

System.out.println(“Name: “ + CName);
System.out.println(“Course ID: “ + CId);
System.out.println(“Course Category: “ + CCat);

The Control Layer Implementation

Acts like an intermediate interface in the middle of Model and View. It receives the user requests from the view layer and processes them. Let’s create CourseContoller Class which will act as a controller.

package MyPackage;

public class CourseController {
private Course model;
private CourseView view;

public CourseController(Course model, CourseView view){

this.model = model;
this.view = view;

public void setCourseName(String name){

public String getCourseName(){
return model.getName();

public void setCourseId(String id){

public String getCourseId(){
return model.getId();

public void setCourseCategory(String category){

public String getCourseCategory(){
return model.getCategory();

public void updateView(){
view.printCourseDetails(model.getName(), model.getId(), model.getCategory());


Controller class is just responsible for calling the model to get/set the data and updating the view based on that.
Let’s tie these together in a JAVA main program.

Main JAVA Class

Defining a class called : “”

package MyPackage;

public class MVCDemo {
public static void main(String[] args) {
Course model = retriveCourseFromDatabase();
CourseView view = new CourseView();
CourseController controller = new CourseController(model, view);

//update model data
System.out.println(“\n After updating, Course Details are as follows”);

private static Course retriveCourseFromDatabase(){
Course course = new Course();
return course;

Output :

Course Details:

Name: Java

Course ID: 01

Course Category: Programming

After updating, Course Details are as follows

Course Details:

Name: Python

Course ID: 01

Course Category: Programming

The MVC Architecture is bringing a new level of modified code which provides better readability and maintainability.

Advantages of MVC Architecture

This Model-View-Controller Architecture provide various advantages for a developer or a coding enthusiast while developing applications, which include:

  • Multiple developers can work with the three layers (Model, View, and Controller) simultaneously.
  • Offers improved scalability, that supplements the ability of the application to grow.
  • As components have a low dependency on each other, they are easy to maintain.
  • A model can be reused by multiple views which provides reusability of code.
  • It makes any application easy to understand and also very presentable or expressive by adopting MVC.
  • Extending and testing of the application becomes easy.
  • Also, they can be modified individually and debugging becomes easy too.

Hope you are clear with all that has been shared with you.
This article is contributed by Pushkar Baichwal, Akash Gorantyal and Aditya Sadamate.