You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.0 KiB
C++

#include "StdAfx.h"
#include "Subject.h"
CSubject::CSubject(void)
{
}
CSubject::~CSubject(void)
{
}
//注册观察者
void CSubject::Attach(CObserver *p)
{
m_ObserverVec.push_back(p);
}
//删除观察者
void CSubject::Delete(CObserver *p)
{
vector<CObserver*>::iterator iter = m_ObserverVec.begin();
vector<CObserver*>::iterator iter_end = m_ObserverVec.end();
for(;iter!=iter_end;iter++)
{
if((*iter) == p)
{
m_ObserverVec.erase(iter);
break;
}
}
}
//通知所有观察者更新
void CSubject::NotifyObservers()
{
vector<CObserver*>::iterator iter = m_ObserverVec.begin();
vector<CObserver*>::iterator iter_end = m_ObserverVec.end();
for(;iter!=iter_end;iter++)
{
(*iter)->Update();
}
}
void CSubject::SetObserversVal1(double val)
{
vector<CObserver*>::iterator iter = m_ObserverVec.begin();
vector<CObserver*>::iterator iter_end = m_ObserverVec.end();
for(;iter!=iter_end;iter++)
{
(*iter)->SetVal1(val);
}
}