#include "stdafx.h"
#include <vector>
#include <iostream>
#include "string"
using namespace std;
class ChatRoom;
struct Observer
{
virtual void SetChatRoom(ChatRoom* pChatRoom) = 0;
virtual void Notify(const string& from, const string&
to, const string& msg) = 0;
};
class ChatRoom //
Subject
{
private:
vector<Observer*> vo;
public:
virtual ~ChatRoom()
{
vector<Observer*>::iterator vi =
vo.begin();
for (; vi != vo.end(); vi++)
{
delete *vi;
}
}
void Login(Observer* po)
{
vo.push_back(po);
po->SetChatRoom(this);
}
void Logout(Observer* po)
{
vector<Observer*>::iterator vi =
vo.begin();
for (; vi != vo.end(); vi++)
{
if (*vi == po)
vo.erase(vi);
}
}
void Notify(const string& from, const string& to,
const string& msg)
{
vector<Observer*>::iterator vi =
vo.begin();
for (; vi != vo.end(); vi++)
{
(*vi)->Notify(from, to, msg);
}
}
};
class Chater : public Observer
// ConcreteObserver
{
private:
string name;
ChatRoom* pChatRoom;
public:
Chater(const string& name) : name(name) {}
void SetChatRoom(ChatRoom* pChatRoom)
{
this->pChatRoom = pChatRoom;
}
void Send(const st
