Design Patterns in the Android Framework Prof ShengDe
- Slides: 55
Design Patterns in the Android Framework Prof. Sheng-De Wang 1
What are Design Patterns? • In software engineering, a design pattern is 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 a problem that can be used in many different situations. – Object-oriented design patterns typically show relationships and interactions between classes or objects. – Design patterns are widely used in the process 2 of designing component-based systems
Definitions of Software Engineering • The application of engineering to software • Field of computer science dealing with software systems More definitions – large and complex ü Application of a systematic, – built by teams disciplined, quantifiable – exist in many versions approach to the development, operation, and – last many years maintenance of software – undergo changes (IEEE 1990) ü Multi-person construction of multi-version software (Parnas 1978) 3
Software Development Life Cycle 4
Object-Oriented Life Cycle • More time is spent in analysis and design, less time in implementation, testing and maintenance. • Life cycle phases intermix 5
Object-oriented (OO) design • The system is viewed as a collection of interacting objects. • The system state is decentralized and each object manages its own state. • Objects may be instances of an object class and communicate by exchanging messages. 6
OO Design Concepts • Design classes – Entity classes – Boundary classes – Controller classes • Inheritance—all responsibilities of a superclass is immediately inherited by all subclasses • Messages—stimulate some behavior to occur in the receiving object • Polymorphism—a characteristic that greatly reduces the effort required to extend the design 7
Android Framework • Android framework is based on object-oriented design • Part of Android Operating Systems What is a framework? Android Operating Systems 8
An important component • Web. Kit – An open source web page layout engine – Used in Apple’s Safari browser, Google’s Chrome Browser, … – Coded in C++ – Ported to Nokia Symbian OS, i. Phone OS, Android OS, Palm’s Web. OS, … 9
First android application example Inheritance enables software reuse. 10
An inheritance example package com. android. webviews; import android. app. Activity; import android. os. Bundle; import android. view. Key. Event; import android. webkit. Web. View. Client; public class Hello. Web. View extends Activity { /** Called when the activity is first created. */ Web. View webview; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); webview = (Web. View) find. View. By. Id(R. id. webview); webview. get. Settings(). set. Java. Script. Enabled(true); webview. load. Url("http: //www. google. com"); 11
webview. set. Web. View. Client(new Hello. Web. View. Client()); @Override public boolean on. Key. Down(int key. Code, Key. Event event) { if ((key. Code == Key. Event. KEYCODE_BACK) && webview. can. Go. Back()) { webview. go. Back(); return true; } return super. on. Key. Down(key. Code, event); } private class Hello. Web. View. Client extends Web. View. Client { @Override public boolean should. Override. Url. Loading(Web. View view, String url) { view. load. Url(url); return true; } } 12 }
Software Frameworks • Components-Based Software Engineering means that we build software by "putting pieces together". – Frameworks provide the context in which the pieces can be used. • A framework may be seen as: – A reusable design of a system, – A skeleton of an application which can be customized by an application developer. 13
Component Frameworks • While frameworks in general describe a typical and reusable situation at a model level, a component framework describes a “circuit-board” with empty slots into which components can be inserted to create a working instance. Coordination Services (transactions, persistence. . ) Component Framework 14
Relationships Between Concepts Interface that satisfies contracts Component-type Specific interface Independent deployment Coordination Services (transactions, persistence. . ) Component implementation Component model Component Framework 15
The components-based software engineering aspects of the Android framework Key components (classes): Activity, Service, Broadcast. Receiver, Content. Provider, Intent 16
Activity life cycle • Entire lifetime • Visible lifetime • Foreground lifetime • 3 nested loops • 7 hooks or callbacks 17
Service Life Cycle 18
Frameworks and Patterns • It is important to realize that design patterns and frameworks are distinct concepts of different natures. – Frameworks are of a physical nature, and are executable software used in either the design or the run-time phase. – Design patterns are of a logical nature, representing knowledge of and experience gained with software. 19
Categories of Design Patterns • Creational Pattern: Deal with initializing and configuring of classes and objects. • Structural Patterns: Deal with decoupling interface and implementation of classes and objects – Composition of classes or objects • Behavioral patterns: Deal with dynamic interactions among societies of classes and objects – How they distribute responsibility 20
Design Pattern Space Purpose Defer object creation to another class Scope Creational Structural Behavioral Class Factory Method Adapter (class) Interpreter Template Method Object Abstract Factory Builder Prototype Singleton Adapter (object) Bridge Composite Decorator Facade Flyweight Proxy Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor Defer object creation to another object Describe ways to assemble objects Describe algorithms and flow control 21
Factory Method Pattern The Factory method lets a class defer instantiation to subclasses. 22
Android Example of Factory Method Pattern Activity View Graphic. View On. Create() . . . View= Factory. Method(). . . My. Draw On. Create() return new Graphic. View
Android Example of Factory Method Pattern- code public class My. Draw extends Activity { @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(new Graphic. View(this)); } } public class Graphic. View extends View{ private Paint paint= new Paint(); Graphic. View(Context ctx) { super(ctx); } @Override protected void on. Draw(Canvas canvas) { int line_x = 10; int line_y = 50; canvas. draw. Color(Color. WHITE); paint. set. Color(Color. GRAY); paint. set. Stroke. Width(3); canvas. draw. Line(line_x, line_y, line_x+120, line_y, paint); … } 24
Composite Pattern Composite allows a group of objects to be treated in the same way as a single instance of an object. 25
Android View and View. Group 26
More Android Composite Pattern Example 27
Observer Pattern A subset of the asynchronous publish/subscribe pattern 28
Observer Pattern Example Observers Subject 29
More Observer Pattern Example 30
Example: Stock Quote Service Real time Market Data Feed Stock Quotes Customer Customer Observers 31
Model-View-Controller Pattern • Application of Observer Pattern • Benefits – Design reuse, Loose Coupling The solid line represents a direct association, the dashed an indirect association via an observer (for example). 32
MVC Pattern in Android • Cursor: model • List. View: view • Activity: control Simple. Cursor. Adapter Cursor List. Adapter Text. View List. View 33
Model 34
View 35
36
Control 37
More MVC example // Get a Spinner and bind it to an Array. Adapter that // references a String array. Spinner s 1 = (Spinner) find. View. By. Id(R. id. spinner 1); Array. Adapter adapter = Array. Adapter. create. From. Resource( this, R. array. colors, android. R. layout. simple_spinner_item); adapter. set. Drop. Down. View. Resource(android. R. layout. simple _spinner_dropdown_item); s 1. set. Adapter(adapter); XML R Array. Adapter Spinner 38
// Load a Spinner and bind it to a data query. private static String[] PROJECTION = new String[] { People. _ID, People. NAME }; Spinner s 2 = (Spinner) find. View. By. Id(R. id. spinner 2); Cursor cur = managed. Query(People. CONTENT_URI, PROJECTION, null); Simple. Cursor. Adapter adapter 2 = new Simple. Cursor. Adapter(this, android. R. layout. simple_spinner_item, // Use a template // that displays a // text view cur, // Give the cursor to the list adatper new String[] {People. NAME}, // Map the NAME column in the // people database to. . . new int[] {android. R. id. text 1}); // The "text 1" view defined in // the XML template adapter 2. set. Drop. Down. View. Resource(android. R. layout. simple_spinner_dropdown _item); 39 s 2. set. Adapter(adapter 2);
Simpe. Cursor. Adapter cursor Spinner SQLite 40
Façade Pattern • A facade is an object that provides a simplified interface to a larger body of code, such as a class library. 41
Android Media Framework 42
Binder: IPC Mechanism of Android Media. Player. Base: Interface of some concrete media player such as Vorbis. Player, PVPlayer 43
Playing a Video. View _player=(Video. View) find. View. By. Id(R. id. videoplay); Intent intent=this. get. Intent(); _player. set. Video. Path(intent. get. Data. String()); _player. start(); 44
A video player with simple GUI import android. app. Activity; import android. os. Bundle; import android. view. View. On. Click. Listener; import android. widget. Button; import android. widget. Video. View; public class Video. Player extends Activity { /** Called when the activity is first created. */ @Override public void on. Create(Bundle icicle) { super. on. Create(icicle); set. Content. View(R. layout. main); final Video. View w =(Video. View)find. View. By. Id(R. id. vdoplayer); Button cmdload = (Button)this. find. View. By. Id(R. id. cmd_load); cmdload. set. On. Click. Listener(new On. Click. Listener(){ public void on. Click(View arg 0) { // TODO Auto-generated method stub w. set. Video. Path("/sdcard/android/kongfu. mp 4"); } }); Button cmdplay = (Button)this. find. View. By. Id(R. id. cmd_play); cmdplay. set. On. Click. Listener(new On. Click. Listener(){ public void on. Click(View arg 0) { // TODO Auto-generated method stub w. start(); } }); Button cmdpause = (Button)this. find. View. By. Id(R. id. cmd_pause); cmdpause. set. On. Click. Listener(new On. Click. Listener(){ public void on. Click(View arg 0) { // TODO Auto-generated method stub w. pause(); } }); } //end of on. Create() } //end of Video. Player 45
Some More Design Patterns • Proxy Pattern • Mediator Pattern • Bridge Pattern 46
Proxy Pattern • The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. 47
Android Proxy Pattern 48
Mediator Pattern • With the mediator pattern, communication between objects is encapsulated with a mediator object. 49
Bridge Pattern • “decouple an abstraction from its implementation so that the two can vary independently” 50
Bridge and Mediator Patterns in Android Bridge patterns in linking Java and Cpp Mediator patterns 51
Binder IPC Mechanism 52
Binder IPC Mechanism 53
Implementation of IPC 54
Concluding Remarks • Android Framework – Object-Oriented Design – Making use of Design Patterns – Containing run-time routines • Android Operating Systems – Making use of three well-developed technologies: • Linux Kernel, Java, XML • Design Patterns facilitate – Software reuse – Mainteinance – Flexibility 55
- Design patterns in android
- Android ril architecture
- Android app components
- Resource manager in android framework
- Android game framework
- In traditional dating patterns dating behavior
- X videos
- Dispositional framework vs regulatory framework
- Theoretical framework example
- Conceptual framework maker
- Conceptual framework theoretical framework
- Dispositional framework vs regulatory framework
- Theoretical framework example
- Hát kết hợp bộ gõ cơ thể
- Lp html
- Bổ thể
- Tỉ lệ cơ thể trẻ em
- Gấu đi như thế nào
- Glasgow thang điểm
- Alleluia hat len nguoi oi
- Môn thể thao bắt đầu bằng chữ đua
- Thế nào là hệ số cao nhất
- Các châu lục và đại dương trên thế giới
- Công thức tính độ biến thiên đông lượng
- Trời xanh đây là của chúng ta thể thơ
- Mật thư anh em như thể tay chân
- Làm thế nào để 102-1=99
- Phản ứng thế ankan
- Các châu lục và đại dương trên thế giới
- Thơ thất ngôn tứ tuyệt đường luật
- Quá trình desamine hóa có thể tạo ra
- Một số thể thơ truyền thống
- Cái miệng bé xinh thế chỉ nói điều hay thôi
- Vẽ hình chiếu vuông góc của vật thể sau
- Thế nào là sự mỏi cơ
- đặc điểm cơ thể của người tối cổ
- V. c c
- Vẽ hình chiếu đứng bằng cạnh của vật thể
- Phối cảnh
- Thẻ vin
- đại từ thay thế
- điện thế nghỉ
- Tư thế ngồi viết
- Diễn thế sinh thái là
- Dot
- Các số nguyên tố là gì
- Tư thế ngồi viết
- Lời thề hippocrates
- Thiếu nhi thế giới liên hoan
- ưu thế lai là gì
- Hổ đẻ mỗi lứa mấy con
- Khi nào hổ con có thể sống độc lập
- Sơ đồ cơ thể người
- Từ ngữ thể hiện lòng nhân hậu
- Thế nào là mạng điện lắp đặt kiểu nổi
- Line design patterns