• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              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 阿攀 閱讀(1907) 評論(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  回復  更多評論
              

            99久久99久久精品国产片果冻| av无码久久久久不卡免费网站| 国产精品嫩草影院久久| 亚洲国产精品热久久| 亚洲国产日韩欧美久久| 色偷偷88888欧美精品久久久| 2020最新久久久视精品爱| 亚洲欧美久久久久9999| 99国产精品久久久久久久成人热| 久久久艹| 久久久国产乱子伦精品作者| 久久精品无码免费不卡| 久久AV高清无码| 中文字幕久久精品| 四虎国产永久免费久久| 国产精品久久久久a影院| 亚洲国产成人久久综合碰碰动漫3d | 色综合色天天久久婷婷基地| 亚洲国产天堂久久综合| 久久综合久久综合九色| 日产精品99久久久久久| 18禁黄久久久AAA片| 久久久精品波多野结衣| 99久久免费国产精品| 国产精品久久自在自线观看| 国产激情久久久久久熟女老人| 精品99久久aaa一级毛片| 久久被窝电影亚洲爽爽爽| 嫩草伊人久久精品少妇AV| 久久精品国产2020| 亚洲Av无码国产情品久久| 污污内射久久一区二区欧美日韩| 久久精品免费一区二区三区| 久久久91精品国产一区二区三区| 久久久久亚洲av无码专区导航| 无码AV波多野结衣久久| 亚洲成色WWW久久网站| 亚洲va中文字幕无码久久| 久久精品水蜜桃av综合天堂| 久久久精品国产sm调教网站| 国产欧美一区二区久久|