#include <stdio.h>
#include <list>

using namespace std;

class Observer;

// 通知者接口
class Subject
{
public:
	virtual void Attach(Observer* observer)=0;
	virtual void Detach(Observer* observer)=0;
	virtual void Notify()=0;
	virtual char*& SubjectState()=0;
};

// 抽象观察者
class Observer
{
public:
	Observer(char* _name,Subject* _sub) : name(_name),sub(_sub){}
	
	virtual void Update()=0;
protected:
	char* name;
	Subject* sub;
};

class Boss : public Subject
{
public:
	// 增加
	virtual void Attach(Observer* observer)
	{
		observers.push_back(observer);
	}
	
	// 减少
	virtual void Detach(Observer* observer)
	{
		observers.remove(observer);
	}
	
	// 通知
	virtual void Notify()
	{
		list<Observer*>::iterator iter;
		for(iter=observers.begin();iter!=observers.end();iter++) (*iter)->Update();
	}
	
	// 老板状态
	virtual char*& SubjectState()
	{
		return action;
	}
protected:
	list<Observer*> observers;
	char* action;
};

class Secretary : public Subject
{
public:
	// 增加
	virtual void Attach(Observer* observer)
	{
		observers.push_back(observer);
	}
	
	// 减少
	virtual void Detach(Observer* observer)
	{
		observers.remove(observer);
	}
	
	// 通知
	virtual void Notify()
	{
		list<Observer*>::iterator iter;
		for(iter=observers.begin();iter!=observers.end();iter++) (*iter)->Update();
	}
	
	// 前台秘书状态
	virtual char*& SubjectState()
	{
		return action;
	}
protected:
	list<Observer*> observers;
	char* action;
};

// 看股票的同事
class StockObserver : public Observer
{
public:
	StockObserver(char* name,Subject* sub) : Observer(name,sub){}
	
	virtual void Update()
	{
		printf("%s %s 关闭股票行情，继续工作！",sub->SubjectState(),name);
	}
};

// 看NBA的同事
class NBAObserver : public Observer
{
public:
	NBAObserver(char* name,Subject* sub) : Observer(name,sub){}
	
	virtual void Update()
	{
		printf("%s %s 关闭NBA直播，继续工作！",sub->SubjectState(),name);
	}
};

int main()
{
	Boss* huhansan = new Boss();
	
	// 看股票的同事
	StockObserver* tongshi1 = new StockObserver("魏关姹",huhansan);
	// 看NBA的同事
	NBAObserver* tongshi2 = new NBAObserver("易管查",huhansan);
	
	huhansan->Attach(tongshi1);
	huhansan->Attach(tongshi2);
	
	huhansan->Detach(tongshi1);
	
	// 老板回来
	huhansan->SubjectState() = "我胡汉三回来了！";
	
	huhansan->Notify();
	
	delete huhansan;
	delete tongshi1;
	delete tongshi2;
	return 0;
}