Hook is a common concept in programming, which is very important. It makes the system very easy to expand (without understanding its internal implementation mechanism, which can reduce a lot of work).
It can be understood that when a glass ball falls from the air and is about to hit a person, an event will happen in advance. For example, tell the person who is hit that the ball is already in the process of falling,
Telling is an event, a hook. We can make different responses for different people. If it's a man, we can tell him that it doesn't hurt when he hits the ball. If it's a woman, we can tell her that it hurts
Hook functions can intercept and process messages from other applications. Whenever a specific message is sent, the hook program captures the message before it reaches the destination window, that is, the hook function gets control first. At this time, the hook function can process (change) the message, continue to deliver the message without processing, and force the end of message delivery.
Hook implementation/*The complete implementation of hooks should be called event driven. Event driven is divided into two stages. The first stage is to register events, aiming to give a name to the "events" that may occur in the future. The simple implementation method is
Use the singleton mode to generate a persistent object or register a global variable, and then insert the event name and the corresponding class and method of the event into the global variable. That is to mount a hook.
The second stage is to trigger events. In essence, it is to query the event name to trigger in the global variables of the event, find the registered classes and methods, instantiate and run them. That way, you can put it
In order to get rid of the traditional way, the program must follow the rules in order to further realize the purpose of decoupling.