设计模式之观察者模式的C++实现

当一个对象改变时需要同时通知其他多个对象,或者说一个对象依赖另一个对象的时候,使用观察者模式可以将这两者封装在独立的对象中使他们各自独立地改变和复用。

观察者模式所做的工作就是在接触耦合,让相互耦合的双方都依赖于抽象而不是具体类,从而使得各自的改变都不会影响到对方。(引用自 《大话设计模式》)

下面提供一个观察者模式的C++实现版本:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #pragma once  
  2. #include <assert.h>  
  3. #include <iostream>  
  4. #include <list>  
  5. #include <string>  
  6. using std::list;  
  7. using std::cout;  
  8. using std::endl;  
  9. using std::string;  
  10.   
  11. //观察者需要通知者当前的状态信息,因此需要保存INotify指针  
  12. class INotify;  //提前声明  
  13. class IObserver  
  14. {  
  15. public:  
  16.     IObserver(const char* pName, INotify* pNotify)  
  17.         : m_strName(pName)  
  18.         , m_pNotify(pNotify)  
  19.     {  
  20.   
  21.     }  
  22.     virtual void    Update() = 0;  
  23. protected:  
  24.     string      m_strName;  
  25.     INotify*    m_pNotify;  
  26. };  
  27.   
  28. class INotify  
  29. {  
  30. public:  
  31.     virtual ~INotify()  
  32.     {  
  33.         list<IObserver*>::iterator itor = m_pObserverList.begin();  
  34.         IObserver* pObs = NULL;  
  35.         while( itor != m_pObserverList.end() )  
  36.         {  
  37.             pObs = *itor;  
  38.             delete pObs;  
  39.             itor++;  
  40.         }  
  41.         m_pObserverList.clear();  
  42.     }  
  43.     virtual void Add(IObserver* pObs)  
  44.     {  
  45.         assert(pObs);  
  46.         m_pObserverList.push_back(pObs);  
  47.     }  
  48.     virtual bool Remove(IObserver* pObs)  
  49.     {  
  50.         assert(pObs);  
  51.         list<IObserver*>::iterator itor = m_pObserverList.begin();  
  52.         while( itor != m_pObserverList.end() )  
  53.         {  
  54.             IObserver* pFind = *itor;  
  55.             if ( pFind == pObs )  
  56.                 return true;  
  57.             itor++;  
  58.         }  
  59.         return false;  
  60.     }  
  61.     virtual void Notify()  
  62.     {  
  63.         list<IObserver*>::iterator itor = m_pObserverList.begin();  
  64.         while( itor != m_pObserverList.end() )  
  65.         {  
  66.             IObserver* pObs = *itor;  
  67.             pObs->Update();  
  68.             itor++;  
  69.         }  
  70.     }  
  71.     const string&   GetStatus()const { return m_strStatus; }  
  72.     void            SetStatus(const string& strStatus)  { m_strStatus = strStatus; }  
  73. private:  
  74.     list<IObserver*>  m_pObserverList;  
  75.     string  m_strStatus;  
  76. };  
  77.   
  78. //试图通知类,通知所有视图更新状态  
  79. class CViewNotify  
  80.     : public INotify  
  81. {  
  82. public:  
  83.     //做一些独立的工作  
  84. };  
  85.   
  86. class CFrameNotify  
  87.     : public INotify  
  88. {  
  89. public:  
  90.     //做一些独立的工作  
  91. };  
  92.   
  93. class CScrollView  
  94.     : public IObserver  
  95. {  
  96. public:  
  97.     CScrollView(const char* pName, INotify* pNotify)  
  98.         :IObserver(pName, pNotify)  
  99.     {  
  100.   
  101.     }  
  102.     virtual void Update()  
  103.     {  
  104.         cout<<m_strName<<" need update now , the status is "<<m_pNotify->GetStatus()<<endl;  
  105.     }  
  106. };  
  107.   
  108. class CEditView  
  109.     : public IObserver  
  110. {  
  111. public:  
  112.     CEditView(const char* pName, INotify* pNotify)  
  113.         :IObserver(pName, pNotify)  
  114.     {  
  115.   
  116.     }  
  117.     virtual void Update()  
  118.     {  
  119.         cout<<m_strName<<" need update now , the status is "<<m_pNotify->GetStatus()<<endl;  
  120.     }  
  121. };  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值