#include<vector>
#include<iostream>
#include<string>
using namespace std;
class Subject;
class Observer{
public:
virtual void update(Subject *base)=0;
protected:
Subject * _subject;
};
class Subject{
private:
vector<Observer *> _observer;
public:
string msg;
void regiObserver(Observer *obs){
_observer.push_back(obs);
}
void deleObserver(Observer *obs){
vector<Observer *>::iterator it;
for(it = _observer.begin(); it != _observer.end(); it++){
if( *it == obs){
_observer.erase(it);
break;
}
}
}
void notify(){
vector<Observer *>::iterator it;
for(it = _observer.begin(); it != _observer.end(); it++)
(*it)->update(this);
}
};
class SecretarySubject:public Subject{
public:
void set(string s){
msg = s;
notify();
}
};
class Gamesobserver :public Observer{
public:
Gamesobserver(Subject *base):Observer(){
_subject = base;
_subject->regiObserver(this);
}
void update(Subject *base){
msg = base->msg;
display();
}
void display(){
cout<<msg<<",关闭游戏界面"<<endl;
}
private:
string msg;
};
class StockObserver:public Observer{
public:
StockObserver(Subject * base){
_subject = base;
_subject->regiObserver(this);
}
void update(Subject *base){
msg = base->msg;
display();
}
void display(){
cout<<msg<<",关闭炒股界面"<<endl;
}
private:
string msg;
};
int main()
{
SecretarySubject * sec = new SecretarySubject;
Gamesobserver * one = new Gamesobserver(sec);
StockObserver * two = new StockObserver(sec);
sec->set("老板来了");
sec->deleObserver(one);
sec->set("老板来了");
return 0;
}