回调 sink回调 delegate回调 delegate回调

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
#include<stdlib.h>
using namespace std;
class
{
virtual void OnDownLoadFinished(const char* url, bool OK) = 0;
}
class CMyDownloader
{
public:
CMyDownloader(IDownloadSink* plink):m_pSink(plink)
{
}
void DownLoadFile(const char* pURL)
{
cout << "downloading:" << pURL << " " << endl;
if(m_pSink)
{
m_pSink->OnDownLoadFinished(pURL,true);
}
}
private:
IDownloadSink* m_pSink;
}
class CMyFlie : public IDownloadSink
{
void download()
{
CMyDownloader download(this);
download.DownLoadFile("www.baidu.com");
}
virtual void OnDownLoadFinished(const char* pURL, bool OK)
{
cout << "OnDownloadFinished, URL:" << pURL << " status:" << bOK << endl;
}
}
int main()
{
CMyFlie myfile;
myfile.download();
system("pause");
}

delegate回调

/**
Copyright:ZOWEE
Author: wufei
Date:2017-07-24
Description:下载代理c++类实现
**/

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
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_arDelegate.begin();
while (itr != m_arDelegate.end())
{
delete *itr;
++itr;
}
m_arDelegate.clear();
}
void operator += (CDownloadDelegateBase* p)
{
m_arDelegate.push_back(p);
}
void operator -= (CDownloadDelegateBase* p)
{
ITR itr = remove(m_arDelegate.begin(), m_arDelegate.end(), p);
ITR itrTemp = itr;
while (itrTemp != m_arDelegate.end())
{
delete *itr;
++itr;
}
m_arDelegate.erase(itr, m_arDelegate.end());
}
void operator ()(const char* pURL, bool bOK)
{
ITR itrTemp = m_arDelegate.begin();
while (itrTemp != m_arDelegate.end())
{
(*itrTemp)->Fire(pURL, bOK);
++itrTemp;
}
}
private:
vector<CDownloadDelegateBase*> m_arDelegate;
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;
}
};
int main()
{
CMyFileEx ff;
ff.download();
system("pause");
}