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

SDA - Lab 7 - 2k21 - Part1-1

The document discusses the Model View Controller (MVC) design pattern. MVC separates an application into three main components: the model, the view, and the controller. The model manages the data and business logic, the view displays the model's data to the user, and the controller handles user input and updates the model and view. The document provides an example of MVC in Java using classes to represent a student model, view, and controller. It also assigns a lab task to create an MVC application for managing employee records with classes for the employee model, view, and controller.

Uploaded by

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

SDA - Lab 7 - 2k21 - Part1-1

The document discusses the Model View Controller (MVC) design pattern. MVC separates an application into three main components: the model, the view, and the controller. The model manages the data and business logic, the view displays the model's data to the user, and the controller handles user input and updates the model and view. The document provides an example of MVC in Java using classes to represent a student model, view, and controller. It also assigns a lab task to create an MVC application for managing employee records with classes for the employee model, view, and controller.

Uploaded by

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

Software Design and Architecture

Laboratory # 07

UET TAXILA
Engr. Sidra Shafi

CLO Learning Outcomes Assessment Item BT Level PLO


No.
1 Construct the experiments / projects Lab Task, Mid Exam,
of varying complexities. Final Exam, Quiz,
P2 3
Assignment, Semester
Project
2 Use modern tool and languages. Lab Task, Semester
P2 5
Project
3 Demonstrate an original solution of Lab Task, Semester
A2 8
problem under discussion. Project
4 Work individually as well as in teams Lab Task, Semester A2 9
Project
Interactive Systems: Model View Controller
Statement Purpose:
After this Lab, students will be able to understand Model View Controller Design Pattern /
Architecture Style.

Tool: Eclipse

Introduction:

The Model View Controller (MVC) design pattern specifies that an application consist of a data
model, presentation information, and control information. The pattern requires that each of these
be separated into different objects.

MVC is more of an architectural pattern, but not for complete application. MVC mostly relates
to the UI / interaction layer of an application. You are still going to need business logic layer,
and data access layer.

Figure 1: Model View Controller

Separation of concerns: view and controller depend on model, but model depends on neither.

3rd Semester UET TAXILA


• The Model contains only the pure application data, it contains no logic describing
how to present the data to a user.
• The View presents the model’s data to the user. The view knows how to access the
model’s data, but it does not know what this data means or what the user can do to
manipulate it.
• The Controller exists between the view and the model. It listens to events triggered
by the view (or another external source) and executes the appropriate reaction to these
events. In most cases, the reaction is to call a method on the model. Since the view
and the model are connected through a notification mechanism, the result of this
action is then automatically reflected in the view.

A fundamental design pattern for the separation of user interface logic from business logic.

Computer Quote App:


• Model (or Domain)
Computer science authors and what they said
• View
The interface shows the quotes
• Controller
Intercept users’ mouse clicks
Inform model (or domain) about quote to display
Inform view to update the quote to be displayed.

Figure 2: A JAVA application using Model View Controller

3rd Semester UET TAXILA


Example of MVC in Java:
Student.java

public class Student {


private String rollNo;
private String name;
public String getRollNo()
{
return rollNo;
}
public void setRollNo(String rollNo)
{
this.rollNo = rollNo;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}

StudentView.java

public class StudentView {


public void printStudentDetails(Student model)
{
System.out.println("Student: ");
System.out.println("Name: " + model.getName());
System.out.println("Roll No: " + model.getRollNo());
} }

StudentController.java

public class StudentController {


private Student model;
private StudentView view;
public StudentController(Student model, StudentView view)
{
this.model = model;
this.view = view;
}
public void setStudentName(String name)
{
model.setName(name);
}
public String getStudentName()
{
return model.getName();
}
public void setStudentRollNo(String rollNo)
{
model.setRollNo(rollNo);

3rd Semester UET TAXILA


}
public String getStudentRollNo()
{
return model.getRollNo();
}
public void updateView()
{
//view.printStudentDetails(model.getName(), model.getRollNo());
view.printStudentDetails(model);
}
}
public class MVC_Pattern {
public static void main(String[] args) {
Student model = retriveStudentFromDatabase();

StudentView view = new StudentView();


StudentController controller = new StudentController(model, view);
controller.updateView();
controller.setStudentName("Ayesha Ahmed");
controller.updateView();
}
private static Student retriveStudentFromDatabase()
{
Student student = new Student();
student.setName("Sidra Shafi");
student.setRollNo("19-SE-150");
return student;
}
}

Output:

Advantages of MVC:

• Multiple developers can work simultaneously on the model, controller, and views.
• MVC enables logical grouping of related actions on a controller together. The views
for a specific model are also grouped together.
• Models can have multiple views.
• Make the code more organized and standard.
• Avoid Complexity.
• Enable easy debugging.

3rd Semester UET TAXILA


Disadvantages:
• The framework navigation can be complex because it introduces new layers of
abstraction and requires users to adapt to the decomposition criteria of MVC.
• Knowledge on multiple technologies becomes the norm. Developers using MVC need
to be skilled in multiple technologies.

Lab Task Marks: 10

Create an MVC Application in Java for the following scenario.

Suppose an organization wants to manage the record of its employees. Each Employee has
an id, name and job. Create appropriate methods to set these values in model. Employee
Model also has a paraemeterized constructor. The Controller recieves Employee obj and
View Obj and set the appropriate values and then call the PrintEmployeeInformation
method of View Class. In main method of MVCDemo class, call the paremeterized
contructor of employee class, and then create view and pass these two objects in controller
object. Use controller to display view. Change some values and then display view again, You
can also call updateview in each setter method of controller or call explicitly in main method.

Expected Output:

***************

3rd Semester UET TAXILA

You might also like