TOPIC E - Introduction to Model View Controller
TOPIC E - Introduction to Model View Controller
1
Outline
Separation of Concerns
Introduction to Model View Controller (MVC)
MVC Examples
MVC Exercises
2
Separation of Concerns
Separation of concerns (SoC) is a
design principle for separating a computer
program into distinct sections such that
each section addresses a separate
concern.
A concern is a set of information that
4
Is this new?
Not Really.
This is very related to the Encapsulation
Design Principle
Encapsulation: group the data and the
methods that operate on the data in the
same module (or Class)
5
How does this relate to User
Interface Design?
In designing User Interfaces, we have
the following three main components:
1) The Data (i.e a student course grades)
2) The User Interfaces/Screens (i.e how you
want to represent the data to the users)
3) The Logic (i.e the rules that governs the GPA
calculations)
Data = Model
User Interfaces = View
Logic = Controller
6
Model View Controller (MVC)
Software architecture pattern that
separates the model, the user interface
and control logic of an application in
three distinct components.
MVC proposes the construction of three
distinct components. One side for the
representation of information, and on the
other hand for user interaction.
7
But Why?
The main purpose of MVC is to:
Facilitate Application Maintenance
Reduce effort to develop new features
8
History 101
This model is not new, it was introduced
in 1987 in the design of the Smalltalk
programming language.
With the boom of Web applications, it
has proven to be a programming mode
that fits quite well with the internet.
Usually, the model and the controller run
server side, and the view on the client
side.
9
MVC Example
We are going to create a Student
object acting as a model.
StudentView will be a view class
which can print student details on
console.
StudentController is the controller
class responsible to store data in
Student object and update view
StudentView accordingly.
10
Step 1 – Create the Model
public class Student {
private String rollNo;
private String name;
12
Step 3 – Create the Controller
public class StudentController {
private Student model;
private StudentView view;
13
Step 4 – MVC Demo
Use the StudentController
methods to demonstrate MVC
design pattern usage
public class MVCPatternDemo {
public static void main(String[] args) {
controller.updateView();
controller.updateView(); }
Student:
Name: John
Roll No: 10
15
Summary
Model View Controller (MVC) is a
popular, widely adopted design practice
The underlying principle is the
Separation of Concerns: Separate the
Data, how the data is presented, and the
rules for updating the data.
MVC facilitates maintenance and
reduces effort to adding new features.
16
Exercise
Consider the following hypothetical example.
Provide an MVC design for an implementation.
Start by creating the Model, the Controller, and
the View Classes (and the Demo).
Every MoneyCoin has a value, issue date, and
owner ID. These coins are controlled by FedCoin,
which has the sole authority to create coins, and
update their information. FedCoin can view a list
of all issued Coins by date and by owner.
17