C++的Delegate模式
1.Construct the abstract delegate base class
1 2 3 4 5 6 7 |
class Delegate { public: virtual void Invoke()=0; protected: Delegate(){} virtual ~Delegate(){} }; |
2.Construct a derive class which accepts a static/global function pointer
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 |
//NonTypeDelegate.h #include "Delegate.h" class NonTypeDelegate : public Delegate { public: void Invoke(); NonTypeDelegate(void (*pfn)(int),int iParam); virtual ~NonTypeDelegate(){} private: void (*m_pfn)(int); int m_iParam; }; //NonTypeDelegate.cpp #include "NonTypeDelegate.h" #include <iostream> using namespace std; NonTypeDelegate::NonTypeDelegate(void (*pfn)(int), int iParam):m_pfn(pfn), m_iParam(iParam) { } void NonTypeDelegate::Invoke() { cout << "NonTypeDelegate Invoke\r\n"; m_pfn(m_iParam); } |
3.Construct another derive class which accepts a member function pointer
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 |
//TypeDelegate.hpp #include "Delegate.h" #include <iostream> using namespace std; template <typename T> class TypeDelegate : public Delegate { public: void Invoke(); TypeDelegate(T &t, void (T::*pfn)(int), int iParam); ~TypeDelegate(){} private: T m_t; void (T::*m_pfn)(int); int m_iParam; }; template<typename T> TypeDelegate<T>::TypeDelegate(T &t, void (T::*pfn)(int), int iParam):m_t(t), m_pfn(pfn), m_iParam(iParam) { } template<typename T> void TypeDelegate<T>::Invoke() { cout << "TypeDelegate Invoke\r\n"; (m_t.*m_pfn)(m_iParam); } |
4.Now glue up all the stuffs
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 |
#include <iostream> #include "NonTypeDelegate.h" #include "TypeDelegate.hpp" #include <vector> using namespace std; void Test(int iParam) { cout << "Test Invoked\r\n"; } class A { public: void Test(int iParam) { cout << "A::Test Invoked\r\n"; } }; int main(int argc, char* argv[]) { NonTypeDelegate nTDelegate(Test,1); A a; TypeDelegate<A> tDelegate(a,A::Test,2); vector<Delegate*> vecpDelegate; vecpDelegate.push_back(&nTDelegate); vecpDelegate.push_back(&tDelegate); for (vector<Delegate*>::const_iterator kItr=vecpDelegate.begin(); kItr!=vecpDelegate.end(); ++kItr) { (*kItr)->Invoke(); } return 0; } |
5.And the output is
1 2 3 4 |
NonTypeDelegate Invoke Test Invoked TypeDelegate Invoke A::Test Invoked |
发表评论
要发表评论,您必须先登录。