ANDROID AND MODEL / VIEW / CONTROLLER
Slide 2 Design Patters Common solutions to programming problems are called design patterns Design patterns are characterized as: Structural patterns that depict the bigger picture of how classes (things) fit together Adapter, MVC Creational patterns depict how objects are created Factory pattern Behavioral patterns describe the interaction between objects
Slide 3 MVC (Introduction) Design pattern with a clear separation between application logic and the user interface In Android, all objects fall into either a model view controller
Slide 4 MVC (Model - 1) The model manages the information and notifies the observers when the information changes It represents the data on which the application operates The model provides the persistent storage of data, which is manipulated by the controller In this chapter, we are modeling a true / false question
Slide 5 MVC (Model - 2) Model has no knowledge of the user interface That’s the responsibility of the view In Android, these are custom classes that you typically create
Slide 6 MVC (View) The view displays the data It takes input from user It renders the model data into a form (screen) to display to the user There can be several views associated with a single model Usually for different devices If it’s visible to the user, it’s a view
Slide 7 MVC (View) The view knows how to draw itself on the screen Together, all of the different view objects make up the view layer Rendering a view is often called inflating a view
Slide 8 MVC (Controller 1) The controller handles all requests coming from the view or (user interface) The data flow to whole application is managed by the controller It forwards the request to the appropriate handler Only the controller is responsible for accessing the model and rendering it into various UIs (views)
Slide 9 MVC (Controller 2) The controller ties the model and views together Controllers are designed to respond to various events triggered by view objects and to manage the flow of data to and from model objects and the view layer
Slide 10 MVC (Illustration)
Slide 11 Chapter 2 MVC Implementation
Slide 12 MVC Benefits Separates the business logic from the user interface We can have multiple views for different devices Applications tend to be more structured thereby making them easier to understand Code reusability tends to increase
Slide 13 Adding a Java Class (1) The project in the first chapter had a single class In this chapter, you will add a second class that will implement the model
Slide 14 Adding a Java Class (2) Click File, New, Class to create a new class Give the class a name
Slide 15 Adding a Java Class (3)
Slide 16 Accessors and Mutators In C# we create what are called property procedures to implement properties Remember that properties store data about an object In Java, we create method pairs called accessors and mutators Accessers read a property Mutators update a property
Slide 17 Accessors Both store data in a hidden variable Accessors typically begin with the prefix get Boolean accessors typically begin with the prefix is Typically the value is stored in a hidden variable
Slide 18 Mutators Both store data in a hidden variable Mutators typically begin with the prefix “set”
Slide 19 Example The following contain accessors and mutators
Slide 20 Icons (Introduction) Icons are stored as external files Multiple versions of an icon are typically needed to support different resolutions Medium (mdpi) (about 160 dpi) High (hdpi) (about 240 dpi) Extra-high (xhdpi) (about 320 dpi) Low density devices are considered obsolete
Slide 21 Icons (Implementation) Icons are stored in the res folder By convention, there are subdirectories that store icons of the various resolutions Android figures out which icon (resource) to use based on the device resolution png, jpg, gif formats are supported among others
Slide 22 Icons (Illustration)
Slide 23 Reading Resources in XML The syntax to read a resource is similar to the process to read a string A reference to a string resource begins A reference to a drawable resource begins
Slide 24 Reading a Resource (Example) Read and display the resource named arrow_right