c++設計模式之Observer觀察者模式
阿新 • • 發佈:2019-01-27
設計模式C++學習筆記之十六(Observer觀察者模式)
16.1.解釋
概念:定義物件間的一種一對多的依賴關係,當一個物件的狀態發生改變時,所有依賴於它的物件都得到通知並被自動更新。
main(),
IObservable,被觀察者介面
CHanFeiZiObservable,被觀察者韓非子
IObserver,觀察者介面
CLiSiObserver,觀察者李斯
CZhouSiObserver觀察者周斯
說明:將觀察者聚集到被觀察者韓非子身邊,韓非子的每一個舉動都會通知給觀察者,如李斯或周斯。
注意:最多允許一個物件既是觀察者也是被觀察者。就像資料庫中的觸發器一樣,成為一個複雜的鏈就很難維護了。觀察者類似於委託的處理方式。
//IObservable.h
#pragma once
#include "IObserver.h"
#include <iostream>
using std::string;
class IObservable
{
public:
IObservable(void)
{
}
virtual ~IObservable(void)
{
}
virtual void AddObserver(IObserver *pObserver) = 0;
virtual void DeleteObserver(IObserver *pObserver) = 0;
virtual void NotifyObservers(string context) = 0;
};
//HanFeiziObservable.h
#pragma once
#include "iobservable.h"
#include "IObserver.h"
#include <vector>
using std::vector;
class CHanFeiziObservable :
public IObservable
{
public:
CHanFeiziObservable(void);
~CHanFeiziObservable(void);
void AddObserver(IObserver *pObserver);
void DeleteObserver(IObserver *pObserver);
void NotifyObservers(string context);
void HaveBreakfast();
void HaveFun();
private:
vector<IObserver*> m_observerList;
typedef vector<IObserver*>::const_iterator ObserverList_C_iterator;
};
//HanFeiziObservable.cpp
#include "StdAfx.h"
#include "HanFeiziObservable.h"
#include <iostream>
using std::string;
using std::cout;
using std::endl;
CHanFeiziObservable::CHanFeiziObservable(void)
{
}
CHanFeiziObservable::~CHanFeiziObservable(void)
{
}
void CHanFeiziObservable::AddObserver( IObserver *pObserver )
{
m_observerList.push_back(pObserver);
}
void CHanFeiziObservable::DeleteObserver( IObserver *pObserver )
{
ObserverList_C_iterator it = m_observerList.begin();
for (; it != m_observerList.end(); it++)
{
string name = (*it)->GetName();
if (name.compare(pObserver->GetName()) == 0)
{
//找到了刪除。
}
}
}
void CHanFeiziObservable::NotifyObservers( string context )
{
ObserverList_C_iterator it = m_observerList.begin();
for (; it != m_observerList.end(); it ++)
{
(*it)->Update(context);
}
}
void CHanFeiziObservable::HaveBreakfast()
{
cout << "韓非子:開始吃飯了..." << endl;
this->NotifyObservers("韓非子在吃飯");
}
void CHanFeiziObservable::HaveFun()
{
cout << "韓非子:開始娛樂了..." << endl;
this->NotifyObservers("韓非子在娛樂");
}
//IObserver.h
#pragma once
#include <iostream>
using std::string;
class IObserver
{
public:
IObserver(string _name)
{
this->m_name = _name;
}
virtual ~IObserver(void)
{
}
virtual void Update(string context) = 0;
virtual string GetName() = 0;//為c++單獨增加的函式,用於刪除時查詢觀察者。
protected:
string m_name;
};
//LiSiObserver.h
#pragma once
#include "iobserver.h"
#include <iostream>
using std::string;
class CLiSiObserver :
public IObserver
{
public:
CLiSiObserver(void);
~CLiSiObserver(void);
void Update(string context);
string GetName();
private:
void ReportToQinShiHuang(string report);
};
//LiSiObserver.cpp
#include "StdAfx.h"
#include "LiSiObserver.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;
CLiSiObserver::CLiSiObserver(void) : IObserver("李斯")
{
}
CLiSiObserver::~CLiSiObserver(void)
{
}
void CLiSiObserver::Update( string context )
{
cout << "李斯:觀察到韓非子活動,開始向老闆彙報了..." << endl;
this->ReportToQinShiHuang(context);
cout << "李斯:彙報完畢,秦老闆賞給他兩個蘿蔔吃吃..." << endl;
}
void CLiSiObserver::ReportToQinShiHuang( string report )
{
cout << "李斯:報告,秦老闆!韓非子有活動了--->" << report.c_str() << endl;
}
string CLiSiObserver::GetName()
{
return m_name;
}
//ZhouSiObserver.h
#pragma once
#include "iobserver.h"
#include <iostream>
using std::string;
class CZhouSiObserver :
public IObserver
{
public:
CZhouSiObserver(void);
~CZhouSiObserver(void);
void Update(string context);
string GetName();
private:
void Cry(string report);
};
//ZhouSiObserver.cpp
#include "StdAfx.h"
#include "ZhouSiObserver.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;
CZhouSiObserver::CZhouSiObserver(void) : IObserver("周斯")
{
}
CZhouSiObserver::~CZhouSiObserver(void)
{
}
void CZhouSiObserver::Update( string context )
{
cout << "周斯:觀察到韓非子活動,自己也開始活動了..." << endl;
this->Cry(context);
cout << "周斯:真真的哭列了..." << endl;
}
void CZhouSiObserver::Cry( string report )
{
cout << "周斯:為因" << report.c_str() << ", ————所以我悲傷呀!" << endl;
}
string CZhouSiObserver::GetName()
{
return m_name;
}
// Observer.cpp
#include "stdafx.h"
#include "HanFeiZi.h"
#include "LiSi.h"
#include "HanFeiZiNew.h"
#include "HanFeiziObservable.h"
#include "LiSiObserver.h"
#include "ZhouSiObserver.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;
void DoNew()
{
//IHanFeiZi.h, HanFeiZiNew.h, ILiSi.h, LiSi.h
// cout << "----------用新的方法試試----------" << endl;
//CHanFeiZiNew hanfeizi;
//hanfeizi.HaveBreakfast();
//hanfeizi.HaveFun();
}
void DoNewNew()
{
//IObservable.h, HanfeiziObservable.h, IObserver.h, LiSiObserver.h
cout << "----------用更新的方法再試試----------" << endl;
IObserver *pLiSi = new CLiSiObserver();
IObserver *pZhouSi = new CZhouSiObserver();
CHanFeiziObservable *pHanFeiZi = new CHanFeiziObservable();
pHanFeiZi->AddObserver(pLiSi);
pHanFeiZi->AddObserver(pZhouSi);
pHanFeiZi->HaveBreakfast();
delete pLiSi;
pLiSi = NULL;
delete pHanFeiZi;
pHanFeiZi = NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
//比較原始的方法,用執行緒來觀察。
//DoIt();
//把李斯這個類聚集到韓非子這個類上,這樣的話耦合度太高了,還是用更抽象的方式。
DoNew();
//更抽象的方式,想要觀察韓非子的人多了去了,不可能只允許李斯觀察。
DoNewNew();
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
_CrtDumpMemoryLeaks();
return 0;
}
16.1.解釋
概念:定義物件間的一種一對多的依賴關係,當一個物件的狀態發生改變時,所有依賴於它的物件都得到通知並被自動更新。
main(),
IObservable,被觀察者介面
CHanFeiZiObservable,被觀察者韓非子
IObserver,觀察者介面
CLiSiObserver,觀察者李斯
CZhouSiObserver觀察者周斯
說明:將觀察者聚集到被觀察者韓非子身邊,韓非子的每一個舉動都會通知給觀察者,如李斯或周斯。
注意:最多允許一個物件既是觀察者也是被觀察者。就像資料庫中的觸發器一樣,成為一個複雜的鏈就很難維護了。觀察者類似於委託的處理方式。
//IObservable.h
#pragma once
#include "IObserver.h"
#include <iostream>
using std::string;
class IObservable
{
public:
IObservable(void)
{
}
virtual ~IObservable(void)
{
}
virtual void AddObserver(IObserver *pObserver) = 0;
virtual void DeleteObserver(IObserver *pObserver) = 0;
virtual void NotifyObservers(string context) = 0;
};
//HanFeiziObservable.h
#pragma once
#include "iobservable.h"
#include "IObserver.h"
#include <vector>
using std::vector;
class CHanFeiziObservable :
public IObservable
{
public:
CHanFeiziObservable(void);
~CHanFeiziObservable(void);
void AddObserver(IObserver *pObserver);
void DeleteObserver(IObserver *pObserver);
void NotifyObservers(string context);
void HaveBreakfast();
void HaveFun();
private:
vector<IObserver*> m_observerList;
typedef vector<IObserver*>::const_iterator ObserverList_C_iterator;
};
//HanFeiziObservable.cpp
#include "StdAfx.h"
#include "HanFeiziObservable.h"
#include <iostream>
using std::string;
using std::cout;
using std::endl;
CHanFeiziObservable::CHanFeiziObservable(void)
{
}
CHanFeiziObservable::~CHanFeiziObservable(void)
{
}
void CHanFeiziObservable::AddObserver( IObserver *pObserver )
{
m_observerList.push_back(pObserver);
}
void CHanFeiziObservable::DeleteObserver( IObserver *pObserver )
{
ObserverList_C_iterator it = m_observerList.begin();
for (; it != m_observerList.end(); it++)
{
string name = (*it)->GetName();
if (name.compare(pObserver->GetName()) == 0)
{
//找到了刪除。
}
}
}
void CHanFeiziObservable::NotifyObservers( string context )
{
ObserverList_C_iterator it = m_observerList.begin();
for (; it != m_observerList.end(); it ++)
{
(*it)->Update(context);
}
}
void CHanFeiziObservable::HaveBreakfast()
{
cout << "韓非子:開始吃飯了..." << endl;
this->NotifyObservers("韓非子在吃飯");
}
void CHanFeiziObservable::HaveFun()
{
cout << "韓非子:開始娛樂了..." << endl;
this->NotifyObservers("韓非子在娛樂");
}
//IObserver.h
#pragma once
#include <iostream>
using std::string;
class IObserver
{
public:
IObserver(string _name)
{
this->m_name = _name;
}
virtual ~IObserver(void)
{
}
virtual void Update(string context) = 0;
virtual string GetName() = 0;//為c++單獨增加的函式,用於刪除時查詢觀察者。
protected:
string m_name;
};
//LiSiObserver.h
#pragma once
#include "iobserver.h"
#include <iostream>
using std::string;
class CLiSiObserver :
public IObserver
{
public:
CLiSiObserver(void);
~CLiSiObserver(void);
void Update(string context);
string GetName();
private:
void ReportToQinShiHuang(string report);
};
//LiSiObserver.cpp
#include "StdAfx.h"
#include "LiSiObserver.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;
CLiSiObserver::CLiSiObserver(void) : IObserver("李斯")
{
}
CLiSiObserver::~CLiSiObserver(void)
{
}
void CLiSiObserver::Update( string context )
{
cout << "李斯:觀察到韓非子活動,開始向老闆彙報了..." << endl;
this->ReportToQinShiHuang(context);
cout << "李斯:彙報完畢,秦老闆賞給他兩個蘿蔔吃吃..." << endl;
}
void CLiSiObserver::ReportToQinShiHuang( string report )
{
cout << "李斯:報告,秦老闆!韓非子有活動了--->" << report.c_str() << endl;
}
string CLiSiObserver::GetName()
{
return m_name;
}
//ZhouSiObserver.h
#pragma once
#include "iobserver.h"
#include <iostream>
using std::string;
class CZhouSiObserver :
public IObserver
{
public:
CZhouSiObserver(void);
~CZhouSiObserver(void);
void Update(string context);
string GetName();
private:
void Cry(string report);
};
//ZhouSiObserver.cpp
#include "StdAfx.h"
#include "ZhouSiObserver.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;
CZhouSiObserver::CZhouSiObserver(void) : IObserver("周斯")
{
}
CZhouSiObserver::~CZhouSiObserver(void)
{
}
void CZhouSiObserver::Update( string context )
{
cout << "周斯:觀察到韓非子活動,自己也開始活動了..." << endl;
this->Cry(context);
cout << "周斯:真真的哭列了..." << endl;
}
void CZhouSiObserver::Cry( string report )
{
cout << "周斯:為因" << report.c_str() << ", ————所以我悲傷呀!" << endl;
}
string CZhouSiObserver::GetName()
{
return m_name;
}
// Observer.cpp
#include "stdafx.h"
#include "HanFeiZi.h"
#include "LiSi.h"
#include "HanFeiZiNew.h"
#include "HanFeiziObservable.h"
#include "LiSiObserver.h"
#include "ZhouSiObserver.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;
void DoNew()
{
//IHanFeiZi.h, HanFeiZiNew.h, ILiSi.h, LiSi.h
// cout << "----------用新的方法試試----------" << endl;
//CHanFeiZiNew hanfeizi;
//hanfeizi.HaveBreakfast();
//hanfeizi.HaveFun();
}
void DoNewNew()
{
//IObservable.h, HanfeiziObservable.h, IObserver.h, LiSiObserver.h
cout << "----------用更新的方法再試試----------" << endl;
IObserver *pLiSi = new CLiSiObserver();
IObserver *pZhouSi = new CZhouSiObserver();
CHanFeiziObservable *pHanFeiZi = new CHanFeiziObservable();
pHanFeiZi->AddObserver(pLiSi);
pHanFeiZi->AddObserver(pZhouSi);
pHanFeiZi->HaveBreakfast();
delete pLiSi;
pLiSi = NULL;
delete pHanFeiZi;
pHanFeiZi = NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
//比較原始的方法,用執行緒來觀察。
//DoIt();
//把李斯這個類聚集到韓非子這個類上,這樣的話耦合度太高了,還是用更抽象的方式。
DoNew();
//更抽象的方式,想要觀察韓非子的人多了去了,不可能只允許李斯觀察。
DoNewNew();
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
_CrtDumpMemoryLeaks();
return 0;
}