一个简单的Observer实现
相信大家对Observer模式都比较了解,在开发过程中这个模式经常被使用。
下面,和大家分享下我的实现,我就直接贴代码了,希望对阅读过的朋友有帮助。
个人认为,其中的模板类比较重要,具有一定的复用性。如模板类中的函数不满足要求,就修改、添加吧。
下面,和大家分享下我的实现,我就直接贴代码了,希望对阅读过的朋友有帮助。
个人认为,其中的模板类比较重要,具有一定的复用性。如模板类中的函数不满足要求,就修改、添加吧。
1
/*
*
2 CopyRight: (C) 2011 by caipan
3 Email: [email protected]
4 */
5 #include < iostream >
6 #include < list >
7 class ICalcEventSource;
8
9 template < class _Listener >
10 class EventSourceMulticaster
11 {
12 public :
13 ~ EventSourceMulticaster()
14 {
15 m_listeners.clear();
16 }
17
18 bool addListenerInternal(_Listener * pListener)
19 {
20 for (typename std::list < _Listener *> ::iterator i = m_listeners.begin(); i != m_listeners.end(); i ++ )
21 {
22 if ( * i == pListener)
23 {
24 return false ;
25 }
26 }
27
28 m_listeners.push_back(pListener);
29
30 return true ;
31 }
32
33 bool removeListenerInternal(_Listener * pListener)
34 {
35 if (pListener == NULL)
36 {
37 return false ;
38 }
39
40 for (typename std::list < _Listener *> ::iterator i = m_listeners.begin(); i != m_listeners.end(); i ++ )
41 {
42 if ( * i == pListener)
43 {
44 m_listeners.erase(i);
45 return true ;
46 }
47 }
48
49 return false ;
50 }
51
52 void sendEvent( void (_Listener:: * func)())
53 {
54 typename std::list < _Listener *> ::iterator iter = m_listeners.begin();
55 while (iter != m_listeners.end())
56 {
57 _Listener * pListener = * iter;
58 ++ iter;
59 (pListener ->* func)();
60 }
61 }
62
63 template < class _A1 >
64 void sendEvent1( void (_Listener:: * func)(_A1), _A1 a1)
65 {
66 typename std::list < _Listener *> ::iterator iter = m_listeners.begin();
67 while (iter != m_listeners.end())
68 {
69 _Listener * pListener = * iter;
70 ++ iter;
71 (pListener ->* func)(a1);
72 }
73 }
74
75 template < class _A1, class _A2 >
76 void sendEvent2( void (_Listener:: * func)(_A1, _A2), _A1 a1, _A2 a2)
77 {
78 typename std::list < _Listener *> ::iterator iter = m_listeners.begin();
79 while (iter != m_listeners.end())
80 {
81 _Listener * pListener = * iter;
82 ++ iter;
83 (pListener ->* func)(a1, a2);
84 }
85 }
86
87 private :
88 std::list < _Listener *> m_listeners;
89 };
90
91 class ICalcEventListener
92 {
93 public :
94 virtual void onCalcBegin(ICalcEventSource * pSender) = 0 ;
95 };
96
97 class ICalcEventSource
98 {
99 public :
100 virtual bool addListener(ICalcEventListener * pListener) = 0 ;
101 virtual bool removeListener(ICalcEventListener * pListener) = 0 ;
102 };
103
104 class CMyPrivateCalcView_1 : ICalcEventListener
105 {
106 public :
107 virtual void onCalcBegin(ICalcEventSource * pSender)
108 {
109 cout << " CMyPrivateCalcView_1 receive CalcBegin message " << endl;
110 }
111
112 void init(ICalcEventSource * pSource)
113 {
114 pSource -> addListener( this );
115 }
116 };
117
118 class CMyPrivateCalcView_2 : ICalcEventListener
119 {
120 public :
121 virtual void onCalcBegin(ICalcEventSource * pSender)
122 {
123 cout << " CMyPrivateCalcView_2 receive CalcBegin message " << endl;
124 }
125
126 void init(ICalcEventSource * pSource)
127 {
128 pSource -> addListener( this );
129 }
130 };
131
132 class CMyPrivateCalc : public ICalcEventSource
133 , public EventSourceMulticaster < ICalcEventListener >
134 {
135 public :
136 virtual bool addListener(ICalcEventListener * pListener)
137 {
138 return addListenerInternal(pListener);
139 }
140
141 virtual bool removeListener(ICalcEventListener * pListener)
142 {
143 return removeListenerInternal(pListener);
144 }
145
146 void calcBegin()
147 {
148 sendEvent1( & ICalcEventListener::onCalcBegin, static_cast < ICalcEventSource *> ( this ));
149 }
150 };
151 //////////////////////////////////////////////////////////////////////// //
152 int _tmain( int argc, _TCHAR * argv[])
153 {
154 CMyPrivateCalc * pCalc = new CMyPrivateCalc;
155 CMyPrivateCalcView_1 * pView_1 = new CMyPrivateCalcView_1;
156 pView_1 -> init(pCalc);
157 CMyPrivateCalcView_2 * pView_2 = new CMyPrivateCalcView_2;
158 pView_2 -> init(pCalc);
159
160 pCalc -> calcBegin();
161 }
2 CopyRight: (C) 2011 by caipan
3 Email: [email protected]
4 */
5 #include < iostream >
6 #include < list >
7 class ICalcEventSource;
8
9 template < class _Listener >
10 class EventSourceMulticaster
11 {
12 public :
13 ~ EventSourceMulticaster()
14 {
15 m_listeners.clear();
16 }
17
18 bool addListenerInternal(_Listener * pListener)
19 {
20 for (typename std::list < _Listener *> ::iterator i = m_listeners.begin(); i != m_listeners.end(); i ++ )
21 {
22 if ( * i == pListener)
23 {
24 return false ;
25 }
26 }
27
28 m_listeners.push_back(pListener);
29
30 return true ;
31 }
32
33 bool removeListenerInternal(_Listener * pListener)
34 {
35 if (pListener == NULL)
36 {
37 return false ;
38 }
39
40 for (typename std::list < _Listener *> ::iterator i = m_listeners.begin(); i != m_listeners.end(); i ++ )
41 {
42 if ( * i == pListener)
43 {
44 m_listeners.erase(i);
45 return true ;
46 }
47 }
48
49 return false ;
50 }
51
52 void sendEvent( void (_Listener:: * func)())
53 {
54 typename std::list < _Listener *> ::iterator iter = m_listeners.begin();
55 while (iter != m_listeners.end())
56 {
57 _Listener * pListener = * iter;
58 ++ iter;
59 (pListener ->* func)();
60 }
61 }
62
63 template < class _A1 >
64 void sendEvent1( void (_Listener:: * func)(_A1), _A1 a1)
65 {
66 typename std::list < _Listener *> ::iterator iter = m_listeners.begin();
67 while (iter != m_listeners.end())
68 {
69 _Listener * pListener = * iter;
70 ++ iter;
71 (pListener ->* func)(a1);
72 }
73 }
74
75 template < class _A1, class _A2 >
76 void sendEvent2( void (_Listener:: * func)(_A1, _A2), _A1 a1, _A2 a2)
77 {
78 typename std::list < _Listener *> ::iterator iter = m_listeners.begin();
79 while (iter != m_listeners.end())
80 {
81 _Listener * pListener = * iter;
82 ++ iter;
83 (pListener ->* func)(a1, a2);
84 }
85 }
86
87 private :
88 std::list < _Listener *> m_listeners;
89 };
90
91 class ICalcEventListener
92 {
93 public :
94 virtual void onCalcBegin(ICalcEventSource * pSender) = 0 ;
95 };
96
97 class ICalcEventSource
98 {
99 public :
100 virtual bool addListener(ICalcEventListener * pListener) = 0 ;
101 virtual bool removeListener(ICalcEventListener * pListener) = 0 ;
102 };
103
104 class CMyPrivateCalcView_1 : ICalcEventListener
105 {
106 public :
107 virtual void onCalcBegin(ICalcEventSource * pSender)
108 {
109 cout << " CMyPrivateCalcView_1 receive CalcBegin message " << endl;
110 }
111
112 void init(ICalcEventSource * pSource)
113 {
114 pSource -> addListener( this );
115 }
116 };
117
118 class CMyPrivateCalcView_2 : ICalcEventListener
119 {
120 public :
121 virtual void onCalcBegin(ICalcEventSource * pSender)
122 {
123 cout << " CMyPrivateCalcView_2 receive CalcBegin message " << endl;
124 }
125
126 void init(ICalcEventSource * pSource)
127 {
128 pSource -> addListener( this );
129 }
130 };
131
132 class CMyPrivateCalc : public ICalcEventSource
133 , public EventSourceMulticaster < ICalcEventListener >
134 {
135 public :
136 virtual bool addListener(ICalcEventListener * pListener)
137 {
138 return addListenerInternal(pListener);
139 }
140
141 virtual bool removeListener(ICalcEventListener * pListener)
142 {
143 return removeListenerInternal(pListener);
144 }
145
146 void calcBegin()
147 {
148 sendEvent1( & ICalcEventListener::onCalcBegin, static_cast < ICalcEventSource *> ( this ));
149 }
150 };
151 //////////////////////////////////////////////////////////////////////// //
152 int _tmain( int argc, _TCHAR * argv[])
153 {
154 CMyPrivateCalc * pCalc = new CMyPrivateCalc;
155 CMyPrivateCalcView_1 * pView_1 = new CMyPrivateCalcView_1;
156 pView_1 -> init(pCalc);
157 CMyPrivateCalcView_2 * pView_2 = new CMyPrivateCalcView_2;
158 pView_2 -> init(pCalc);
159
160 pCalc -> calcBegin();
161 }