220 likes | 349 Views
Model-View-Controller. CS 4720 – Web & Mobile Systems. Abstractions and Tiers. Every system we seem to build we talk about tiers How do we reason about the architecture of a system? How can we communicate how the pieces fit together?. A Two-Tier System. A Two-Tier System?.
E N D
Model-View-Controller CS 4720 – Web & Mobile Systems
Abstractions and Tiers • Every system we seem to build we talk about tiers • How do we reason about the architecture of a system? • How can we communicate how the pieces fit together? 2
Describing the World • We generally refer to client/server systems as “two-tier systems” • Why? • What does that communicate? 5
A Three-Tier System • How might a three-tier system be different than a two tier? • Do we add a server? • Do we add a client? • What does it mean to havethree tiers? 6
Context Clues • It’s all in what you’re talking about. • If you’re discussion theoverall system, includingthe client, a common view of a three-tier architecture might be this: 7
Context Switch • What if we are talking about just inside our application? • We assume that the client is there, but it is somewhat immaterial • How do we define the layers now? 8
Three-Tier Architecture • We will see this architecture pattern over and over again in this class • Web development • Web services • Android (basically all mobile…) • Here we are discussing it in the context of a RESTful web architecture 10
Principles of REST • Remember this one? • 3. Addressable Resources • Everything has its place and every place has a thing • “Everything is a resource” • This is actually key to how we go about building a three-tier web application 11
RESTful Tiers • If every resource is a place, and every place is a resource, how do we define our resources? • We know the URL would look something like: • http://mysite.com/users/mss2x • Where: • mysite.com is the domain • users is the “category of resources” • mss2x is a specific instance of the resource 12
RESTful Tiers • What we want the web server / web application to do is translate this: • http://mysite.com/users/mss2x • Into effectively a function call that • Identifies what the user is asking for • Loads a particular resource • Displays the pertinent info about that resource back to the user 13
Model-View-Controller • This is the definition of what MVC is • The MVC pattern maps: • Identifies what the user is asking for • Loads a particular resource • Displays the pertinent info about that resource back to the user • To Model, Controller, View (in that order) 14
MVC 15
MVC 16
Controller • The role of the controller is basically traffic cop • It takes the request from the user and (with the assistance of the server and routing rules) turns it into a method call of sorts • It finds the appropriate model to load • It finds the appropriate view to load • It returns the whole thing back to the user 17
Model • The model is the representation of the data • This may or may not be directly linked to a database (but often is in larger apps) • A model is often translated directly into a DB table, with the columns as its attributes • Think “class definition w/ DB backend” • Often contains relationship rules (a Student has many Classes, for instance) 18
View • The closest thing to what you’ve been dealing with so far is the view • It’s effectively an HTML template that will be populated with the appropriate data from the loaded model • It often has PHP (or whatever) embedded in it • All UI components go here 19
Putting it all Together • So, if you were building a blog, what might some of the models be? • What are the resources that should have addresses to them? • How do they relate to each other? 20
REST and MVC • REST and MVC aren’t directly related, but they intersect in many, many ways • The idea of “nouns” in the system – of addressable resources – is a major component of both • In general, a good MVC system will be RESTful, although a RESTful system does not have to follow MVC. 21
Example Code • What do some of these things look like? 22