The main content of this article comes from a PowerPoint document I spoke about earlier, and now it is organized as follows. Please correct me. The following is from my own experience, you are welcome to mention their suggestions. 1. Some concepts
definition of the pattern:
Each pattern describes a problem that continues to arise 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 design mode?
A design pattern is a typical, general-purpose solution to a problem in a particular situation.
We need to properly understand and learn some design patterns, in the program development process, will always involve some framework design, module design and other things, if you can understand and run the design pattern, you design the module or framework will be much more stable, because these design patterns are common solutions, is a practical experience.
For example, in the program, there may be notification module, a module of the data changes, B module needs to be notified, for such a need, you may think of "broadcast", "message" or "callback" way to solve, indeed, just now I said that the three can also be resolved, but these three are there are some shortcomings, For example, broadcasting, using intent to transfer data is very difficult, for "message", not very good tracking, for "callback", it is possible that you a and B modules can not access each other at all. 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 problems, my main meaning is that, to use the model properly, we can not use the pattern to use patterns, we have to use the pattern to solve our actual problems.
Conceptual Integrity
On the concept of integrity, in the "People of the Moon myth" a lot of elaboration, here, I write my understanding to share with you.
1) Conceptual integrity is one of the most important considerations in system design. The greater the size of your system, the more obvious it is.
2) in order to obtain the completeness of the concept, the design must be done by a single person or a small team with a consensus. This is very well understood, about the design, can let all the people involved, but the decision right in the hands of a few, if everyone wants to participate in the design, there is no way to ensure that the system design is unified and complete.
3) in order to achieve conceptual integrity, one must control these concepts, similar to the autocratic rule of the aristocracy. Here, the project manager or architect in the team must have absolute authority over the project, otherwise, the project will not be unified command.
4) Conceptual integrity is represented by:
-Consistency of requirements, design, and coding during the development process
-The entire program has a unified style, such as dialog box 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, the asynchronous access to callback method to notify the results, the same function should be extracted into common modules.
-developers are well able to implement the intent of the people and designers who need them.
-Complete documentation, requirements documentation, design documentation, test documentation, process documentation, etc.
How to maintain conceptual integrity
-To guarantee the system, the person responsible for the product must establish the absolute technical Authority
-Technical staff (SE,SL) must strictly implement the project requirements, design, must go deep into the coding details
-Maintain continuous communication with all personnel at different stages, and encourage developers to make suggestions.
-Let the developer participate in the design, but do not decide the design
-Implement module reuse through continuous feedback and communication 2. What to do before the design
2.1 Design of common class
2.1.1 Widget design
- TextView
- EditText
- Button
- Title Bar
- Tool Bar
- ...
Why do you provide these common controls?
- Unified font size, such as app fonts do not change with system fonts
- Unified UI style, such as button, edittext with the same background, etc.
- Reuse Code
2.1.2 Adapter Items
- According to the specifications, extract the item that needs to be displayed in the Adapterview
- A simple composite layout
- Self-drawing, which improves sliding performance
-Improves up and down sliding performance when gallery in the ListView
-Optimize drawing as much as possible
Data-driven
- Adapter items provides the core approach
-SetData (Object data)
-GetData ();
- Adapter#getview implementation Easier
-Simple to implement
-will not change because of UI changes
The following code example shows the implementation of the Adapter#getview () method, which returns the Bookview,bookview provides a way to receive data, and as for the display of the Bookview, it is displayed according to the data set, which is the data-driven UI.
@Override Public View GetView (int position, view Convertview, ViewGroup parent) { if (null = = Convertview) { new Bookview (GetContext ()); Convertview.setlayoutparameter (new abslistview.layoutparameter); } = M_booklist.get (position); = (Bookview) Convertview; Bookview.setbook (book); return convertview; }
2.1.3 Dialog
- extended to the dialog class
- Provides dialog shutdown events
- The height of the dialog changes with the content
- You can set the button text, visibility, font and other methods
- Set the listener of a button click event
- To consider the three properties of a dialog box: Title, Content area, Action buttons
2.1.4 Utility
- Log
- DateFormat
- Bitmap
- Notification
- Shared Preference
- Environment
- Device
- ...
2.2 Task Management
The thread is just a mechanism to ensure that the task we are going to accomplish is not running on the UI thread (that is, not blocking the UI), and that the task is the core of our focus, so that we can design, encapsulate the thread, and let the user feel no thread at all, he only cares about what he wants to do.
Here, we can design a framework of "asynchronous chained calls" that encapsulate threads. This is only necessary for use:
New TaskManager (). Next (Task1). Next (Task2). Next ( task3) . Execute ();
Here, Task1, Task2, Task3 are executed sequentially, for example: we want to access the network, get a picture, use this taskmanager we need 3 task,
Task1: Displays a progressdialog.
TASK2: Access the network and create the bitmap.
TASK3: Closes the dialog box, showing bitmap.
This point, you can refer to the Corelib project in the Task.taskmanager class.
For TaskManager, the following points need to be noted:
- Encapsulates a thread
- Let callers focus only on their own business processes
- Ensure that a task is executed sequentially and chained
- Output from the previous task, as input to the next task
- Ability to pause and resume any task
2.3 Cache Design
- Storing large memory-intensive objects in the cache, such as bitmap
- Using the LRUCache class to achieve
- Using the Asynctask class to load the bitmap
- No more manual release of bitmap memory, the operation is risky
- No more concern about the scroll state of Abslistview.
For more details on caching, please refer to [please refer to the cache package in the Corelib project].
In doing so, there is no need to manually release bitmap intrinsic, the operation is risky because the bitmap has a view reference, and if a view is trying to draw a bitmap that has been recycled, it throws an exception.
2.4 Thread Management
threads with no message loops:
New Thread (nullnew Runnable () { publicvoid run () { / / Do you works. } "Thread_name_xxx"). Start ();
Under what circumstances this thread is used:
- Finish one thing to the end, this event occurs at a low frequency, such as reading the 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 take advantage of height and tracking.
a thread that has a message loop:
Such a thread has a message loop that is suspended when there is no message in the message queue. When we have to do something, we just need to send a message to it.
This is usually done in order to reuse threads and not to create threads frequently, such as a music player program, to start a thread that has a message loop to get album pictures of music.
We also typically create a handler that is associated with the message loop (Looper) of this thread, which handles the message, noting that the thing that is done is running in the background thread. 3, how the program framework is designed
structure of the Android program
- UI Layer
- Data presentation and Management
- User interaction
- Draw
- Adapter
- Business Logic Layer
- Persisted data (in-memory, equivalent to global data)
- Data-plus (data-tier data sometimes needs to be processed into the data required by the UI layer)
- Notification mechanism for data changes
- Data layer
- Data access (DB, file, network, etc.)
- Cache (Pictures, files, etc.)
- Configuration file (Shared perference)
Below, I try to draw the structure of an Android program, if there is a bad place, please correct me.
4, some basic principles
Here are some general principles that we should follow during the development process, welcome additions and corrections.
4.1 Initialize () method available
Called in the Activity.oncreate () or view construction method, when you look at the code in the future, people will usually first look for a method like Initialize ().
4.2 Package Click events
Put the view of the Click event, the Commission method, so that at listener is just a method caller, the general event package is: Onxxxclick (View v).
4.3 Designing a baseactivity class
Let all the activity inherit from the Baseactivity class, so that we can do a lot of useful things
- Defining Common Properties
- Show common dialog box (Progress Dialog)
- Get top activity
- You can manually manage the activated activity
4.4 Design Application class
- Save global data, application context than top activity.
4.5 Exception Handling
- Report function is the essence of handling exceptions
- To perform a cleanup operation in a finally block
- Don't use try-catch-finally to judge business logic
- Consider designing a custom exception class
4.6 Use of annotations
- The method of rewriting must be added @override
- Unused methods, not deletions, 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 the bitmap operations, such as loading from files, saving, downloading the network, and dynamically computing sample size. With encapsulation, we can optimize it centrally.
4.9 Drawing processing
Be sure to pay attention to what you are drawing, and do not create new objects in OnDraw ()/ontouchevent ().