MVC Pattern
An object-oriented method for separating
presentation logic
Lecture 16a: MVC business logic
(Model-View-Controller) business model
It facilitates reuse
Wendy Liu
CSC309F – Fall 2007 [Link]
1 2
Model View Controller MVC Architecture
Model
Views
VIEW
State Change
State
Query Change
Notification
User
Gestures
A = 50%
View Controller
Model
B = 30% View
C = 20% Selection
Method invocations
3 Events 4
1
Model View
Represents application data and business rules Renders the contents of a model
that govern access to and updates of this data Specifies how the model data should be presented
In enterprise software, a model often serves as a When the model changes, the view must update its
software approximation of a real-world process presentation
Notifies views when it changes and enables push model
the view registers itself with the model for change notifications
the view to query
pull model
Allows the controller to access application the view is responsible for calling the model when it needs to
functionality encapsulated by the Model retrieve the most current data
Forwards user gestures to the controller
5 6
Controller MVC Architecture
Defines application behavior Model
Encapsulates application state
Interprets user gestures and maps them into Responds to state queries
Exposes application functionality
actions Notifies view of changes
State Change
For the model to perform State
Change
Query
Notification
In selecting a different view
E.g., a web page of results to present back to the user User
View Gestures Controller
In a Web application, user gestures appear as Renders the model Defines application behavior
Requests updates from models Maps user actions to model updates
HTTP requests View
Sends user gestures to controller Selection Selects view for response
Allows controller to select view One for each functionality
Method invocations
7 Events 8
2
Best Practices: MVC Separation Interaction between MVC Components (1)
Organize model elements, views and controller Once the model, view, and controller objects are instantiated,
actions into “physically” separate: the following occurs:
Source code units
Source code packages
1. The view registers as a listener on the model
Any changes to the underlying data of the model immediately
Benefits: result in a broadcast change notification, which the view receives
Helps enforce logical separation of concerns
This is an example of the push model described earlier
In a large project, a developer with specialized skillset
The model is not aware of the view or the controller
may work on just one of M, V or C
It simply broadcasts change notifications to all interested listeners
Simplifies maintenance, e.g. porting app to new platform
Frameworks that emphasize MVC separation: 2. The controller is bound to the view
Apache Struts I.e., any user actions that are performed on the view will implicitly
invoke a registered event listener method in the controller class
JavaServer Faces
Ruby on Rails 3. The controller is given a reference to the underlying model
9 10
Interaction between MVC Components (2) MVC Example
Once a user interacts with the view, the following
actions occur:
1. The view recognizes that a user action has occurred
2. The view generates an event, which implicitly
invokes appropriate method in the controller
3. The controller accesses the model
Possibly updating it with respect to the user's action
4. If the model has been altered, it notifies interested
listeners, such as the view, of the change
The controller may also update the view
11 12
3
Known Examples
A few well-known examples of RESTful, resource-oriented
web services include:
Services that expose the Atom Publishing Protocol
Lecture 16b: RESTful,
[Link]
Atom’s variants such as GData
Resource-Oriented Web Services [Link]
Amazon’s Simple Storage Service (S3)
[Link]
Wendy Liu Most of Yahoo!’s web services
[Link]
CSC309F – Fall 2007 Most other read-only web services that don’t use SOAP
Static web sites
Many web applications, especially read-only ones like search
engines
1 2
RPC vs. RESTful RESTful Design
An RPC application (e.g. SOAP-based app) is RESTful design constrains the aspects of a resource
exposed as one or more network objects, each that define its interface (the verbs and content
types)
with an often unique set of functions that can
This leads to the definition of fewer types on the
be invoked network than an RPC-based application but more
A client must have knowledge of the object and unrestricted resource identifiers (nouns)
identity to locate it and knowledge of the object RESTful design seeks to
type to communicate with it define a set of resources that clients can interact with
uniformly,
A RESTful web application requires a and to provide hyperlinks between resources that clients
different design approach from an RPC can navigate without requiring knowledge of the whole
(remote procedure call) application resource set
3 4
1
Actions (Verbs) Example: Turn On Light
Controllers cannot have arbitrary verbs, only
these: RPC style:
POST [Link](“ba2185”);
Create - create a new resource
GET
Retrieve - retrieve a representation of a resource
REST style:
PUT
Update - update a resource PUT [Link] HTTP/1.1
DELETE
Delete - delete a resource on
5 6
Success Factor: Uniformity
The scoping information (“why should the
server send this data instead of that data?”) is
kept in the URI REST
This is the principle of uniform addressability
(Representational State Transfer)
The method information (“why should the
server send this data instead of deleting it?”) is
kept in the HTTP method [Link]
dissertation/rest_arch_style.htm
This is the principle of the uniform interface
There are only a few HTTP methods, and
everyone knows ahead of time what they do 2000
7 8
2
What is REST Key Constraints
An architectural style for distributed Client server style
hypermedia systems Separating the user interface concerns from the
data storage concerns
Stateless
Server keeps no session state (only resource state)
Each request from client to server must contain
all of the information necessary to understand
the request
Cannot take advantage of any stored context on
the server
9 10
Stateless: Disadvantages and Benefits Key Constraints (cont’d)
Disadvantages Uniform interface between components
Client needs to manage session state and its path
through the resources Resource is the unit of identification
Client needs to re-send certain data with each request, Resource is manipulated through exchange of
thus decreasing network performance
representations
Benefits
Scalability Resource-generic interaction semantics
server can deal with every client in the same way Self-descriptive messaging
Cacheability
GET and PUT are idempotent
all methods have no “side effects”
Longevity
meaning of actions never evolves
11 12
3
Lecture 16c: Web Services
GData
for A3
Wendy Liu Built on Atom and RSS Syndication
CSC309F – Fall 2007
1 2
What is a Feed Web Syndication
A web feed is a data format used for providing Syndication makes available a feed for an
users with frequently updated content information source
Designed to be machine-readable
Making a collection of web feeds accessible in one Web syndication makes available a section of
spot is known as aggregation, which is performed a website for other sites or individual
by a feed aggregator subscribers
Receiver end
Recentlyadded content
Pulling data from servers
Weblog entries
How it relates to REST
A feed is a collection of resources News headlines
Feed helps to discover resources
3 4
1
Atom GData
Atom is designed to be a universal publishing A GData feed conforms to either the Atom or RSS
standard for personal content and weblogs syndication formats
Atom is the default underlying data format for Google
The Atom Syndication Format is an XML web services
language used for web feeds To acquire information from a service that supports
GData, you send an HTTP GET request; the
service returns results as an Atom or RSS feed
You can update data (where supported by a
particular GData service) by sending an HTTP PUT
request
[Link]
5 6
Google Calendar
Feeds are not essential for calendar service
Google provides it as a platform for all its web services
Google calendar gadget
Calendar feeds
metafeed
allcalendars
owncalendars
event
Requesting a representation
user ID (or default)
visibility (private, public)
projection (full, full-noattendees, composite, attendees-only, free-busy,
basic)
[Link]
7