Design Patterns in the Android Framework
Transcript of Design Patterns in the Android Framework
-
8/10/2019 Design Patterns in the Android Framework
1/55
Design Patterns in the Android
Framework
Prof. Sheng-De Wang
1
-
8/10/2019 Design Patterns in the Android Framework
2/55
What are Design Patterns? In software engineering, a design patternis a
general reusable solution to a commonly
occurring problem in software design.
is not a finished design
is a description or template for how to solve aproblem that can be used in many different
situations.
Obect-orienteddesign patterns typically showrelationships and interactions between
classes or obects.
Design patterns are widely used in the process
of designing component-based systems 2
-
8/10/2019 Design Patterns in the Android Framework
3/55
Definitions of Software Engineering
!he application of engineering to software
"ield of computer science dealing withsoftware systems
large and comple#
built by teams
e#ist in many versions
last many years undergo changes
3
$ore definitions %pplication of a systematic,
disciplined, &uantifiable
approach to the
development, operation, and
maintenance of software
'I((( )**+ $ulti-person construction
of multi-version software
'Parnas )*
-
8/10/2019 Design Patterns in the Android Framework
4/55
Software Deelopment !ife "ycle
4
-
8/10/2019 Design Patterns in the Android Framework
5/55
#b$ect-#riented !ife "ycle
$ore time is spent in analysis and design,
less time in implementation, testing and
maintenance.
/ife cycle phases intermi#
5
-
8/10/2019 Design Patterns in the Android Framework
6/55
#b$ect-oriented %##& design
!he system is viewed as a collection of
interacting obects.
!he system state is decentrali0edand each
obect manages its own state.
Obects may be instances of an obect class
and communicate by e#changing messages.
6
-
8/10/2019 Design Patterns in the Android Framework
7/55
## Design "oncepts
Design classes
(ntity classes
1oundary classes
2ontroller classes
Inheritance3all responsibilities of a superclass is immediately
inherited by all subclasses
$essages3stimulate some behavior to occur in the receiving
obect
Polymorphism3a characteristic that greatly reduces the effort
re&uired to e#tend the design
7
-
8/10/2019 Design Patterns in the Android Framework
8/55
Android Framework
%ndroid framewor4 is based on obect-orienteddesign
Part of %ndroid Operating Systems
8
AndroidOperating
Systems
What is a
framework?
-
8/10/2019 Design Patterns in the Android Framework
9/55
An important component
Web5it
%n open source web page layout engine
6sed in %pple7s Safaribrowser, 8oogle7s 2hrome
1rowser, 9
2oded in 2::
Ported to ;o4ia Symbian OS, iPhone OS, %ndroid
OS, Palm7s WebOS, 9
9
-
8/10/2019 Design Patterns in the Android Framework
10/55
First android application
e'ample
Inheritance enables software reuse.
10
-
8/10/2019 Design Patterns in the Android Framework
11/55
pac4age com.android.webviewselloWeb=iew2lient'ow they distribute responsibility
20
-
8/10/2019 Design Patterns in the Android Framework
21/55
Design Pattern Space
Purpose
"reational Structural (ehaioral
Scope "lass Factory 4ethod Adapter %class& 3nterpreter2emplate 4ethod
#b$ect Abstract Factory(uilderPrototypeSingleton
Adapter %ob$ect&(ridgeCompositeDecoratorFacade
FlyweightProxy
"hain of1esponsibility"ommand3terator4ediator
4ementoObserverStateStrategyVisitor
Defer ob$ect creation to
another class
Defer ob$ect creation to
another ob$ect
Describe algorithms and
flow control
Describe ways to
assemble ob$ects
21
-
8/10/2019 Design Patterns in the Android Framework
22/55
Factory 4ethod Pattern!he "actory method lets a class defer
instantiation to subclasses.
22
-
8/10/2019 Design Patterns in the Android Framework
23/55
View
Activity
GraphicView
OnCreate()
yDraw
OnCreate()
!!!
View" #actoryetho$()
!!!
ret%rn new GraphicView
Android E'ample of Factory
4ethod Pattern
A d id E l f F
-
8/10/2019 Design Patterns in the Android Framework
24/55
Android E'ample of Factory
4ethod Pattern- code
24
public class MyDraw extends Activity { @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphicView(this));
}
}
public class GraphicView extends View{ private Paint paint= new Paint();
GraphicView(Context ctx) {
super(ctx);
}
@Override protected void onDraw(Canvas canvas) {
int line_x = 10;
int line_y = 50; canvas.drawColor(Color.WHITE);
paint.setColor(Color.GRAY);
paint.setStrokeWidth(3);
canvas.drawLine(line_x, line_y, line_x+120, line_y, paint);
}
}
-
8/10/2019 Design Patterns in the Android Framework
25/55
"omposite Pattern2omposite allows a group of obects to be treated in the
same way as a single instance of an obect.
25
-
8/10/2019 Design Patterns in the Android Framework
26/55
Android 5iew and 5iew6roup
26
"
-
8/10/2019 Design Patterns in the Android Framework
27/55
4ore Android "omposite Pattern
E'ample
27
-
8/10/2019 Design Patterns in the Android Framework
28/55
#bserer Pattern
28
A subset of the asynchronous publish/subscribe pattern
-
8/10/2019 Design Patterns in the Android Framework
29/55
#bserer Pattern E'ample
Subect
Observers
29
-
8/10/2019 Design Patterns in the Android Framework
30/55
4ore #bserer Pattern E'ample
30
-
8/10/2019 Design Patterns in the Android Framework
31/55
E'ample+ Stock 7uote Serice
Ceal time
$ar4et Data
"eed
Stoc4 Muotes
2ustomer
2ustomer
2ustomer 2ustomer
2ustomer
Observers
31
-
8/10/2019 Design Patterns in the Android Framework
32/55
4odel-5iew-"ontroller Pattern
%pplication of Observer Pattern
1enefits Design reuse, /oose 2oupling
32
!he solid line representsa direct association, the
dashedan indirect
association via an
observer 'for e#ample.
-
8/10/2019 Design Patterns in the Android Framework
33/55
45" Pattern in Android
2ursorG model
/ist=iewG view
%ctivityG control
33
SimpleCursorAdapter
ListAdapter
2ursor !e#t=iew /ist=iew
control
-
8/10/2019 Design Patterns in the Android Framework
34/55
4odel
34
-
8/10/2019 Design Patterns in the Android Framework
35/55
5iew
35
-
8/10/2019 Design Patterns in the Android Framework
36/55
36
-
8/10/2019 Design Patterns in the Android Framework
37/55
"ontrol
37
4 45" l
-
8/10/2019 Design Patterns in the Android Framework
38/55
4ore 45" e'ample@@ 8et a Spinner and bind it to an %rray%dapter that
@@ references a String array.
Spinner s) 'Spinner find=iew1yId'C.id.spinner)