阿攀的博客

海阔天空

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  5 随笔 :: 2 文章 :: 11 评论 :: 0 Trackbacks
相信大家对Observer模式都比较了解,在开发过程中这个模式经常被使用。
下面,和大家分享下我的实现,我就直接贴代码了,希望对阅读过的朋友有帮助。
个人认为,其中的模板类比较重要,具有一定的复用性。如模板类中的函数不满足要求,就修改、添加吧。
  1 /**
  2    CopyRight:  (C)  2011 by caipan
  3    Email:      caipan0206@163.com
  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 (*== 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 (*== 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 }
posted on 2011-04-29 14:49 阿攀 阅读(1846) 评论(3)  编辑 收藏 引用 所属分类: 设计模式

评论

# re: 一个简单的Observer实现 2011-04-29 15:19 陈梓瀚(vczh)
使用新的stl可以写成
SendEvent(const std::function<void(_Listener*)>& listener);
然后就有
SendEvent([](MyListener* listener){listener->DoSomething(a, b, c);});

VC++2010以及新版gcc均支持,免去各种参数数目的SendEvent重载,而且就算您不用lambda expression,std::function还支持很多种类型的函数指针譬如成员函数指针等等。应有尽有,任君选择。  回复  更多评论
  

# re: 一个简单的Observer实现 2011-04-29 17:28 Apan
@陈梓瀚(vczh)
谢谢你的提醒!很敬佩你的技术。  回复  更多评论
  

# re: 一个简单的Observer实现 2011-06-11 12:00 egmkang
之前看过你的blog,这两天也有类似的需求,自己用boost::function写了一个(蛋疼了一下)
你看看有啥不妥的么
http://www.cnblogs.com/egmkang/archive/2011/06/11/CPP_Delegate_And_Event.html  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理