Analysis of callback ideas in android system design, android callback

Source: Internet
Author: User
Tags call back

Analysis of callback ideas in android system design, android callback
I. Why do I write this article?

During the process of gradually getting into touch with android development, I have found that many callback ideas are used in android (at least in app development. For example, the lifecycle of an activity and fragment are implemented by callback functions. One of the event processing mechanisms in android is callback, thread-based Asynchronous tasks, loader, and hanlder are all callback-based tasks.

Callback callback: the basic idea of android is callback. When you participate, you need to call the relevant callback method and fill it in.

Callback callback can be said to infiltrate the idea of callback and all aspects of android design. Almost the on Method in android development is a callback method. As a newbie who is devoted to deep learning about mobile development, it is necessary to thoroughly understand callback.

Ii. Why should callback be introduced?

The Zookeeper callback is an alternative design policy. Imagine a system implementation: In a download system, there is a File Download module and a file download progress display module, the system requires real-time display of the file download progress. Think about it very easily. In the object-oriented world, there are only two categories. But the problem is exactly here. How does the module drive the download progress bar? The display module does not know and should not know the file download progress that the download module knows (encapsulation of Object Oriented Design, decoupling between modules, and cohesion within the module ), the file download progress is only known to the download module. The solution is simple: pass a function pointer to the download module as the callback function driver to display the progress of the module.

There are quite a few examples of zookeeper In the object-oriented world,The root cause of this problem is that we have learned from the above description that it is the object-oriented programming idea, that is, the module independence required by the design model, high Cohesion, low coupling, and other features.

The guiding ideology that zookeeper encapsulates the changing programming strategy to programmers is interface-oriented programming, that is, virtual programming mentioned in the design pattern rather than implementation-oriented. This idea of programming greatly revolutionizes the world of programming. It can be said that there is no object-oriented programming without this principle. This principle provides a guiding ideology for programming: that is, how to map a realistic model to a program model.This design concept greatly promotes highly independent modules and weakens the collaboration between modules, that is, coupling. It enables more modules to engage in one-way calls, one module needs a certain service to find another module, which makes the program hierarchical. The High-level layer calls the underlying layer through the interface and provides services at the underlying layer.However, in the real world, systems that strictly follow the current hierarchy are rare. The absolute MVC does not exist,Because more modules require communication and collaboration, it can be seen that there is no good call relationship for collaboration without coupling. coupling is really not wrong.

Zookeeper: since we need collaboration between modules, and we hate to discard the ambiguous relationship between modules, you and me, how can we generate a system, the answer is the function pointer (not necessarily the function pointer), that is, the callback method.If an object cares about the state changes of another object, register the callback function for the state changes to notify you of such State changes, in this way, the module changes are encapsulated, and the inter-module collaboration relationships are separated to the object through independent paths.

Iii. Common Understanding of callback

When you are hungry and want to eat, I will ask your mom later, "Are you ready ?" This is a normal function call.

It takes a long time for your mom to make dumplings today. so you told your mom that I went out for a meeting and called my cell phone when I had dinner. after a while, your mom called you and said, "Open dinner. Come back for dinner!"

When you tell your mom to call you, it means you save the callback function handle to your mom's action. When you call your mom, it is a callback process.

Iv. Callback explanation

Method 1: Generally, you write a program to Call the system API. This process is called Call. If you write a function to call your function, the CallBack function is called by the system. In android, we generally call the api defined by framwork, but if we write a method called by framwork, this method is the callback function.

Method 2: The call back function runs in the following way: "You don't need to call me, wait for me to call you ". Callback means that after you execute a statement successfully, and then execute the function you passed in, it is particularly useful for asynchronous processing.

Understanding Method 3: Program nature, excluding the object-oriented concepts, the callback function is a function called through the function pointer. The function pointer is used as a parameter. The function pointer is the callback function.

Callback callback is a programming design mode, not language-based. In C/C ++, to use a callback function, the called function must tell the caller its own pointer address, but there is no pointer in JAVA. What should I do? We can use interfaces to define callback functions. In the. Net world, Delegate = Callback.

5. How to Use callback in android?

Java callback is implemented through interfaces.
Example:
1. class A, class B
2. class A implements the interface operate
3. class B has a function test (operate o) with parameters of the operate interface type)
4. class A calls the test function in class B during runtime, and passes in parameters as itself
5. If class B has obtained A, the method in the operate interface implemented by class B can be called back at any time.

Vi. Use Cases

I used a previous blog as a case study to analyze it:

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

Callback illustration:

VII. Where to Use callback 8. Advantages of callback

The callback function makes the program design more flexible.

Implement module independence and separate the implementer from the caller. This is in line with the design philosophy of High Cohesion and low coupling of objects.

9. disadvantages of callback

Ease of coding is not good.

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.