static void Main() { IMediator mediator = new Mediator(); var talkColleague = new ColleagueA(mediator); var fightColleague = new ColleagueB(mediator); talkColleague.Act(); fightColleague.Act(); }
static void Main(string[] args) { //一个房主、一个租房者、一个中介机构 ConcertorMediator mediator = new ConcertorMediator(); //房主和租房者只需要知道中介机构即可 ColleagueA colleagueA = new ColleagueA("张三", mediator); ColleagueB colleagueB = new ColleagueB("李四", mediator); //中介结构要知道房主和租房者 mediator.colleagueA = colleagueA; //房主 mediator.colleagueB = colleagueB; //租房者 colleagueB.PushMessage(" 听说你那里有三室的房主出租....."); colleagueA.PushMessage(" 是的!请问你需要租吗?"); }
public void Register(AbstractColleague colleague) { switch (colleague) { case ColleagueA a: this.a = a; break; case ColleagueB b: this.b = b; break; default: break; } }
static void Main(string[] args) { Mediator m = new Mediator(); Colleague head1 = new Colleague(m, "John"); ColleagueB branch1 = new ColleagueB(m, "David"); Colleague head2 = new Colleague(m, "Lucy"); head1.Send("Meeting on Tuesday, please all ack"); branch1.Send("Ack"); m.Block(branch1.Receive); head1.Send("Still awaiting some Acks"); head2.Send("Ack"); m.Unblock(branch1.Receive); head1.Send("Thanks all"); Console.ReadLine(); }
static void Sample2() { Sample2.Mediator m = new Sample2.Mediator(); Colleague head1 = new Colleague(m, "John"); ColleagueB branch1 = new ColleagueB(m, "David"); Colleague head2 = new Colleague(m, "Lucy"); head1.Send("Meeting on Tuesday, please all ack"); branch1.Send("Ack"); m.Block(branch1.Receive); head1.Send("Still awaiting some Acks"); head2.Send("Ack"); m.Unblock(branch1.Receive); head1.Send("Thanks all"); }