Model-View Controller

peruvianwageslaveInternet and Web Development

Feb 5, 2013 (4 years and 6 months ago)

203 views

DESI GN PATTERN PRESENTATI ON AND
DI SCUSSI ON








RAJAT
ARYA

(
RAJATA@U
)

EFECS * OI M

Model
-
View Controller

Agenda


Definition


what is this fancy thing called MVC?


History / Origins


where does it come from?


Functional Description


how does MVC actually work?


Goal / Benefits


why is it important?


Example Scenarios


where could MVC be used?


Sample MVC Implementation Review


lets see this thing actually working


Modern Implementations


where does it exist right now?


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


(3
)

(2)

(1)

Controller

View

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


Scaling an application


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


Multiple user interfaces to application


web interface, mobile phone interface, point
-
of
-
sale interface,
desktop interface


multiple views, multiple controllers, single model

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


Implementations of MVC in web
-
based frameworks


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)

QUESTI ONS?

Fin.

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
-
Professional/dp/0201633612/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=12333
61435&sr=8
-
1



MVC



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



Observer Pattern


http://www.research.ibm.com/designpatterns/example.htm


http://en.wikipedia.org/wiki/Observer_pattern


http://msdn.microsoft.com/en
-
us/library/ms954621.aspx