Sample Android App

chemistoddΤεχνίτη Νοημοσύνη και Ρομποτική

6 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

81 εμφανίσεις

Sample Android App

Chien
-
Chung
Shen

CIS, UD

cshen@cis.udel.edu

Java Interface


In
software
engineering, it
is important
for disparate groups of programmers to
agree to a "
contract
" that spells out
how their software
interacts


Each group should be able to write their
code
without

any knowledge of
how

the
other group's code is
written


Such a contract is termed
interface

in Java

Example of Driverless Car


Automobile
manufacturers write
software
that
operates the
car


Electronic
guidance instrument
manufacturers
make computer
system
that
uses
GPS
and traffic info to
drive
the
car


Neither
group
needs to know
how

the
other group's software is implemented
as long as
they adhere
to the published
interface

An Example Java Interface


public
interface

OperateCar

{



/
/ constant declarations, if any



/
/
method

signatures



int

turn(Direction direction, double radius, double
startSpeed
, double
endSpeed
);



int

changeLanes
(Direction direction, double
startSpeed
,
double
endSpeed
);



int

signalTurn
(Direction direction,
boolean

signalOn
);



int

getRadarFront
(double
distanceToCar
, double
speedOfCar
);



int

getRadarRear
(double
distanceToCar
, double
speedOfCar
);



}


Write
a class that
implements

the
interface


public class OperateBMW760i
implements

OperateCar

{


/
/ the
OperateCar

method signatures,
with
implementation




int

signalTurn
(Direction direction,
boolean

signalOn
) {



/
/ code to turn BMW's LEFT turn indicator lights on



/
/ code to turn BMW's LEFT turn indicator lights off



.......


}




/
/ other members, as needed
--

for example, helper classes not



/
/ visible to clients of the interface



}

Interfaces as
APIs


Interface is
used as an
Application
Programming
Interface

(
API
)


One company
sells a software package that contains
complex methods that another company wants to use
in its own software
product


e.g.


One company sells
a package of digital image processing
methods
to
companies making end
-
user graphics
programs


The
image processing company writes its classes to
implement an
interface
, which it makes public


The
graphics company then invokes the image processing
methods using the signatures and return types defined in the
interface


While
API is made public (to its customers), its
implementation
is kept as a closely guarded secret

UI Events


Android provides
listeners

for events, such as
click, touch,
etc.


Interface

TextWatcher



watch for text
changes in
edit text
field


Interface

OnClickListener



handle click
event


Implement

TextWatcher

and
attach

it to the
edit text
field where user is typing the text


Methods of
TextWatcher

will be invoked
(used) as user changes the text