Discussion on asynchronous callback method

Source: Internet
Author: User

There are many times when an asynchronous operation is required to avoid blocking the current thread, and the result of an asynchronous operation requires a callback to inform the caller that there are multiple callbacks and different language support scenarios.

A callback mechanism is a piece of code that is prepared before a certain time, and once the event occurs, the code is executed accordingly. So the callback has two main components: (1) The code that needs to be executed when the registration process (2) occurs.

There are several main ways to implement the callback mechanism:

1. Registering objects and Callback methods

(1) Target-action pair: Asynchronously invokes a pointer to the target object pointer and its callback processing method, and when the asynchronous operation ends, the called object finds the entry for the callback and executes the callback.

(2) The delegation mechanism: only need to save the target object's pointer, and the target object definitely implements the callback protocol, that is, its callback processing method is unique, clear, so only through the target object, you can find the callback interface. A delegation mechanism is available when an object receives multiple events and requires the same object to handle all events.

(3) Notification mechanism: similar to the Observer pattern in design mode (subscriber-publisher mode), multiple listener objects, each object has a callback entry (target-action pair), they listen to the same asynchronous event, when the asynchronous completion, they will be one by one back tune. The advertisement mechanism can be used when multiple objects (or two unrelated objects) are required to handle the same event.

2. Inheriting callback classes

We can create Class A to inherit a callback Class (interface), and implement the specified callback method, then the object of Class A can be used as a callback object, so that the asynchronous call without registering the object and method, instead of directly passing in a callback object. This approach is convenient during the registration process, but it is troublesome to prepare the callback code because different types of callbacks create a new class that causes the code to swell.

3. Implementation callback at Registration

This approach is only used in the same way as the callback, which allows you to write callback processing code while invoking the asynchronous interface, which incorporates the process of registering the callback and preparing the callback code. This is a good way to handle simple and fast callback tasks and help developers write short, refined code. The block mechanism in the known object-c and the callback mechanism in Java support this approach.

Discussion on asynchronous callback method

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.