#include #include #include using namespace std; class Mediator; class Colleague{ Mediator* pm=0; protected: string name; public: Colleague(Mediator& m, string name); virtual void Receive(string message, string from){ cout< respond; public: void SignOn(Colleague& colleague){ respond.push_back(&colleague);} void Block(Colleague& colleague){ for (auto item = respond.begin(); item != respond.end();item++){ if (*item==&colleague) { respond.erase(item); break; } } //throw "Not found!"; } void Unblock(Colleague& colleague){ respond.push_back(&colleague);} // Send is implemented as a broadcast void Send(string message, string from){ for (auto &c : respond) (*c).Receive(message, from); cout<Send(message, name); } class ColleagueB:public Colleague{ public: ColleagueB(Mediator& mediator,string name) :Colleague(mediator, name){ } // Does not get copies of own messages void Receive(string message, string from){ if (from!=name) Colleague::Receive(message, from); } }; int main(){ Mediator m; // Two from head office and one from a branch office Colleague head1(m,"John"); ColleagueB branch1(m,"David"); Colleague head2(m,"Lucy"); head1.Send("Meeting on Tuesday, please all ack"); branch1.Send("Ack"); // by design does not get a copy m.Block(branch1); // temporarily gets no messages head1.Send("Still awaiting some Acks"); head2.Send("Ack"); m.Unblock(branch1); //open again head1.Send("Thanks all"); return 0; } /* Output Send (From John): Meeting on Tuesday, please all ack John received from John: Meeting on Tuesday, please all ack David received from John: Meeting on Tuesday, please all ack Lucy received from John: Meeting on Tuesday, please all ack Send (From David): Ack John received from David: Ack Lucy received from David: Ack Send (From John): Still awaiting some Acks John received from John: Still awaiting some Acks Lucy received from John: Still awaiting some Acks Send (From Lucy): Ack John received from Lucy: Ack Lucy received from Lucy: Ack Send (From John): Thanks all John received from John: Thanks all Lucy received from John: Thanks all David received from John: Thanks all */