• No results found

Model-View-Controller (MVC) Design Pattern

N/A
N/A
Protected

Academic year: 2021

Share "Model-View-Controller (MVC) Design Pattern"

Copied!
17
0
0

Loading.... (view fulltext now)

Full text

(1)

Computer Science and Engineering „College of Engineering „The Ohio State University

Model-View-Controller (MVC)

Design Pattern

(2)

Computer Science and Engineering „The Ohio State University

Motivation

†

Basic parts of any application:

„ Data being manipulated

„ A user-interface through which this

manipulation occurs

†

The data is logically independent from

how it is displayed to the user

„ Display should be separately

designable/evolvable

†

Example: grade distribution in class

„ Displayed as both pie chart and/or bar chart

†

Anti-example: see BigBlob

„ Presentation, logic, and state all mixed

(3)

Computer Science and Engineering „The Ohio State University

Model-View-Controller Pattern

†

Model

„ The data (ie state)

„ Methods for accessing and modifying state

†

View

„ Renders contents of model for user „ When model changes, view must be

updated

†

Controller

„ Translates user actions (ie interactions

with view) into operations on the model

„ Example user actions: button clicks, menu

(4)

Computer Science and Engineering „The Ohio State University

Basic Interactions in MVC

Model Controller View Input Output “change data” “change display” “user action”

(5)

Computer Science and Engineering „The Ohio State University

Implementing Basic MVC in Swing

† Mapping of classes to MVC parts

„ View is a Swing widget (like a JFrame & JButtons) „ Controller is an ActionListener

„ Model is an ordinary Java class (or database)

† Alternative mapping

„ View is a Swing widget and includes (inner) ActionListener(s) as event handlers

„ Controller is an ordinary Java class with “business logic”, invoked by event handlers in view

„ Model is an ordinary Java class (or database)

† Difference: Where is the ActionListener?

„ Regardless, model and view are completely decoupled (linked only by controller)

(6)

Computer Science and Engineering „The Ohio State University

Mechanics of Basic MVC

† Setup

„ Instantiate model „ Instantiate view

† Has reference to a controller, initially null

„ Instantiate controller with references to both

† Controller registers with view, so view now has a (non-null) reference to controller

† Execution

„ View recognizes event

„ View calls appropriate method on controller

„ Controller accesses model, possibly updating it

„ If model has been changed, view is updated (via the

controller)

† Example: CalcMVC

„ CalcModel, CalcView, CalcController

„ Note: View includes (gratuitous) reference to model „ Note 2: The example code has a bug! Can you find it?

(7)

Computer Science and Engineering „The Ohio State University

Extended Interactions in MVC

Model Controller View Input Output “change data” “I have changed” “user action” “give me data”

(8)

Computer Science and Engineering „The Ohio State University

Extended Pattern

†

Background: Observer pattern

„ One object is notified of changes in another „ In extended MVC, view is an observer of

model

†

Application within MVC

„ Asynchronous model updates

† Model changes independent of user actions

† Associated view must be notified of change in

order to know that it must update

„ A model may have multiple views

† But a view has one model

† All views have to be updated when model

(9)

Computer Science and Engineering „The Ohio State University

Mechanics of Extended MVC

† Setup

„ Instantiate model

† Has reference to view, initially null

„ Instantiate view with reference to model

† View registers with model

„ Instantiate controller with references to both

† Controller registers with view

† Execution

„ View recognizes event

„ View calls appropriate method on controller

„ Controller accesses model, possibly updating it „ If model has been changed, it notifies all

registered views

„ Views then query model for the nature of the

(10)

Computer Science and Engineering „The Ohio State University

Problems with Classic MVC

†

Controller might need to produce its own

output

„ eg Popup menu

†

Some state is shared between controller

and view, but does not belong in model

„ eg Selection (highlighted text)

†

Direct manipulation means that user can

interact (control) visual elements (views)

„ eg Scrollbar

†

Overall issue: Input and output are often

intermingled in a GUI

(11)

Computer Science and Engineering „The Ohio State University

Delegate-Model Pattern

†

Model

„ Data, same as before

†

Delegate

„ Responsible for both input and output

„ A combination of both view and controller

†

Many other names

„ UI-Model

(12)

Computer Science and Engineering „The Ohio State University

Basic Interactions in Delegate Model

Model Controller View Input Output “change data” “change display” “user action”

(13)

Computer Science and Engineering „The Ohio State University

Basic Interactions in Delegate Model

Model Controller View Input Output “change data” “I have changed” “give me data” Delegate

(14)

Computer Science and Engineering „The Ohio State University

Mechanics of Delegate Model

†

Setup

„ Instantiate model

† As with MVC, model does not know/care about UI

„ Instantiate delegate with reference to model

†

Execution

„ Delegate recognizes event and executes

appropriate handler for the event

„ Delegate accesses model, possibly updating it „ If model has been changed, UI is updated

†

Example: CalcV3

„ CalcModel, CalcViewController

„ Note: CalcModel is exactly the same as with

(15)

Computer Science and Engineering „The Ohio State University

Notes

†

Litmus test: Swapping out user

interface

„ Can the model be used, without

modification, by a completely different UI?

„ eg Swing vs console text interface

†

Model can be easily tested with JUnit

†

Model actions should be quick

„ GUI is frozen while model executes

„ Alternative: multithreading, which gets

(16)

Computer Science and Engineering „The Ohio State University

Supplemental Reading

†

Sun Developer Network

„ “Java SE Application Design with MVC”

„ http://java.sun.com/developer/technicalAr

ticles/javase/mvc/

†

OnJava article

„ “A Generic MVC Model in Java”

„ http://www.onjava.com/pub/a/onjava/20

(17)

Computer Science and Engineering „The Ohio State University

Summary

†

Motivation: Information hiding

„ Data (state) vs user interface

„ State should be agnostic of user interface

†

Model-View-Controller

„ Model contains state (data)

„ View displays model to user (presentation) „ Controller modifies model (business logic)

†

UI-Model

„ Allows for tight coupling between view and

controller

References

Related documents

This study explores the perception of the body from members both in and outside of the bodybuilding community using qualitative grounded theory content analysis (Strauss, 1987).

In the area of risk and financial services, the firm provides solutions in risk and capital management,. reinsurance intermediary services and actuarial consulting,

The results of multivariate analysis demonstrated that the diagnosis of the disease (AML), carbapenem use in ear- lier hospitalization and length of piperacillin/tazobactam use

In or- der to obtain the average emission during the low gamma-ray state we apply the same procedure as for the R-band flux; how- ever we apply a larger margin in time, using the

Those approaches that strategically reduce vehicle weight (using new lightweight materials to reduce weight while holding vehicle size constant and reducing the weight of the

Dari uraian di atas dapat disimpulkan bahwa konsep manajemen perpustakaan yang Islami selalu berlandaskan kepada prinsip dan nilai-nilai ketauhidan yang ada dalam

characters of the maxillae, mandible structure, and labial sensilla with respect to their phylogenetic value based on the ground plan was compared with the basic model within the

In this paper we are using a method called Social Network Analysis (SNA) to analyze the communication that takes place between the students of an online language-learning course..