Android Program Framework Design

Source: Internet
Author: User
Keywords We can so very
1. Definitions of some conceptual models:

Each pattern describes a problem that continues to occur in our environment, and then describes the core of the solution to the problem. In this way, you can use the existing solutions countless times without having to repeat the same work.

What is a design pattern?

A design pattern is a typical, generic solution to a problem in a particular situation.

We need to properly understand and learn some design patterns, in the process of program development, there will always be some framework design, module design, and so on, if you can understand and run the design pattern, you will design a more stable module or framework, because these design patterns are common solutions, is a practical experience.

For example, in a program, there may be notification module, a module of data changes, B module needs to be notified, for such a need, you may think of the "broadcast", "message" or "callback" way to solve, indeed, I said that the three kinds of can also be solved, but, these three kinds of is there are some shortcomings, For example, broadcast, with intent to transmit data is very difficult, for "message", not very good tracking, for "callback", it is possible that you a and B module can not access each other. At this point, if you use the Observer pattern problem, this problem can be easily solved.

Of course, here is the need for concrete analysis of specific issues, my main meaning is that the appropriate use of patterns, we can not use the model to use the model, we are to use the model to solve our practical problems.

Conceptual Integrity

On the concept of integrity, in the "Human Moon Myth" a book in a large number of elaboration, here, I write out my understanding, and share with you.

1 conceptual integrity is the most important factor to consider in system design. The bigger the system, the more obvious it is.

2 in order to obtain the integrity of the concept, the design must be done by a small team of people or consensus. This is well understood, about design, can let all people participate, but the decision in the hands of a few, if everyone wants to participate in the design, it is simply no way to protect the system design is unified integrity.

3 to obtain conceptual integrity, someone must control these concepts, similar to the autocratic rule of nobility. Here, the project manager or architect in the team must have absolute authority over the project, otherwise the project cannot unify the command.

4 The Concept integrity performance:

12345-During the development process, requirements, design, coding consistency-the entire program has a unified style, such as dialog style, button style, hue and other UI elements-the entire program specific unified structure, such as different modules to access the network, they call the same way, for example, asynchronous access to the callback method to notify the results, The same function should be extracted into common modules. -developers are well able to execute requirements and design staff intentions. -have complete documentation, requirements documentation, design documents, test documents, process documentation, etc. How to maintain conceptual integrity 12345-institutional assurances, the person responsible for the product must establish the absolute authority of the technology-the technical Officer (SE,SL) must strictly implement the project requirements, design, and must go deep into the coding details-at different stages, maintain continuous communication with all personnel and encourage developers to make comments. -Get developers involved in the design, but not the design-through continuous feedback and communication to achieve module reuse 2, what should be done before the design of 2.1 Common class Design 2.1.1 Widget design

TextView
EditText
Button
Title Bar
Tool Bar
...
Why do ### provide these common controls? ###

Uniform font size, such as app fonts that don't change with system fonts
Unified UI style such as button, edittext with same background, etc.
Reusing code

2.1.2 Adapter Items

Extract the item that needs to be displayed in the Adapterview according to the style
Simple composite layout
Self-drawing to improve sliding performance
-Increase and lower sliding performance when gallery in ListView
-Optimize the drawing as far as possible
### Data Driven ###

Adapter items provide the core approach
-SetData (Object data)
-GetData ();
Adapter#getview implementation is simpler
-Simple to implement
-No changes due to UI changes
The following code examples the implementation of the Adapter#getview () method, which returns the Bookview,bookview method to receive the data, and the display of Bookview, based on the set data, is the data-driven UI.

