Brief analysis on the callback thought in the design of Android system

Source: Internet
Author: User
Tags call back

Why write this article

?? As I slowly dive into Android development, I'm increasingly finding that there's a lot of callback ideas in Android, at least in application development. For example, the life cycle of activity, the life cycle of fragment is the callback function implementation, the event processing mechanism in Android is a callback, the thread of the asynchronous task, loader, Hanlder, etc. are based on callback, and so on.

?? One of the fundamental ideas of Android is the callback, which requires you to participate, call back the relevant callback method, and you populate it.

?? It can be said that the callback of the idea of infiltration and Android design aspects, Android development almost with the method is a callback method. As a novice who is committed to deep learning about mobile development, it is necessary to understand the callback in depth.

Why to introduce callbacks

?? Callback is not a design strategy, imagine a system implementation: In a download system has a file download module and a download file current progress Display module, the system requires real-time display file download progress, think very simple, in the object-oriented world is nothing more than the implementation of two classes. But here's the problem, showing how the module drives the download progress bar? Display module do not know and should not know the download module is aware of the file download progress (object-oriented design of the package, the module to be decoupled, the module to be cohesive), the file download progress is only the download module to know things, The solution is simple to pass a function pointer to the download module as the callback function to drive the Display module display progress.

?? In the object-oriented world, such an example is quite a lot, causing the root cause of such problems, I believe that we have learned from the above narrative, is the object-oriented program design ideas, is the design mode of the module independence, cohesion low coupling and other characteristics.

?? Packaging change programming strategy The first idea for programmers is to interface-oriented programming, which is referred to in design patterns for virtual programming rather than for implementation. This kind of programming thought greatly revolutionized the programming world, it can be said that without this principle there is no object-oriented programming, this principle to the design of a guiding ideology: How to better map the real model into a program model. Such design ideas in the highly independent module to greatly develop and weaken the collaboration between the modules, that is, coupling, it makes the module more engaged in one-way call work, a module needs some kind of service to find another module, which makes the program show a hierarchy, high-level through the interface call the bottom, Services on the ground floor. but the real-world strict adherence to the current level of the system is very rare, the absolute MVC is not there, because more modules require to work together, there is no coupling can be seen without the coordination of no good call relationship, coupling is really not wrong.

?? Now that we need the collaboration between modules, and we hate to abandon the module between you and me I have your ambiguous relationship how to build a system, the answer is a function pointer (not necessarily a function pointer) is the way to use callbacks. if an object is concerned about the state of another object then the change to the state registers the callback function so that it notifies you of such a state change, so that when the module changes are encapsulated, the collaborative relationship between the modules is also decoupled to the object.

The popular understanding of callbacks

?? You're hungry, you want to eat, just a minute to ask your mother, "Dinner no ah?" This is the normal function call.

?? But today your mother dumplings, spend more time, you run AH run ah, bored. So you say to your mother, I first go out to play, the meal when the call my cell phone. After a while, your mother called you and said, "dinner, come back to eat!"

?? Which, you tell your mom to call you on the phone, is a you put the callback function handle to your mother's action. Your mom called you, that's a callback procedure.

Detailed explanation of callbacks

Understanding Mode 1: The General writing program is the API you call the system, this process is called call. If the relationship is reversed, you write a function that lets the system call your function, which is the callback (CallBack), the function called by the system is the callback function. In Android, we typically invoke the API defined by Framwork, but if we write a method that is called by Framwork, this method is the callback function.

Understanding Mode 2: The so-called callback function, which runs in a way that says, "You don't have to call me, wait for me to call you." The callback means that after you execute a statement, it succeeds, and then executes the function you passed in, which is especially useful when doing asynchronous processing.

Understanding Mode 3: To the nature of the program, shaving those object-oriented concepts, the callback function is a function pointer called by function. is to use a function pointer to do the argument. The function pointer is the callback function.

?? Callbacks are design patterns at the programming level, not language-based. To use a callback function, the called function needs to tell the caller its own pointer address, but there is no pointer in Java. We can implement the definition callback function through the interface (interface). In the world of. NET delegate ==callback.

How do I use callbacks in Android?

Java callback is implemented through interfaces.
Cases:
1.class A,class B
2.class a implementation interface operate
3.class B has a function with a parameter of operate interface type Test (operate O)
4.class a runtime invokes the test function in Class B to pass in the parameter itself
5.class B has obtained a, it can be recalled at any time the method of the operate interface implemented by a

Use case

I wrote a previous blog for the case to analyze:

http://blog.csdn.net/a910626/article/details/45767079

Callback diagram:

Where to call back
    1. The callback function is used primarily for some time-consuming operations, or to respond to events that do not know when the event will occur, and the asynchronous combination of situations (async is actually a thread that is re-opened).

    2. When there is a need for some co-operation between two objects, a callback function is often used, such as an object wanting to get some data inside another object.

Advantages of Callbacks

Callback functions make programming more flexible.

It realizes the module independence, separates the realization and the caller, and conforms to the design idea of high cohesion and low coupling in the face object.

Drawback of callbacks

The readability of the code is not good.

Brief analysis on the callback thought in the design of Android system

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.