(1)Callback方式
Callback的本質是設置一個函數指針進去,然后在需要需要觸發某個事件時調用該方法, 比如Windows的窗口消息處理函數就是這種類型。比如下面的示例代碼,我們在Download完成時需要觸發一個通知外面的事件:
1 typedef void (__stdcall *DownloadCallback)(const char* pURL, bool bOK); 2 void DownloadFile(const char* pURL, DownloadCallback callback) 3 { 4 cout << "downloading: " << pURL << "" << endl; 5 callback(pURL, true); 6 } 7 void __stdcall OnDownloadFinished(const char* pURL, bool bOK) 8 { 9 cout << "OnDownloadFinished, URL:" << pURL << " status:" << bOK << endl; 10 }
(2)Sink方式
Sink的本質是你按照對方要求實現一個C++接口,然后把你實現的接口設置給對方,對方需要觸發事件時調用該接口, COM中連接點就是居於這種方式。上面下載文件的需求,如果用Sink實現,代碼如下:
1 class IDownloadSink 2 { 3 public: 4 virtual void OnDownloadFinished(const char* pURL, bool bOK) = 0; 5 }; 6 7 8 class CMyDownloader 9 { 10 public: 11 CMyDownloader(IDownloadSink* pSink) 12 :m_pSink(pSink) 13 { 14 } 15 16 void DownloadFile(const char* pURL) 17 { 18 cout << "downloading: " << pURL << "" << endl; 19 if(m_pSink != NULL) 20 { 21 m_pSink->OnDownloadFinished(pURL, true); 22 } 23 } 24 25 private: 26 IDownloadSink* m_pSink; 27 }; 28 29 class CMyFile: public IDownloadSink 30 { 31 public: 32 void download() 33 { 34 CMyDownloader downloader(this); 35 downloader.DownloadFile("www.baidu.com"); 36 } 37 38 virtual void OnDownloadFinished(const char* pURL, bool bOK) 39 { 40 cout << "OnDownloadFinished, URL:" << pURL << " status:" << bOK << endl; 41 } 42 };
(3)Delegate方式
Delegate的本質是設置成員函數指針給對方,然后讓對方在需要觸發事件時調用。C#中用Delegate的方式實現Event,讓C++程序員很是羡慕,C++中因為語言本身的關系,要實現Delegate還是很麻煩的。上面的例子我們用Delegate的方式實現如下:
class CDownloadDelegateBase { public: virtual void Fire(const char* pURL, bool bOK) = 0; }; template<typename O, typename T> class CDownloadDelegate: public CDownloadDelegateBase { typedef void (T::*Fun)(const char*, bool); public: CDownloadDelegate(O* pObj = NULL, Fun pFun = NULL) :m_pFun(pFun), m_pObj(pObj) { } virtual void Fire(const char* pURL, bool bOK) { if(m_pFun != NULL && m_pObj != NULL) { (m_pObj->*m_pFun)(pURL, bOK); } } private: Fun m_pFun; O* m_pObj; }; template<typename O, typename T> CDownloadDelegate<O,T>* MakeDelegate(O* pObject, void (T::*pFun)(const char* pURL, bool)) { return new CDownloadDelegate<O, T>(pObject, pFun); } class CDownloadEvent { public: ~CDownloadEvent() { vector<CDownloadDelegateBase*>::iterator itr = m_arDelegates.begin(); while (itr != m_arDelegates.end()) { delete *itr; ++itr; } m_arDelegates.clear(); } void operator += (CDownloadDelegateBase* p) { m_arDelegates.push_back(p); } void operator -= (CDownloadDelegateBase* p) { ITR itr = remove(m_arDelegates.begin(), m_arDelegates.end(), p); ITR itrTemp = itr; while (itrTemp != m_arDelegates.end()) { delete *itr; ++itr; } m_arDelegates.erase(itr, m_arDelegates.end()); } void operator()(const char* pURL, bool bOK) { ITR itrTemp = m_arDelegates.begin(); while (itrTemp != m_arDelegates.end()) { (*itrTemp)->Fire(pURL, bOK); ++itrTemp; } } private: vector<CDownloadDelegateBase*> m_arDelegates; typedef vector<CDownloadDelegateBase*>::iterator ITR; }; class CMyDownloaderEx { public: void DownloadFile(const char* pURL) { cout << "downloading: " << pURL << "" << endl; downloadEvent(pURL, true); } CDownloadEvent downloadEvent; }; class CMyFileEx { public: void download() { CMyDownloaderEx downloader; downloader.downloadEvent += MakeDelegate(this, &CMyFileEx::OnDownloadFinished); downloader.DownloadFile("www.baidu.com"); } virtual void OnDownloadFinished(const char* pURL, bool bOK) { cout << "OnDownloadFinished, URL:" << pURL << " status:" << bOK << endl; } };
可以看到Delegate的方式代碼量比上面其他2種方式大多了,並且我們上面是固定參數數量和類型的實現方式,如果要實現可變參數,要更加麻煩的多。可變參數的方式可以參考這2種實現:
Yet Another C#-style Delegate Class in Standard C++
Member Function Pointers and the Fastest Possible C++ Delegates
我們可以用下面的代碼測試我們上面的實現:
int _tmain(int argc, _TCHAR* argv[]) { DownloadFile("www.baidu.com", OnDownloadFinished); CMyFile f1; f1.download(); CMyFileEx ff; ff.download(); system("pause"); return 0; }
最后簡單比較下上面3種實現回調的方法:
第一種Callback的方法是面向過程的,使用簡單而且靈活,正如C語言本身。
第二種Sink的方法是面向對象的,在C++里使用較多,可以在一個Sink里封裝一組回調接口,適用於一系列比較固定的回調事件。
第三種Delegate的方法也是面向對象的,和Sink封裝一組接口不同,Delegate的封裝是以函數為單位,粒度比Sink更小更靈活。
你更傾向於用哪種方式來實現回調?
還有一種是通過sigslot信號槽機制來實現委托
相關sigslot的用法可以參考這篇文章。
相關頭文件下載地址