#include <iostream>
#include <string>
using namespace std;
class absperson;
class absmed
{
public:
absmed(){}
virtual ~absmed(){}
virtual void send(string,absperson*)=0;
virtual void set(absperson*,absperson*)=0;
};
class absperson
{
public:
absperson(){}
virtual ~absperson(){}
virtual void send(string)=0;
virtual void get(string)=0;
protected:
absmed *p_med;
};
class person_buy : public absperson
{
public:
person_buy(absmed *p){p_med=p;}
virtual ~person_buy(){}
void send(string str)
{
p_med->send(str,this);
}
void get(string str)
{
cout<<"买主得到信息:"<<str<<endl;
}
};
class person_sell : public absperson
{
public:
person_sell(absmed *p){p_med=p;}
virtual ~person_sell(){}
void send(string str)
{
p_med->send(str,this);
}
void get(string str)
{
cout<<"卖主得到信息:"<<str<<endl;
}
};
class med : public absmed
{
protected:
absperson *person1;
absperson *person2;
public:
med(){}
virtual ~med(){}
void send(string str,absperson* p)
{
if (p==person1)
person2->get(str);
else
person1->get(str);
}
void set(absperson *p1, absperson *p2)
{
person1=p1;
person2=p2;
}
};
int main()
{
absmed *p_med=new med;
absperson *p_buy=new person_buy(p_med);
absperson *p_sell=new person_sell(p_med);
p_med->set(p_buy,p_sell);
p_buy->send("我想买电脑");
p_sell->send("哥卖的不是电脑,是寂寞");
p_buy->send("好吧,你赢了");
delete p_med;
delete p_buy;
delete p_sell;
system("pause");
return 0;
}