??????根據(jù)四人幫解釋,觀察者模式解決當(dāng)某些其他對象改變狀態(tài)時(shí),如果一組對象要進(jìn)行更新,那么將這兩個(gè)對象分別封裝在兩個(gè)獨(dú)立的對象中。
??????個(gè)人理解,在觀察者模式中,最重要的要理解哪個(gè)對象是主動(dòng)的,也就是事件源,那么觀察者模式就很簡單了。
??????以下是我實(shí)現(xiàn)偵聽器模式,可以生成廣播信息。
??????代碼如下:
??????頭文件如下:
/*! @file?? Liserner.h??
\brief
?文件功能:?????? 偵聽器模式???????????????????????????????????? <br>
????????????????????????????????????????????????????? <br>
?文件名稱:?? Liserner.h???????????????????????????????????????????????????????? <br>
?模塊名? :?????????????????????????????????????????????????????? <br>
\author????????? wenglj
\version????? 1.0.1
\remark??????????????????????????????????????????????????????????????????????? <br>
修改記錄 :? <br>?
日 期 版本 修改人 修改內(nèi)容<br>
YYYY/MM/DD?? 1.0.0? ? <XXX> ? 創(chuàng)建? <br>
******************************************************************************** <br>
版權(quán):浙江傲雪工作室????? 保留所有權(quán)利
*******************************************************************************/
#pragma once
#include <vector>
using?std::vector;
/*! @class? abstract Event
********************************************************************************
\brief?
功能???? :???? 事件<br>
類名稱?? :??? abstract Event?
\exception??
\author???wenglj
\date???? 2006-9-23
\remark??? <br>
典型用法 :???
******************************************************************************/
class Event
{
public:
?enum?
?{
??ADD,
??DEL
?}TYPE;
?//?
?/*! @function? public pure virtual? OnAdd
?********************************************************************************
?\brief?
?功能??? :???????? 添加對象事件<br>
?函數(shù)名? :???? OnAdd
?\return void <insert return value description here>
?\exception?
?\author????? wenglj
?\remark??? <br>
?典型用法:?? <br>
?復(fù)雜度 :????????????????????????
?*******************************************************************************/
?virtual?void?OnAdd( void ) = 0;
?//
?/*! @function? public pure virtual? OnDel
?********************************************************************************
?\brief?
?功能??? :???對象刪除事件<br>
?函數(shù)名? :???OnDel
?\return void <insert return value description here>
?\exception?
?\author????wenglj
?\remark??? <br>
?典型用法:?? <br>
?復(fù)雜度 :????????????????????????
?*******************************************************************************/
?virtual?void?OnDel( void ) = 0;
};?//?Event
/*! @class? CDictograph
********************************************************************************
\brief?
功能???? :???? 偵聽器,事件源<br>
類名稱?? :??? CDictograph?
\exception??
\author???wenglj
\date???? 2006-9-23
\remark??? <br>
典型用法 :???
******************************************************************************/
class CDictograph
{
public:
?CDictograph(){};
?~CDictograph(){};
public:
?typedef?vector<Event*>?Liseners;
?typedef?vector<Event*>::iterator?LisenersIteraor;
?/*! @function? public? AddLisener
?********************************************************************************
?\brief?
?功能??? :??????? 添加偵聽對象<br>
?函數(shù)名? :???AddLisener
?@param? pLisener Event *? < 偵聽對象 >
?\return void <insert return value description here>
?\exception?
?\author????wenglj
?\remark??? <br>
?典型用法:?? <br>
?復(fù)雜度 :????????????????????????
?*******************************************************************************/
?void?AddLisener( Event* pLisener );
?/*! @function? public? DelLisener
?********************************************************************************
?\brief?
?功能??? :????? 刪除偵聽對象<br>
?函數(shù)名? :??DelLisener
?@param? pLisener Event *? < 偵聽對象 >
?\return void <insert return value description here>
?\exception?
?\author???wenglj
?\remark??? <br>
?典型用法:?? <br>
?復(fù)雜度 :????????????????????????
?*******************************************************************************/
?void?DelLisener( Event* pLisener );
?/*! @function? public? ToNotify
?********************************************************************************
?\brief?
?功能??? :???? 通知偵聽對象事件發(fā)生<br>
?函數(shù)名? :??? ToNotify
?@param? nCode int?? < 事件類型 >
?\return void <insert return value description here>
?\exception?
?\author???wenglj
?\remark??? <br>
?典型用法:?? <br>
?復(fù)雜度 :????????????????????????
?*******************************************************************************/
?void?ToNotify( int nCode );
?
protected :
?//?偵聽對象集合
?Liseners?m_vtLiseners;
};?//?CDictograph
/*! @class? CLisener
********************************************************************************
\brief?
功能???? :???? 偵聽實(shí)體, 事件的接受者<br>
類名稱?? :??? CLisener?
\exception??
\author???wenglj
\date???? 2006-9-23
\remark??? <br>
典型用法 :???
******************************************************************************/
class CLisener :?public?Event
{
public:
?CLisener(void);
?~CLisener(void);
public :
?//?
?/*! @function? public virtual? OnAdd
?********************************************************************************
?\brief?
?功能??? :??????????? 添加事件響應(yīng)???????????? <br>
?函數(shù)名? :
?\return void <insert return value description here>
?\exception?
?\author?????wenglj
?\remark??? <br>
?典型用法:?? <br>
?復(fù)雜度 :????????????????????????
?*******************************************************************************/
?virtual?void?OnAdd( void );
?//
?/*! @function? public virtual? OnDel
?********************************************************************************
?\brief?
?功能??? :??????? 刪除事件響應(yīng)<br>
?函數(shù)名? :
?\return void <insert return value description here>
?\exception?
?\author????wenglj
?\remark??? <br>
?典型用法:?? <br>
?復(fù)雜度 :????????????????????????
?*******************************************************************************/
?virtual?void?OnDel( void );
?
};?//CLiserner
/*! @class? CClient
********************************************************************************
\brief?
功能???? :???? 客戶對象, 觸發(fā)事件<br>
類名稱?? :??? CClient?
\exception??
\author???wenglj
\date???? 2006-9-23
\remark??? <br>
典型用法 :???
******************************************************************************/
class?CClient
{
public :
?typedef?vector<int>??Objects;
?typedef?vector<int>::iterator?ObjectsIterator;
?CClient();
?~CClient();
public :
?/*! @function? public? AddObject
?********************************************************************************
?\brief?
?功能??? :?????? 添加事件觸發(fā)????????????????? <br>
?函數(shù)名? :
?@param? i int?? < insert parameter description here>
?\return void <insert return value description here>
?\exception?
?\author???wenglj
?\remark??? <br>
?典型用法:?? <br>
?復(fù)雜度 :????????????????????????
?*******************************************************************************/
?void?AddObject( int i );
?/*! @function? public? DelObject
?********************************************************************************
?\brief?
?功能??? :????????? 刪除事件觸發(fā)?????????????? <br>
?函數(shù)名? :
?@param? i int?? < insert parameter description here>
?\return void <insert return value description here>
?\exception?
?\author????wenglj
?\remark??? <br>
?典型用法:?? <br>
?復(fù)雜度 :????????????????????????
?*******************************************************************************/
?void?DelObject( int i );
?/*! @function? public? SetLiseners
?********************************************************************************
?\brief?
?功能??? :???? 設(shè)置偵聽器??????????????????? <br>
?函數(shù)名? :
?@param? pObject CDictograph *? < insert parameter description here>
?\return void <insert return value description here>
?\exception?
?\author???wenglj
?\remark??? <br>
?典型用法:?? <br>
?復(fù)雜度 :????????????????????????
?*******************************************************************************/
?void?SetLiseners( CDictograph* pObject );
protected :
?Objects?m_vtObjects;
?CDictograph*?m_pDictograph;
};?//?CClient
實(shí)現(xiàn)文件如下:
/*! @file?? Liserner.cpp??
\brief
?文件功能:??????? 偵聽器模式實(shí)現(xiàn)??????????????????????????????????? <br>
????????????????????????????????????????????????????? <br>
?文件名稱:?? Liserner.cpp???????????????????????????????????????????????????????? <br>
?模塊名? :?????????????????????????????????????????????????????? <br>
\author????wenglj
\version?????
\remark??????????????????????????????????????????????????????????????????????? <br>
修改記錄 :? <br>?
日 期 版本 修改人 修改內(nèi)容<br>
YYYY/MM/DD?? 1.0.0? ? <XXX> ? 創(chuàng)建? <br>
******************************************************************************** <br>
版權(quán):浙江傲雪工作室????? 保留所有權(quán)利
*******************************************************************************/
#include "StdAfx.h"
#include ".\liserner.h"
#include <algorithm>
/*! @function? public? AddLisener
********************************************************************************
\brief?
功能??? :?????? 添加偵聽對象????????????????? <br>
函數(shù)名? :
@param? pLisener Event *? < insert parameter description here>
\return void <insert return value description here>
\exception?
\author???wenglj
\remark??? <br>
典型用法:?? <br>
復(fù)雜度 :????????????????????????
*******************************************************************************/
void?CDictograph::AddLisener( Event* pLisener )
{
?m_vtLiseners.push_back( pLisener );
?std::cout<<"Add Lisener Object"<<std::endl;
}
/*! @function? public? DelLisener
********************************************************************************
\brief?
功能??? :??????? 刪除偵聽對象??????????????? <br>
函數(shù)名? :
@param? pLisener Event *? < insert parameter description here>
\return void <insert return value description here>
\exception?
\author???wenglj
\remark??? <br>
典型用法:?? <br>
復(fù)雜度 :????????????????????????
*******************************************************************************/
void?CDictograph::DelLisener( Event* pLisener )
{
?LisenersIteraor?it;
?it?=?std::find( m_vtLiseners.begin(), m_vtLiseners.end(), pLisener );
?if ( it != m_vtLiseners.end() )
??m_vtLiseners.erase( it );
?std::cout<<"Delete Lisener Object"<<std::endl;
}
/*! @function? public? ToNotify
********************************************************************************
\brief?
功能??? :????? 通知偵聽對象?????????????????? <br>
函數(shù)名? :
@param? nCode int?? < insert parameter description here>
\return void <insert return value description here>
\exception?
\author???wenglj
\remark??? <br>
典型用法:?? <br>
復(fù)雜度 :????????????????????????
*******************************************************************************/
void?CDictograph::ToNotify( int nCode )
{
?std::cout<<"ToNotify Event"<<std::endl;
?for( LisenersIteraor it = m_vtLiseners.begin(); it != m_vtLiseners.end(); ++it )
?{
??Event*?p?=?*it;
??switch( nCode )
??{
??case Event::ADD:
???p->OnAdd();
???break;
??case Event::DEL:
???p->OnDel();
???break;
??default:
???break;
??}
??
?}
}
CLisener::CLisener(void)
{
}
CLisener::~CLisener(void)
{
}
/*! @function? public virtual? OnAdd
********************************************************************************
\brief?
功能??? :???? 添加事件響應(yīng)??????????????????? <br>
函數(shù)名? :
\return void <insert return value description here>
\exception?
\author??wenglj
\remark??? <br>
典型用法:?? <br>
復(fù)雜度 :????????????????????????
*******************************************************************************/
void?CLisener::OnAdd()
{
?std::cout<<"Add Object Event "<<std::endl;
}
/*! @function? public virtual? OnDel
********************************************************************************
\brief?
功能??? :?????? 刪除事件響應(yīng)????????????????? <br>
函數(shù)名? :
\return void <insert return value description here>
\exception?
\author???wenglj
\remark??? <br>
典型用法:?? <br>
復(fù)雜度 :????????????????????????
*******************************************************************************/
void?CLisener::OnDel()
{
?std::cout<<"Del Object Event"<<std::endl;
}
CClient::CClient()
:?m_pDictograph( NULL )
{
}
CClient::~CClient()
{
}
/*! @function? public? AddObject
********************************************************************************
\brief?
功能??? :??? 觸發(fā)添加事件???????????????????? <br>
函數(shù)名? :
@param? i int?? < insert parameter description here>
\return void <insert return value description here>
\exception?
\author??? wenglj
\remark??? <br>
典型用法:?? <br>
復(fù)雜度 :????????????????????????
*******************************************************************************/
void?CClient::AddObject( int i )
{
?m_vtObjects.push_back( i );
?std::cout<<"CClient Object AddObject"<<std::endl;
?m_pDictograph->ToNotify( Event::ADD );
?
}
/*! @function? public? DelObject
********************************************************************************
\brief?
功能??? :????? 觸發(fā)刪除事件?????????????????? <br>
函數(shù)名? :
@param? i int?? < insert parameter description here>
\return void <insert return value description here>
\exception?
\author???wenglj
\remark??? <br>
典型用法:?? <br>
復(fù)雜度 :????????????????????????
*******************************************************************************/
void?CClient::DelObject( int i )
{
?ObjectsIterator?it;
?it?=?std::find( m_vtObjects.begin(), m_vtObjects.end(), i );
?if ( it != m_vtObjects.end() )
?{
??m_vtObjects.erase( it );
??std::cout<<"CClient Object DelObject"<<std::endl;
??m_pDictograph->ToNotify( Event::DEL );
??
?}
}
/*! @function? public? SetLiseners
********************************************************************************
\brief?
功能??? :?????? 設(shè)置偵聽器對象????????????????? <br>
函數(shù)名? :
@param? pObject CDictograph *? < insert parameter description here>
\return void <insert return value description here>
\exception?
\author???wenglj
\remark??? <br>
典型用法:?? <br>
復(fù)雜度 :????????????????????????
*******************************************************************************/
void?CClient::SetLiseners( CDictograph* pObject )
{
?m_pDictograph?=?pObject;
}
主程序如下:
???// DictaphonePattern.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include "Liserner.h"
/*! @function? global? _tmain
********************************************************************************
\brief?
功能??? :??? 主程序???????????????????? <br>
函數(shù)名? :
@param? argc int?? < insert parameter description here>
@param? argv _TCHAR *[]? < insert parameter description here>
\return int <insert return value description here>
\exception?
\author??wenglj
\remark??? <br>
典型用法:?? <br>
復(fù)雜度 :????????????????????????
*******************************************************************************/
int _tmain(int argc, _TCHAR* argv[])
{
?CClient?clientObject;
?CDictograph?liseners;
?CLisener?lisener;
?liseners.AddLisener( &lisener );
?clientObject.SetLiseners( &liseners ) ;
?clientObject.AddObject( 1 );
?clientObject.AddObject( 2 );
?clientObject.AddObject( 3 );
?clientObject.AddObject( 4 );
?clientObject.AddObject( 5 );
?clientObject.DelObject( 3 );
?return 0;
}
具體的函數(shù)實(shí)現(xiàn)解釋都有注釋了。