• <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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              5 隨筆 :: 2 文章 :: 11 評(píng)論 :: 0 Trackbacks
            相信大家對(duì)Observer模式都比較了解,在開發(fā)過程中這個(gè)模式經(jīng)常被使用。
            下面,和大家分享下我的實(shí)現(xiàn),我就直接貼代碼了,希望對(duì)閱讀過的朋友有幫助。
            個(gè)人認(rè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 阿攀 閱讀(1906) 評(píng)論(3)  編輯 收藏 引用 所屬分類: 設(shè)計(jì)模式

            評(píng)論

            # re: 一個(gè)簡(jiǎn)單的Observer實(shí)現(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ù)  更多評(píng)論
              

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

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


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


            99久久精品免费观看国产| 无码国产69精品久久久久网站| 99久久中文字幕| 亚洲综合精品香蕉久久网97 | 91精品无码久久久久久五月天| 青青草原综合久久大伊人精品| 一级a性色生活片久久无| 亚洲va久久久噜噜噜久久狠狠 | 久久99国产综合精品| 99热成人精品免费久久| 久久久久久国产a免费观看黄色大片| 性做久久久久久久| 久久99国产精品成人欧美| 亚洲精品午夜国产VA久久成人| 国产精品天天影视久久综合网| 欧美久久久久久| 国产免费久久精品99久久| 波多野结衣AV无码久久一区| 精品久久人人做人人爽综合| 久久这里只有精品18| 一97日本道伊人久久综合影院| 久久A级毛片免费观看| 久久无码高潮喷水| 久久久久久国产精品免费免费| 91视频国产91久久久| 一本色道久久HEZYO无码| 久久青青草原亚洲av无码| 久久精品国产精品国产精品污| 无码AV中文字幕久久专区| 香蕉久久夜色精品国产尤物| 久久精品亚洲男人的天堂| 久久99久久99小草精品免视看 | 日韩av无码久久精品免费| 香蕉久久夜色精品国产2020| 亚洲人成电影网站久久| 2021国产精品久久精品| 久久人人青草97香蕉| 久久精品国产亚洲αv忘忧草| 美女久久久久久| 久久久午夜精品| 国产精品乱码久久久久久软件|