1 2 3 4 5 6 7 8 91011121314@overridepublic view GetView (int position, view Convertview, ViewGroup parent) {if (null = = Convertview) {Convertview = new Bookview (GetContext ()); Convertview.setlayoutparameter (new Abslistview.layoutparameter (150, 150)); Book book = M_booklist.get (position); Bookview Bookview = (bookview) Convertview; Bookview.setbook (book); return convertview; 2.1.3 Dialog

Extended to Dialog class
Provides dialog shutdown events
The height of the dialog changes with the content
You can set the text, visibility, font, and so on for the button
Set Button click event Listener
To consider the three properties of the dialog box: Title, Content area, Action buttons

2.1.4 Utility

-log
DateFormat
Bitmap
Notification
Shared Preference
Environnement
Device
...

2.2 Task Management

Threads are just a mechanism to ensure that the tasks we want to accomplish do not run on the UI thread (that is, not blocking the UI). The task is the core of our attention, so we can design, the thread encapsulation, so that users do not feel a thread, he only care about what he wants to do.
Here, we can design an "asynchronous chained call" framework that encapsulates threads. All you need to do is use this:

12345new TaskManager (). Next (Task1). Next (Task2). Next (TASK3). Execute ();

Here, Task1, Task2, task3 are sequential, for example: we want to access the network, get a picture, use this taskmanager we need 3 tasks,

Task1: Displays a progressdialog.

TASK2: Access network, create bitmap.

TASK3: Closes the dialog box and displays the bitmap.

This can refer to the Task.taskmanager class in Corelib engineering.

For TaskManager, there are several points to note:

-Encapsulates threads
Let callers focus on their own business processes
Ensure sequential chained execution of a task
The output of the previous task as input to the next task
Can suspend or resume any task

2.3 Cache Design

-Store objects with large memory footprint in the cache, such as bitmap
Using the LRUCache class to implement
Using the Asynctask class to load the bitmap
No more manual release of bitmap memory, this operation is risky
No more caring about the scroll state of Abslistview.
For more details on caching, please refer to the cache package in the Corelib project.

What is the benefit of doing this without manually releasing bitmap inside, the operation is risky because the bitmap has a view reference, and if a view is trying to draw a reclaimed bitmap, this throws an exception.

2.4 Thread-managed threads without message loops: 123456new thread (NULL, new Runnable () {public void run () {//Does/works.}}, "Thread_name_xxx"). Start ();

When to use this thread:

-When you finish one thing, it doesn't happen very often, like reading image data from SD card
No need to reuse threads

In the use of threads, it is best to add a name to the thread, so use the height and tracking.

a thread with a message loop:

Such a thread has a message loop, and the thread is suspended when there is no message in the message queue. When we're going to do something, we just need to send it a message.

This is usually used to reuse threads, not to create threads frequently, such as music player programs, to start a thread with a message loop to get album pictures of music.

We also typically create a handler associated with this thread's message loop (Looper), which handles the message, noting that the thing to do is run in the background thread.

3, how the program framework designs the structure of the Android program

UI Layer
Data display and management
User interaction
Draw
Adapter
Business Logic Layer
Persisted data (in memory, equivalent to global data)
Data additive (data-tier data sometimes needs to be processed into the data required by the UI layer)
Notification mechanism for data changes
Data tier
Data access (DB, files, network, etc.)
Caching (Pictures, files, etc.)
Configuration file (Shared perference)

Below, I try to draw an Android program structure, if there is a bad place, welcome correction.

4, some basic principles

Here are some common principles that we should follow in the development process, welcome to add and correct.

4.1 provides initialize () method

Called in the Activity.oncreate () or the view's construction method, when looking at the code later, people usually look for the Initialize () method first.

4.2 Package Click event

The view of the Click event, the Commission method, so at the listener is just a method caller, the general event package is: Onxxxclick (View v).

4.3 design a baseactivity class

Let all the activity inherit from the Baseactivity class, so that we can do a lot of useful things

-Define common attributes
Show common dialog box (Progress Dialog)
Get top activity
Can manually manage initiated activity

4.4 Design Creator class

Save global data, creator context.

4.5 Exception Handling

-reporting function is the essence of handling exceptions
Performing cleanup operations in a finally block
Don't use try-catch-finally to judge business logic
Consider designing custom exception classes

4.6 Labeling using

-Rewrite the method must add @override
Methods that are not used, do not delete, can be marked as @deprecated, which is particularly useful in maintenance-oriented projects.

4.7 Registration and anti-registration

-Local broadcast
Various listener
Service, etc.

4.8 Package Bitmap Operation

We should encapsulate bitmap operations, such as loading, saving, downloading from files, dynamically computing sample size, etc. With encapsulation, we can optimize it centrally.

4.9 Rendering processing

Be sure to be aware of the drawing aspect, and do not create new objects in OnDraw ()/ontouchevent ().

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.