//
// easy_test.cpp
// develop by ZhouFeng , Mar 6th 2013
#include <iostream>
#include <vector>
#include <string>
using std::cout;
using std::endl;
using std::vector;
using std::string;
class IObserver;
class IObserable
{
public:
virtual void addObserver(IObserver*) = 0;
virtual void removeObserver(IObserver*) = 0;
virtual void notifyObserver() = 0;
};
class IObserver
{
public:
virtual void update() = 0;
};
class Boss : public IObserable
{
typedef vector<IObserver*>::iterator it_obs;
typedef vector<IObserver*> t_vobs;
public:
Boss()
: vobs(new t_vobs)
{}
~Boss()
{
delete vobs;
}
void addObserver(IObserver *obs)
{
vobs->push_back(obs);
}
void removeObserver(IObserver *obs)
{
for(int i = 0; i < vobs->size(); ++i)
{
if((*vobs)[i] == obs)
{
vobs->erase(vobs->begin() + i);
break;
}
}
}
void notifyObserver()
{
for(int i = 0; i < vobs->size(); ++i)
{
(*vobs)[i]->update();
}
}
private:
t_vobs* vobs;
};
class TechnologicalLeader : public IObserver
{
public:
TechnologicalLeader(string name)
{
_name = name;
}
void update()
{
cout << "TechnologicalLeader:" << _name << endl;
cout << "Yes, I have got it.Thank you!" << endl;
cout << "--End--" << endl;
}
private:
string _name;
};
int main(int argc, char* argv[])
{
IObserable *obs = new Boss;
IObserver *observer = new TechnologicalLeader("JunLing.Wu");
IObserver *observer2 = new TechnologicalLeader("YouZhen.Wang");
obs->addObserver(observer);
obs->addObserver(observer2);
obs->notifyObserver();
obs->removeObserver(observer);
obs->notifyObserver();
return 0;
}
|