Model-View Controller

Design pattern presentation and discussion

Rajat arya (rajata@u) EFECS * OIM

Agenda
 Definition

what is this fancy thing called MVC? where does it come from? how does MVC actually work? why is it important? where could MVC be used? lets see this thing actually working where does it exist right now?

 History / Origins

 Functional Description

 Goal / Benefits

 Example Scenarios

 Sample MVC Implementation Review

 Modern Implementations

 Questions?  Reference Materials

MVC Definition
The Model-View Controller Paradigm is a triad of classes used to build modular applications. MVC consists of three components (objects): Model – application object (data) View – user interface (presentation) Controller – defines how user interface reacts to user action

History
First Appeared Formally in: “A cookbook for using the model-view controller user interface paradigm in Smalltalk-8”
by Glenn E. Krasner and Stephen T. Pope Journal of Object-Oriented Programming, 1(3):26-49 August/September 1988

Functional Description Intro – Observer Pattern
 MVC utilizes two

applications of the Observer Pattern  Observer Pattern (aka Publish-Subscribe)  Attach / Detach / Notify

 Decouples Subjects from

Observers, subjects do not know about observers directly

MVC Functional Description
 MVC Definition: Specific

application of the Observer Design Pattern  Model is application object (data)  View is the user interface (presentation) – observes Model  Controller defines way user interface reacts to input – observes View  Observer Pattern Used Twice (Controller observes View and View observes Model)

View

Controller

Model

MVC Functional Description (2)
Controller maintains

knowledge of both View and Model, View has knowledge of Model Controller observes changes in View (1), informs Model (2) View observes changes in Model (3) and updates

View

(1)

(3)

Controller

(2) Model

Goal / Benefits
 Modularity  decoupling components  allows each component to be versioned independently  worked on by individuals on team (UI person, DB person, etc)  Flexibility  multiple Views for one Model (web frontend, desktop frontend, mobile frontend, etc)  replace one component (replace data storage from flat file to database)  Maintainability  only change one component where bug exists, less risk in late changes  Testability  each component communicates through contract so each component can be unit-tested independently

Example Scenarios
Spreadsheet application (classic)
  

same data (model) spreadsheet can have multiple charts (views) updating spreadsheet should result in updated charts example of multiple views, single controller, single model view is desktop or web, model is a flat-file database updating the database to clustered set of SQL servers only model needs to take changes for architecture, controller connects to new model and doesn’t know that the model is now backed by several DBs instead of one file web interface, mobile phone interface, point-of-sale interface, desktop interface multiple views, multiple controllers, single model

Scaling an application
  

Multiple user interfaces to application
 

Sample Implementation Review

Small WPF Application written in C# to illustrate how MVC. Email me if you would like a copy of the source code.

Modern Implementations
 GUI Frameworks for desktop applications
     

Application Kit – part of Cocoa for OS X desktop apps GTK+ - part of GTK libraries, used in lots of apps Microsoft Foundation Classes (MFC) – Document/View Architecture Java Swing Windows Presentation Framework (WPF) TK Toolkit – part of Tcl/Tk ASP .NET MVC Framework (.NET) ProMesh.NET (.NET) Grails (Java) Struts (Java) Spring (Java) PureMVC (JavaScript) Ruby on Rails (Ruby) Zend Framework (PHP) Django (Python) Pylons (Python)

 Implementations of MVC in web-based frameworks
         

Fin.
Questions?

Reference Links
 Design Patterns: Elements of Reusable Object-Oriented Software

If you get nothing else from this talk, please go look this up – it is the most important book I’ve read regarding object-oriented programming: http://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professio http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller http://www.ootips.org/mvc-pattern.html http://c2.com/cgi/wiki?ModelViewController http://st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html http://www.research.ibm.com/designpatterns/example.htm http://en.wikipedia.org/wiki/Observer_pattern http://msdn.microsoft.com/en-us/library/ms954621.aspx

 MVC
   

 Observer Pattern
  

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.