Model-View-Controller (MVC) Design Pattern
Model-View-Controller (MVC) Design Pattern
Computer Science and Engineering College of Engineering The Ohio State University
Lecture 23
Motivation
Computer Science and Engineering The Ohio State University
Basic parts of any application: Data being manipulated A user-interface through which this manipulation occurs The data is logically independent from
Example: grade distribution in class Displayed as both pie chart and/or bar chart Anti-example: see BigBlob Presentation, logic, and state all mixed together
Model-View-Controller Pattern
Computer Science and Engineering The Ohio State University
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 selections
Model
View Output
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)
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?
Controller registers with view, so view now has a (nonnull) reference to controller
Model
Output
give me data
Extended Pattern
Computer Science and Engineering The Ohio State University
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
changes
Setup
Instantiate model
Instantiate view with reference to model Instantiate controller with references to both
View registers with model 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 change, rendering new information as appropriate
output
eg Popup menu
Delegate-Model Pattern
Computer Science and Engineering The Ohio State University
Model Data, same as before Delegate Responsible for both input and output A combination of both view and controller Many other names UI-Model Document-View
Model
View Output
Model
give me data
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 CalcMVC
Notes
Computer Science and Engineering The Ohio State University
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 much more complicated
Supplemental Reading
Computer Science and Engineering The Ohio State University
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 04/07/07/genericmvc.html
Summary
Computer Science and Engineering The Ohio State University
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 Preserves most significant separation