????2?????????????????Observer????????д??update()????????ú??????ж??????????????????????????update()????????????
??????????????£?
class Test1 : public Observer
{
public:
//observer
virtual void update(Observable* from?? YtEvent *e)
{
if(NULL == e)
return;
std::string szEvent = e->getEvent();
if(szEvent == "start_update")
{
printf("I find a Update Message. ");
}
}
};
????3???????????????????Observable?????ж????????value???????????????????value?????仯???????Test1???????
??????????????£?
#ifndef __DOC_DATA_
#define __DOC_DATA_
#include "Observer.h"
class CDoc : public Observable
{
public:
CDoc()
{
value = 0;
}
~CDoc()
{
value = -1;
}
void setValue(int arg)
{
value= arg;
}
int getValue()
{
return value;
}
private:
int value;
};
#endif
????4???????????CDoc??????addObserver()?????????????????????????????????????????????????????????????????notifyObservers()?????????????
??????????????£?
// ObserverTest.cpp : ??????????ó????????
//
#include "stdafx.h"
#include "Observer.h"
#include "Test1.h"
#include "DocData.h"
#include "YtEvent.h"
void main()
{
CDoc data;
TestOBSERVER1 test;
char aa[128];
data.addObserver(static_cast<Observer*>(&test));
YtEvent yet("start_update");
data.notifyObservers(&yet);
printf("aaaaaaaaaaaaaaaaaaaaaaa ");
scanf("%s"?? aa);
}