Structured designed rescue straw-callback mechanism

xiaoxiao2021-03-06  41

Summary: The establishment of development model is an indispensable part of the software development process. In terms of process and object-oriented, it is two main design methods. Although object-oriented OOP is a popular word, but does not mean the process of process It must be good, after all, the software system for the process design method is also suitable for its application: the functional operation is mainly, the scalability is not high, no need to consider multiple demographics and software versatility. Isn't that the process-oriented design method doesn't have a way for the system framework expansion?

According to the basic principles of the process, the system function module, the module is divided into functions, generate a structural model of the system, which seems to have anything until anything can be used to provide system expansion, and the method of solving is still, this root Help straw is a callback mechanism.

As soon as the callback mechanism, there is certainly less of our protagonist: System API (usually) and callback functions, both of which are not possible. In fact, the basic thinking of callback is to provide us with some interfaces, which is often used, which can use the address of a certain function as one of its parameters, and can use this address to call this function, and The called function is what we usually call the callback function. The following is a small example of a callback function: ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - / / Equivalent to the system ApimainFunc (Void * userfunc) / / of course, the parameter is not as simple, just simulating {while (...) {printf ("ok!"); // Call the callback function IF (userfunc! = null) userfunc ();}} It can be seen that MainFunc can call it according to the address of the function userfunc. ----------------------------------------- This user only needs to define a function : Void myfunc (), followed by Mainfunc (& MyFunc) (only means that the delivery is the function of the function, there is no specific meaning), allowing our mainfunc to invoke MyFunc to achieve the corresponding function, which of course can complete our expectations. Purpose - Extend existing systems.

In the Windows system, the system API supports such a callback mechanism does not occupy a few, like the SortItem () function of ListControl sort, and the functions used by FONT funfampies () have this callback mechanism, making our users Opportunity to add your expectations. Of course, the use of the callback function is not a relaxed thing. If there is a lot of callback functions in our system, it is difficult to manage, this is the same as a large amount of global variables in the system, there is a number of functions to discuss the same variable. We are difficult to use simple logic to handle, which is easy to fall into chaos, so, although the callback mechanism can achieve our goal to some extent, it cannot be used, otherwise it is difficult to expect. Of course, for the detailed callback function, you still need to be able to study, here I just summarize: 1 callback function is a function defined by developers according to certain prototypes (each callback function must follow this prototype)

For example: ----------------------------------------- Bool Callback DialogProc (HWND HWNDDLG , // Handle of Dialog Box Uint UMSG, // Message WParam WParam, // First Message Parameter LParam Lparam // Second Message Parameter; -------------------------------------------------------------------------------------------- ---------------------- Description: The callback function must have the keyword Callback callback function itself must be a full function or static function, which is not defined as a particular class. Member function

2 The callback function does not cause the developer to call execution (just using the system interface API function as the starting point) 3 The callback function is used to pass to the system API as a parameter, and the 4 callback function may be called once by the system API, or it may be cycled Call multiple times (SortItem is self-adjusted)

Finally, it is true that there is another mechanism-message mechanism in the Windows system, which is also a more good tool that can provide a solution for many practical issues. This will then summarize.

--Kingle ---

转载请注明原文地址:https://www.9cbs.com/read-68799.html

New Post(0)