static void Main(string[] args) { ConcreteMediator m = new ConcreteMediator(); ConcreteColleague1 c1 = new ConcreteColleague1(m); ConcreteColleague2 c2 = new ConcreteColleague2(m); m.Colleague1 = c1; m.Colleague2 = c2; c1.Send("吃过饭了吗?"); c2.Send("没有呢,你打算情况吗"); }
static void Main(string[] args) { ConcreteMediator mediator = new ConcreteMediator(); ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator); ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator); mediator.Colleague1 = colleague1; mediator.Colleague2 = colleague2; colleague1.Send("吃过饭了吗?"); colleague2.Send("没有呢 , 你打算请客吗?"); }
//中介者模式和之前写过的观察者模式很相似,均属于行为型模式 //观察者模式中观察者与被观察者(也叫主题),通常是多对一的关系 //一个主题被多个观察者观察,观察是单方面的 //中介者模式中,注册到中介者的各个类不直接联系,通过中介转发,呈现出多对多的关系 //注册到中介的各个类,既可以发送通知也可以接收通知 static void Main(string[] args) { IMediator mediator = new ConcreteMediator(); WorkMate wm1 = new ConcreteMate1(); WorkMate wm2 = new ConcreteMate2(); mediator.register(wm1); mediator.register(wm2); wm1.send("今天老板要请大家吃麻辣烫"); wm2.send("明天老板娘要请大家吃小龙虾"); Console.Read(); }
static void Main(string[] args) { ConcreteMediator m = new ConcreteMediator(); ConcreteColleague1 c1 = new ConcreteColleague1(m); ConcreteColleague2 c2 = new ConcreteColleague2(m); m.Colleague1 = c1; m.Colleague2 = c2; c1.Send("吃过饭了吗?"); c2.Send("没有呢,你打算请客?"); Console.Read(); }
static void Main(string[] args) { ConcreteMediator m = new ConcreteMediator(); ConcreteColleague1 c1 = new ConcreteColleague1(m); ConcreteColleague2 c2 = new ConcreteColleague2(m); m.Colleague1 = c1; m.Colleague2 = c2; c1.Send("How are You!"); c2.Send("Fine,thanks!"); Console.ReadKey(); }
static void Main(string[] args) { ConcreteMediator m = new ConcreteMediator(); ConcreteColleague1 c1 = new ConcreteColleague1(m); ConcreteColleague2 c2 = new ConcreteColleague2(m); m.Colleague1 = c1; m.Colleague2 = c2; c1.Send("吃过饭了吗?"); c2.Send("没有呢,你打算请客?"); Console.Read(); }
static void Main(string[] args) { ConcreteMediator m = new ConcreteMediator(); ConcreteColleague1 c1 = new ConcreteColleague1(m); ConcreteColleague2 c2 = new ConcreteColleague2(m); m.Colleage1 = c1; m.Colleage2 = c2; c1.Send("吃过饭了吗?"); c2.Send("没有呢,你打算请客?"); UnitedNationsSecurityCouncil unsc = new UnitedNationsSecurityCouncil(); USA usa = new USA(unsc); Iraq iraq = new Iraq(unsc); unsc.Usa = usa; unsc.Iraq = iraq; usa.Declare("不准研制核武器,否则要发动战争"); iraq.Declare("我们没有核武器,也不怕侵略"); }
static void Main(string[] args) { ConcreteMediator cm = new ConcreteMediator(); Colleague1 c1 = new Colleague1(); c1.SetMediator(cm); Colleague2 c2 = new Colleague2(); c2.SetMediator(cm); cm.AddColleague(c1); cm.AddColleague(c2); c1.SendMessage("大家吃饭了吗!"); c2.SendMessage("我还没有吃饭,你请我吃吧c1!"); }