Talk about hooks (hook) People familiar with Windows development should be familiar with, such as mouse hooks, keyboard hooks and so on. In a simple language description is to place a hook in the normal processing process, when the execution to place the hook will go into the specified hook function to deal with, pending processing and return to the original process to continue processing, of course, can also directly stop the original process execution. So the hook is an important mechanism for Windows message processing, specifically for monitoring certain event messages that you specify.
If you look at a higher-level, more abstract angle, the hook is actually a mechanism is a thought, its core idea is to trigger the event message at all the key points of the complex processing flow, and if the hook is added, the hook function is invoked, and the function can execute different logic according to the event message passed over. It's like transparently letting the program hang on to extra processing, and the processing logic can be deferred until later by the developer customization.
Why use a hook mechanism? It can be argued that in a large system, some basic processing processes are relatively fixed, and that the internal logic of the system should not allow external modification of it, but also to consider the scalability of the system, Certain interfaces must be reserved so that developers can customize some additional processing logic without changing the basic processing flow within the system. So the introduction of the hook mechanism, according to the final effect of the hook thought is in a permit to embed the custom code in place, this mechanism ensures that the system is not subject to external modification while reserving enough space for expansion.
What is familiar to Java is that the JVM's closure hooks are shutdownhook, providing a function to perform additional operations before the virtual machine shuts down. Of course, hooks are not just specific features, it is a mechanism, is a design method. See how Tomcat's response object uses the hook mechanism.
① Defining Hook Interfaces
Public interface Actionhook {
public void Action (Actioncode actioncode, Objectparam);
}
② defines the message state value for ease of understanding, assuming there are only two states, which actually contain dozens of states
public enum Actioncode {
CLOSE, COMMIT
}
The ③ response object, which contains the hook properties
public class Response {
Public Actionhook Hook;
Public Actionhook Gethook () {
return hook;
}
public void Sethook (Actionhook hook) {
This.hook = hook;
}
public void action (Actioncode actioncode,object param) {
Hook.action (Actioncode, param);
}
}
④ hook processing class, respectively, different message states to different logical processing
public class Http11processor implements Actionhook {
public void action (Actioncode actioncode,object param) {
if (Actioncode ==actioncode.close) {
System.out.println ("beforeclosing");
} else if (Actioncode ==actioncode.commit) {
System.out.println ("beforecommitting");
}
}
}
⑤ test class, assuming that the response object is processed as follows, then each key node through the action method triggers the hook, and with the message state, so each key point can do something extra, as long as by modifying the action in the Http11processor release can be, Custom processing logic based on state.
public class Hooktest {
public static void Main (string[] args) {
Actionhook actionhook=new http11processor ();
Response response=new Response ();
Response.sethook (Actionhook);
Response.action (actioncode.commit, NULL);
System.out.println ("Commit ...");
Response.action (actioncode.close, NULL);
System.out.println ("close ...");
}
}
Hooks are more than just windows for you to leave behind the back door