什么是钩子
之前有转一篇关于回调函数的文章
钩子函数、注册函数、回调函数,他们的概念其实是一样的。
钩子函数,顾名思义,就是把我们自己实现的hook函数在某一时刻挂接到目标挂载点上。
1. hook函数,就是我们自己实现的函数,函数类型与挂载点匹配(返回值,参数列表)
2. 挂接,也就是hook或者叫注册(register),使得hook函数对目标可用
3. 目标挂载点,也就是挂我们hook函数的地方(我们想在这个目标点实现我们自己的功能)
先看一张图:
hook的概念在windows的消息响应机制里面体现的尤为明显。可能我们大家有写过windows桌面相关的程序(像MFC),里面有各种消息监听响应机制。比如,要监听鼠标左键是否按下这个事件,我们要去实现一个onLeftKeyDown()之类的方法,该方法可以称为钩子函数。同时,我们还要去注册钩子函数,MFC中是通过一组宏来实现的。这样当鼠标左键按下后,就能调到我们定义的方法了。
为什么需要钩子
大家思考一下上面这个例子,左键按下方法具体的逻辑是由框架自身去实现,还是由我们用户(调用者)去实现呢?显然应该由我们自己去实现。要提供通用的框架能力,框架自身去实现该方法功能,是没有意义的,所以框架给提供一个挂载的point,把具体逻辑的实现交给用户就好了,灵活可用。
钩子使用
hook是一个编程机制,与语言无关。这里给个python的简单例子,帮助大家理解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
import time
class LazyPerson( object ):
def __init__( self , name):
self .name = name
self .watch_tv_func = None
self .have_dinner_func = None
def get_up( self ):
print ( "%s get up at:%s" % ( self .name, time.time()))
def go_to_sleep( self ):
print ( "%s go to sleep at:%s" % ( self .name, time.time()))
def register_tv_hook( self , watch_tv_func):
self .watch_tv_func = watch_tv_func
def register_dinner_hook( self , have_dinner_func):
self .have_dinner_func = have_dinner_func
def enjoy_a_lazy_day( self ):
# get up
self .get_up()
time.sleep( 3 )
# watch tv
# check the watch_tv_func(hooked or unhooked)
# hooked
if self .watch_tv_func is not None :
self .watch_tv_func( self .name)
# unhooked
else :
print ( "no tv to watch" )
time.sleep( 3 )
# have dinner
# check the have_dinner_func(hooked or unhooked)
# hooked
if self .have_dinner_func is not None :
self .have_dinner_func( self .name)
# unhooked
else :
print ( "nothing to eat at dinner" )
time.sleep( 3 )
self .go_to_sleep()
def watch_daydayup(name):
print ( "%s : The program ---day day up--- is funny!!!" % name)
def watch_happyfamily(name):
print ( "%s : The program ---happy family--- is boring!!!" % name)
def eat_meat(name):
print ( "%s : The meat is nice!!!" % name)
def eat_hamburger(name):
print ( "%s : The hamburger is not so bad!!!" % name)
if __name__ = = "__main__" :
lazy_tom = LazyPerson( "Tom" )
lazy_jerry = LazyPerson( "Jerry" )
# register hook
lazy_tom.register_tv_hook(watch_daydayup)
lazy_tom.register_dinner_hook(eat_meat)
lazy_jerry.register_tv_hook(watch_happyfamily)
lazy_jerry.register_dinner_hook(eat_hamburger)
# enjoy a day
lazy_tom.enjoy_a_lazy_day()
lazy_jerry.enjoy_a_lazy_day()
|
代码运行结果:
1
2
3
4
5
6
7
8
|
Tom get up at: 1509246940.32
Tom : The program - - - day day up - - - is funny!!!
Tom : The meat is nice!!!
Tom go to sleep at: 1509246949.34
Jerry get up at: 1509246949.34
Jerry : The program - - - happy family - - - is boring!!!
Jerry : The hamburger is not so bad!!!
Jerry go to sleep at: 1509246958.37
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Mybigkid/article/details/78383898