Callback

The callback function is that you write a function and let the pre-written system call it. You go to call the function of the system, it is a direct adjustment. Let the system call your function, which is a callback. But if you are satisfied with this one-sentence conclusion, you will not really understand.

The callback function can be seen as the extra information passed in by letting others do things.

For example, A asks B to do things. Depending on the granularity, it can be understood that A function calls B function, or class A uses class B, or component A uses component B, and so on. Anyway, A tells B to do things.

When B does this, the information it needs is not enough, and A has it. It is necessary for A to pass in from the outside, or for B to actively apply to the outside. For B, one is to passively obtain information, and the other is to actively obtain information. Some people give terms for these two methods, which are called information push and information pull.

A calls B, and A needs to pass parameters to B. Such as simple function:
int max(int a, int b);

To use this function to get the largest value of the two, you must pass in a, b. This is easy to understand.

void qsort(void *, size_t, size_t, int (*)(const void *, const void *));

And this function is used for sorting, and the last parameter is the callback function, which seems to be more difficult to understand. This is because of the artificial separation of code and data.

Let's pause and look at the weirder part of the computer, which is the unification of code and data. This is a threshold. If we do not get through this, many concepts will be unclear. We often say that computer programs are divided into two parts: code and data. Many people will think that the code can be run, which is dynamic, and the data is used by the code, which is static. These are two completely different things.

In fact, code is just a description of behavior, such as a robot that can turn on the lights, turn off the lights, and sweep the floor. If agreed with the robot, 0 means turn on the lights, 1 means turn off the lights, and 2 means sweep the floor. I send out the command string, 0 1 2, and I can control the robot to turn on the lights, turn off the lights, and sweep the floor. Then agree to use binary representation, two bits are an instruction, there is a string of numbers, 000111, at this time, the string of numbers 000111 describes a series of actions of the robot, which is understood from one aspect as code, which can control the behavior of the robot. But on the other hand, it can be passed, it can be recorded, it can be modified, that is, data. As long as everyone negotiates, the code can be encoded into data, and when the data is interpreted and run, it will also become code.

Code and data can be used without distinction, and are collectively called information. Since int max(int ​​a, int b) int, double and other things that represent ordinary data can be passed in, why can't the function that represents code be passed in. Some languages ​​are indeed indistinguishable, and its function (representing code) is the same as int and double. The language is that functions are first-class values.

In some languages, functions cannot be stored, functions cannot be dynamically created, and functions cannot be dynamically destroyed. Only a pointer to a function can be stored, and this language calls functions a second-class value.

In addition, some languages ​​can not only transfer functions, but also use some external information (including code, data) in functions. Those languages ​​can store functions along with the information they use. This way of taking a function and the information it uses as a whole is called a closure.

Unify the code and data. After this threshold, many difficult concepts will become clearer.

Now let's go back and look at the callback function. The callback function is that A asks B to do things, and B is doing it. If there is not enough information, and if he does not know what to do, he will let the outside deal with it.

For example, in the above sorting example, A lets B sort, and B will sort, but sorting needs to know which is bigger than which. B doesn't know this, so A needs to tell it. And this kind of judgment is a kind of action in itself. Since the function of the first value cannot be passed in the C language, it is designed to pass the function pointer of the second value. This function pointer is the information that A transmits to B. Use to represent an action. Originally, A called B, and as a result, B called the information that A told it, which is also called callback.

Another example is that A asks B to monitor a certain message in the system, such as which key was pressed. Follow B to listen to it, but it doesn't know how to handle this message, so it is handled by people who care about this message outside and know how to handle this message. This processing process itself is a behavior, since this language cannot pass functions, and You can only pass a function pointer. If I store the function pointer, I can call it anytime later. Both code and data are information, and data can be stored, as can functions used to represent behavior.

Followed by some people, it will be extended, what registration, notification, and so on. If B is monitoring, C, D, E, F, G, H tell B that he is interested in knowing the news, then B will tell C, D, E, F, G and others when he listens, so that many people will be notified. , called broadcasting.

In fact, you understand, you don't need to care about the terminology at all. Terminology is a set of agreed-upon words that others are going to tell you, or you are going to tell people. Essentially this thing, the result will be a lot of terms.

Then generalize the concept of callback, for example, someone cares about A, B, C, D, E, F events at the same time, and these events are a group, such as keyboard typing, mouse movement, mouse click and so on. Combine a set of events. In some languages, it is mapped to an interface, and the interface has N functions. Some languages ​​map to a structure with N function pointers in it. Then instead of passing in a single function pointer, you pass in an interface, or a structure of function pointers. Depending on the usage, some people call it proxy, listener, observer, etc.

In fact, it also stores a certain behavior and calls it later when it is needed. There is no difference at a deeper level with callback functions.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325028718&siteId=291194637