您好,登錄后才能下訂單哦!
什么是鉤子
之前有轉(zhuǎn)一篇關(guān)于回調(diào)函數(shù)的文章
鉤子函數(shù)、注冊函數(shù)、回調(diào)函數(shù),他們的概念其實是一樣的。
鉤子函數(shù),顧名思義,就是把我們自己實現(xiàn)的hook函數(shù)在某一時刻掛接到目標掛載點上。
1. hook函數(shù),就是我們自己實現(xiàn)的函數(shù),函數(shù)類型與掛載點匹配(返回值,參數(shù)列表)
2. 掛接,也就是hook或者叫注冊(register),使得hook函數(shù)對目標可用
3. 目標掛載點,也就是掛我們hook函數(shù)的地方(我們想在這個目標點實現(xiàn)我們自己的功能)
先看一張圖:
hook的概念在windows的消息響應機制里面體現(xiàn)的尤為明顯??赡芪覀兇蠹矣袑戇^windows桌面相關(guān)的程序(像MFC),里面有各種消息監(jiān)聽響應機制。比如,要監(jiān)聽鼠標左鍵是否按下這個事件,我們要去實現(xiàn)一個onLeftKeyDown()之類的方法,該方法可以稱為鉤子函數(shù)。同時,我們還要去注冊鉤子函數(shù),MFC中是通過一組宏來實現(xiàn)的。這樣當鼠標左鍵按下后,就能調(diào)到我們定義的方法了。
為什么需要鉤子
大家思考一下上面這個例子,左鍵按下方法具體的邏輯是由框架自身去實現(xiàn),還是由我們用戶(調(diào)用者)去實現(xiàn)呢?顯然應該由我們自己去實現(xiàn)。要提供通用的框架能力,框架自身去實現(xiàn)該方法功能,是沒有意義的,所以框架給提供一個掛載的point,把具體邏輯的實現(xiàn)交給用戶就好了,靈活可用。
鉤子使用
hook是一個編程機制,與語言無關(guān)。這里給個python的簡單例子,幫助大家理解:
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()
代碼運行結(jié)果:
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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。