LAB # 14 MVC (Model View Controller) Pattern Objective
LAB # 14 MVC (Model View Controller) Pattern Objective
LAB # 14
OBJECTIVE
Implementation of MVC (Model View Controller) Pattern.
THEORY:
The Model-View-Controller (MVC) software design pattern is a method for separating
concerns within a software application. In principle, the application logic, or controller, is
separated from the technology used to display information to the user, or the view layer.
As the name implies, the MVC pattern has three layers: The Model defines the business layer
of the application, the Controller manages the flow of the application, and the View defines
the presentation layer of the application.
1. The browser on the client sends a request for a page to the controller present on the
server.
2. The controller performs the action of invoking the model, thereby, retrieving the data it
needs in response to the request.
3. The controller then gives the retrieved data to the view.
4. The view is rendered and sent back to the client for the browser to display.
Separating a software application into these three distinct components is a good idea for a
number of reasons. Let’s take a look at what those are.
Java Code:
Step 1
Create the Model Layer.
In the MVC design pattern, the model is the data layer which defines the business logic of the
system and also represents the state of the application. The model objects retrieve and store the
state of the model in a database. Through this layer, we apply rules to data, which eventually
represents the concepts our application manages. Now, let’s create a model using Course Class.
Course.java
The code is easy to understand and is self-explanatory. It consists of functions to get/set course
details.
Step 2
Create the View Layer.
This layer of the MVC design pattern represents the output of the application or the user interface.
It displays the data fetched from the model layer by the controller and presents the data to the user
whenever asked for. It receives all the information it needs from the controller and it doesn’t need
to interact with the business layer directly. Let’s create a view using CourseView Class.
Here we have used JFrame. The javax.swing.JFrame class is a type of container which inherits
the java.awt.Frame class. JFrame works like the main window where components like labels,
buttons, textfields are added to create a GUI (Graphical User Interface).
CourseView.java
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
frame.setVisible(true);
//System.out.println("Course Details: ");
//System.out.println("Name: " + CourseName);
//System.out.println("Course ID: " + CourseId);
//System.out.println("Course Category: " + CourseCategory);
}
}
Step 3
Create the Controller Layer.
The Controller is like an interface between Model and View. Let’s create CourseContoller
Class which acts as a controller.
CourseController.java
public class CourseController {
private Course model;
private CourseView view;
}
A glance at the code will tell us that this controller class is just responsible for calling the model
to get/set the data and updating the view based on that. Now let’s have a look at how all of these
are tied together.
Step 4
Main Java Class.
public class MVCPattern {
controller.updateView();
controller.updateView();
}
Output:
Exercise:
Consider the scenario of online shopping. New Year celebrations are going on and different
brands are offering discounts on their websites. Select a brand of your choice. (Clothing, shoe,
jewelry, toys, etc.). Implement MVC design pattern and display the view with first the original
items prices and then the updated view with the discounted prices.