(1)Callback方式
Callback的本質是設置一個函數指針進去,而後在須要須要觸發某個事件時調用該方法, 好比Windows的窗口消息處理函數就是這種類型。
好比下面的示例代碼,咱們在Download完成時須要觸發一個通知外面的事件:
typedef
void (__stdcall *DownloadCallback)(
const
char* pURL,
bool bOK);
void DownloadFile(
const
char* pURL, DownloadCallback callback)
{
cout << "downloading: " << pURL << "
" << endl;
callback(pURL,
true);
}
void __stdcall OnDownloadFinished(
const
char* pURL,
bool bOK)
{
cout << "OnDownloadFinished, URL:" << pURL << " status:" << bOK << endl;
}
(2)Sink方式
Sink的本質是你按照對方要求實現一個C++接口,而後把你實現的接口設置給對方,對方須要觸發事件時調用該接口, COM中鏈接點就是居於這種方式。
上面下載文件的需求,若是用Sink實現,代碼以下:
class IDownloadSink
{
public:
virtual
void OnDownloadFinished(
const
char* pURL,
bool bOK) = 0;
};
class CMyDownloader
{
public:
CMyDownloader(IDownloadSink* pSink)
:m_pSink(pSink)
{
}
void DownloadFile(
const
char* pURL)
{
cout << "downloading: " << pURL << "
" << endl;
if(m_pSink != NULL)
{
m_pSink->OnDownloadFinished(pURL,
true);
}
}
private:
IDownloadSink* m_pSink;
};
class CMyFile:
public IDownloadSink
{
public:
void download()
{
CMyDownloader downloader(
this);
downloader.DownloadFile("www.baidu.com");
}
virtual
void OnDownloadFinished(
const
char* pURL,
bool bOK)
{
cout << "OnDownloadFinished, URL:" << pURL << " status:" << bOK << endl;
}
};
(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更小更靈活。 你更傾向於用哪一種方式來實現回調?