You are on page 1of 15

Programming with Mobile

Applications
Chapter 3: Best Practices for
Small Device Programming

In Chapter 3, You Learn to


Describe best practices for
smartphone development
Explain object-oriented programming
techniques, including encapsulation,
inheritance, and polymorphism
Use design patterns
Optimize your code for smartphones

Programming with Mobile Applications

Object Oriented Programming


(OOP)
Data-centric
Classes the fundamental OOP
construct
Classes are templates for objects
OOP fundamentals
Encapsulation
Inheritance
Polymorphism
Programming with Mobile Applications

Encapsulation

Bundling data and behavior


Protection from the outside
Private data, public methods
Accessor methods
Getters
Setters
Properties (C#)

All data in Objective-C is public


Programming with Mobile Applications

Inheritance
Automatic access to information in
superclass
Allows you to define how your class
differs from its superclass
Subclassing is the process of inheriting
from a superclass
C#, Java, and Objective-C all have a
single-inheritance model
Dynamic method invocation
Programming with Mobile Applications

Inheritance (continued)

Figure 3-2 An inheritance tree for the Vehicle class

Programming with Mobile Applications

Polymorphism
The ability to substitute a subclass
for a superclass
Interfaces, protocols
define behavior of objects
provide for multiple inheritance

Programming with Mobile Applications

Design Patterns
Best practices for structuring an
application
Model-View-Controller (MVC)
Delegation

Programming with Mobile Applications

MVC
Separation of programming concerns
into separate tiers
Model the data tier
View the interface presented to the
user
Controller brokers requests
between the model and the view
Figure 3-4 The Model-View-Controller model
Programming with Mobile Applications

Delegate Design Pattern


One object hands over some of its
work to another object called the
delegate
Delegates typically pass information
back to the more complex object
using a reference
Avoid subclassing

Programming with Mobile Applications

10

Optimization
Making programs run faster or on
less memory
Smartphones = constrained
resources

Programming with Mobile Applications

11

Strings, Buffers, and


Operations
Strings are immutable
C#, Java, and Objective-C include
mutable String objects commonly
called buffers
When string contents need to be
changed, use a buffer instead

Programming with Mobile Applications

12

Loops and Conditional


Statements
Choose the correct loop
While
For
Dowhile

Avoid calculation in loop termination


rules
Keep loop invariants outside of loops
Order conditional statements carefully to
avoid making unnecessary calculation
Programming with Mobile Applications

13

Memory Management
C# and Java include automatic garbage
collection
Reference Counting
Memory Leaks
Objective-C Memory Management Rules
If the new, alloc, or copy methods are used to
create an object, youre responsible for releasing it.
If another mechanism is used to get a reference to
an object, assume it has been auto-released.
If you retain an object, you must eventually release
it.
Programming with Mobile Applications

14

Memory Management
(continued)
Create objects only when needed.
Create the fewest number of objects
possible.
Close everything you open.
Be careful with collections of objects.
Restrict the scope of objects when
possible.
Programming with Mobile Applications

15

You might also like