• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            阿攀的博客

            海闊天空

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              5 隨筆 :: 2 文章 :: 11 評論 :: 0 Trackbacks
            相信大家對Observer模式都比較了解,在開發(fā)過程中這個模式經(jīng)常被使用。
            下面,和大家分享下我的實現(xiàn),我就直接貼代碼了,希望對閱讀過的朋友有幫助。
            個人認為,其中的模板類比較重要,具有一定的復(fù)用性。如模板類中的函數(shù)不滿足要求,就修改、添加吧。
              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 阿攀 閱讀(1901) 評論(3)  編輯 收藏 引用 所屬分類: 設(shè)計模式

            評論

            # re: 一個簡單的Observer實現(xiàn) 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均支持,免去各種參數(shù)數(shù)目的SendEvent重載,而且就算您不用lambda expression,std::function還支持很多種類型的函數(shù)指針譬如成員函數(shù)指針等等。應(yīng)有盡有,任君選擇。  回復(fù)  更多評論
              

            # re: 一個簡單的Observer實現(xiàn) 2011-04-29 17:28 Apan
            @陳梓瀚(vczh)
            謝謝你的提醒!很敬佩你的技術(shù)。  回復(fù)  更多評論
              

            # re: 一個簡單的Observer實現(xiàn) 2011-06-11 12:00 egmkang
            之前看過你的blog,這兩天也有類似的需求,自己用boost::function寫了一個(蛋疼了一下)
            你看看有啥不妥的么
            http://www.cnblogs.com/egmkang/archive/2011/06/11/CPP_Delegate_And_Event.html  回復(fù)  更多評論
              


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            中文字幕亚洲综合久久菠萝蜜| 午夜视频久久久久一区 | 亚洲AV成人无码久久精品老人 | 国产精品久久久久久搜索| 久久精品免费一区二区| 亚洲精品蜜桃久久久久久| 久久精品国产第一区二区三区 | 久久久久亚洲AV无码专区桃色| 久久有码中文字幕| 久久久久久国产精品美女| 99精品国产在热久久| 国内精品欧美久久精品| 久久久久久久波多野结衣高潮| 久久ww精品w免费人成| 久久久久久亚洲精品不卡| 亚洲精品乱码久久久久久自慰| 97久久精品人人澡人人爽| 中文字幕无码久久久| 一本大道久久a久久精品综合| 无码任你躁久久久久久老妇 | 99精品久久精品一区二区| 亚洲人AV永久一区二区三区久久| 久久久亚洲欧洲日产国码二区| 久久久中文字幕日本| 91久久婷婷国产综合精品青草| 色综合久久久久综合99| 大美女久久久久久j久久| 久久久久亚洲av综合波多野结衣| 午夜不卡888久久| 日韩精品久久久久久免费| 无码任你躁久久久久久久| 一本久久a久久精品综合夜夜| 亚洲va国产va天堂va久久| 午夜视频久久久久一区| 国产精品久久久久一区二区三区| 热re99久久精品国99热| 国产色综合久久无码有码| 噜噜噜色噜噜噜久久| 一本色综合久久| 伊人久久大香线蕉精品不卡| 国产精品丝袜久久久久久不卡|