Model-View-Controller
Department of IT Engineering
Lecturer: Kor Sokchea
Java Programming
Model-View-Controller
Model
Updates
Manipulates
View Controller
Sees uses
User
Model-View-Controller
Android applications are designed around an architecture called Model-View-Controller
(MVC)
Model
Controller
View
Model Object
A model object holds application’s data and “business logic”
Model class are typically designed to model the things your app is concerned with:
• A user
• A Product in a store
• A photo on a sever
Model objects have no knowledge of the user interface
The main purpose is holding and managing data
In Android applications, model classes are generally custom classes you create
All of the model objects in your application compose its model layer
View Object
View objects know:
How to draw themselves on the screen
How to respond to user input like touches
A simple rule of thumb is that if you can see it on screen, then it is a view
Android provides a wealth of configurable view classes
You can also create custom view classes
An application’s view objects make up its view layer
In android, View layer consists of the widgets that are inflated from activity_main.xml
Controller Object
Controller objects tie the view and model objects together
Controller objects contain “application logic”
Controllers are designed:
• To respond to various events triggered by view objects
• To manage the flow of data to and from model objects and the view layer
In Android, a controller is typically a subclass of Activity, Fragment, or Service
MVC Flow with User Input
MVC Example
Counter
Model _mCount
mCounterObj
Controller
CounterActivity
_mCurrentCount;
View (layout)
countTextView countButton
TextView Button