0% found this document useful (0 votes)
76 views

Experiment 10

The document discusses implementing the Model-View-Controller (MVC) design pattern in Java. It creates classes for the model (Course), view (CourseView), and controller (CourseController). The model stores course data, the view displays it, and the controller coordinates communication between them. It demonstrates how to set/get data from the model and update the view through the controller. Finally, it includes sample output and multiple choice questions to test understanding of MVC patterns.

Uploaded by

sumit jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Experiment 10

The document discusses implementing the Model-View-Controller (MVC) design pattern in Java. It creates classes for the model (Course), view (CourseView), and controller (CourseController). The model stores course data, the view displays it, and the controller coordinates communication between them. It demonstrates how to set/get data from the model and update the view through the controller. Finally, it includes sample output and multiple choice questions to test understanding of MVC patterns.

Uploaded by

sumit jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Shivajirao Kadam Institute of Technology and Management, Indore

(Acropolis Technical Campus)


Department of Computer Science and Engineering

CS606 Skill Development Lab

Experiment No. 10

Problem Statement: - Design an a module using Model-View-Controller design Pattern.

Objective To implement the concept of design pattern

Implementation of MVC using Java


To implement a web application based on MVC design pattern, we will create
 Course Class, which  acts as the model layer
 CourseView Class, which defines the presentation layer (view layer)
 CourseContoller Class, which acts as a controller
Now, let’s explore these layers one by one.

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.
package MyPackage;
 
public class Course {
       private String CourseName;
       private String CourseId;
       private String CourseCategory;
        
       public String getId() {
          return CourseId;
       }
        
       public void setId(String id) {
          this.CourseId = id;
       }
        
       public String getName() {
          return CourseName;
       }
        
       public void setName(String name) {
          this.CourseName = name;
       }
        
       public String getCategory() {
              return CourseCategory;
           }
        
       public void setCategory(String category) {
              this.CourseCategory = category;
           }
        
    }
The code is easy to understand and is self-explanatory. It consists of functions to get/set course
details.

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.
package MyPackage;
 
public class CourseView {
       public void printCourseDetails(String CourseName, String CourseId,
String CourseCategory){
          System.out.println("Course Details: ");
          System.out.println("Name: " + CourseName);
          System.out.println("Course ID: " + CourseId);
          System.out.println("Course Category: " + CourseCategory);
       }
    }

The Controller Layer


The Controller is like an interface between Model and View. It receives the user requests from
the view layer and processes them, including the necessary validations. The requests are then
sent to model for data processing. Once they are processed, the data is again sent back to the
controller and then displayed on the view. Let’s create CourseContoller Class which acts 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){
          model.setName(name);     
       }
 
       public String getCourseName(){
          return model.getName();      
       }
 
       public void setCourseId(String id){
          model.setId(id);     
       }
 
       public String getCourseId(){
          return model.getId();    
       }
 
       public void setCourseCategory(String category){
              model.setCategory(category);     
       }
 
           public String getCourseCategory(){
              return model.getCategory();      
       }
       public void updateView(){               
          view.printCourseDetails(model.getName(), model.getId(),
model.getCategory());
       }   
    }

A cursory 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.

Main Java Class


Let’s call this class “MVCPatternDemo.java”. Check out the code below.
package MyPackage;
 
3public class MVCPatternDemo {
       public static void main(String[] args) {
 
          //fetch student record based on his roll no from the database
          Course model  = retriveCourseFromDatabase();
 
          //Create a view : to write course details on console
          CourseView view = new CourseView();
 
          CourseController controller = new CourseController(model, view);
 
          controller.updateView();
 
          //update model data
          controller.setCourseName("Python");
          System.out.println("nAfter updating, Course Details are as
follows");
 
          controller.updateView();
       }
 
       private static Course retriveCourseFromDatabase(){
          Course course = new Course();
          course.setName("Java");
          course.setId("01");
          course.setCategory("Programming");
          return course;
       }
    }

The above class fetches the course data from the function using which user enters the set of
values. It then pushes those values into the Course model. Then, it initializes the view we had
created earlier in the article. Further, it also invokes the CourseController class and binds it to
the Course class and the CourseView class. The updateView() method which is a part of the
controller then updates the course details on the console. Check out the output below.

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 provides an altogether new level of modularity to your


code which makes it a lot more readable and maintainable. This brings us to
the end of this article. Hope you are clear with all that has been shared with
you.

MCQ FOR MVC

Q 1 - In MVC pattern, View represents the visualization of the data that model contains.
A - false
B - true
Q 2 - Which of the following pattern is used to separate application's concerns?
A - Visitor Pattern
B - MVC Pattern
C - Business Delegate Pattern
D - Composite Entity Pattern
3. What does MVC pattern stands for?
a) Mock View Control
b) Model view Controller
c) Mock View Class
d) Model View Class
4. Which design pattern works on data and action taken based on data provided?
a) Command pattern
b) Singleton pattern
c) MVC pattern
d) Facade pattern
5. In the model-view-controller (MVC) architecture, the model defines the
1. Data-access layer
2. Presentation layer
3. Business-logic layer
4. Interface layer

----------------- End of the experiment 10 -----------------

You might also like