static void Main(string[] args) { Console.WriteLine("***Mediator Pattern Demo***\n"); ConcreteMediator mediator = new ConcreteMediator(); Friend1 Amit = new Friend1(mediator, "Amit"); Friend2 Sohel = new Friend2(mediator, "Sohel"); Boss Raghu = new Boss(mediator, "Raghu"); //Registering participants mediator.Register(Amit); mediator.Register(Sohel); mediator.Register(Raghu); //Displaying the participant's list mediator.DisplayDetails(); Console.WriteLine("Communication starts among participants..."); Amit.Send("Hi Sohel,can we discuss the mediator pattern?"); Sohel.Send("Hi Amit,Yup, we can discuss now."); Raghu.Send("Please get back to work quickly."); //An outsider/unknown person tries to participate Unknown unknown = new Unknown(mediator, "Jack"); unknown.Send("Hello Guys.."); // Wait for user Console.Read(); }
static void Main(string[] args) { ConcreteMediator concreteMediator = new ConcreteMediator(); ColleagueLuke colleagueLuke = concreteMediator.CreateColleague <ColleagueLuke>(); ColleagueTom _ = concreteMediator.CreateColleague <ColleagueTom>(); colleagueLuke.Send("Ahoj Tome, tady Luke"); }
static void Main(string[] args) { ConcreteMediator mediator = new ConcreteMediator(); Console.WriteLine("Program change colleague1 Status to false"); mediator.colleague1.ChangeStatus(false); Console.WriteLine("Program change colleague2 Status to false"); mediator.colleague2.ChangeStatus(false); Console.WriteLine(mediator.colleague1.Status); }
static void Main(string[] args) { var cm = new ConcreteMediator(); var c1 = new ConcreateColleague1(cm); var c2 = new ConcreateColleague2(cm); cm.SetColleague1(c1); cm.SetColleague2(c2); c1.Action(); c2.Action(); Console.ReadKey(); }
static void Main(string[] args) { AbstractMediator mediator = new ConcreteMediator(); AbstractColleague colleague1 = new Colleage("张三", mediator); AbstractColleague colleague2 = new Colleage("李四", mediator); mediator.Add(colleague1); mediator.Add(colleague2); colleague1.Send("李四", "在吗?"); colleague2.Send("张三", "我在这儿!"); Console.ReadKey(); }
static void Main(string[] args) { ConcreteMediator mediator = new ConcreteMediator(); ColleaguaA colleaguaA = new ColleaguaA(mediator); ColleaguaB colleaguaB = new ColleaguaB(mediator); mediator.ColleaguaA = colleaguaA; mediator.ColleaguaB = colleaguaB; colleaguaA.Notify("一起玩游戏啊!"); colleaguaB.Notify("不了,我还要学习。"); }
void Test() { mediator = new ConcreteMediator(); colleague1 = new ConcreteColleague1(mediator); colleague2 = new ConcreteColleague2(mediator); mediator.Colleague1 = colleague1; mediator.Colleague2 = colleague2; colleague1.Send("同志们好!"); colleague2.Send("首长好!"); }
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(); }
public static void Main(string[] args) { ConcreteMediator mediator = new ConcreteMediator(); Colleague colleague1 = new ConcreteColleague1(mediator); Colleague colleague2 = new ConcreteColleague2(mediator); mediator.Colleague1 = colleague1; mediator.Colleague2 = colleague2; colleague1.Send("Hello, how are you?"); colleague2.Send("Fine, Thx, and you?"); Console.Read(); }
static void Main(string[] args) { AbstractCardPartner partnerA = new PartnerA("张三"); AbstractCardPartner partnerB = new PartnerB("李四"); partnerA.Money = 20m; partnerB.Money = 20m; AbstractMediator mediator = new ConcreteMediator(partnerA, partnerB); partnerA.ChangeMoney(5, mediator); Console.WriteLine("{0}:{1},{2}:{3}", partnerA.Name, partnerA.Money.ToString("C"), partnerB.Name, partnerB.Money.ToString("C")); partnerB.ChangeMoney(10, mediator); Console.WriteLine("{0}:{1},{2}:{3}", partnerA.Name, partnerA.Money.ToString("C"), partnerB.Name, partnerB.Money.ToString("C")); Console.ReadKey(); }
public static void Main(string[] args) { var mediator = new ConcreteMediator(); var c1 = new ConcreteColleague1(mediator); var c2 = new ConcreteColleague2(mediator); mediator.Colleague1 = c1; mediator.Colleague2 = c2; c1.Send("吃饭了吗?"); c2.Send("没有呢,你打算请客?"); Console.ReadLine(); }
static void Main(string[] args) { ConcreteMediator m = new ConcreteMediator(); ConcreteColleageue1 c1 = new ConcreteColleageue1(m); ConcreteColleageue2 c2 = new ConcreteColleageue2(m); m.Colleageue1 = c1; m.Colleageue2 = c2; c1.Send("hello"); c2.Send("矮油,不错呦"); Console.ReadKey(); }
static void Main(string[] args) { ConcreteMediator concreteMediator = new ConcreteMediator(); ConcreteColleague1 colleague1 = new ConcreteColleague1(concreteMediator); ConcreteColleague2 colleague2 = new ConcreteColleague2(concreteMediator); concreteMediator.setC1(colleague1); concreteMediator.setC2(colleague2); concreteMediator.doSomething1(); concreteMediator.doSomething2(); concreteMediator.getC1().depMethod1(); concreteMediator.getC2().depMethod2(); Console.ReadLine(); }
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"); // Wait for user Console.Read(); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); Console.WriteLine("--------------------DediatorPattern--------------------"); Console.WriteLine(); var mediator = new ConcreteMediator(); FinancialConcreteColleague financial = new FinancialConcreteColleague(mediator); DevelopmentConcreteColleague development = new DevelopmentConcreteColleague(mediator); MarketConcreteColleague market = new MarketConcreteColleague(mediator); mediator.SetFinancial(financial); mediator.SetDevelopment(development); mediator.SetMarket(market); market.Process(); market.Apply(); Console.ReadKey(); }
static void Main(string[] args) { ConcreteMediator mediator = new ConcreteMediator(); Farmer farmer = new Farmer(mediator); Cannery cannery = new Cannery(mediator); Shop shop = new Shop(mediator); mediator.Farmer = farmer; mediator.Cannery = cannery; mediator.Shop = shop; farmer.GrowTomato(); Console.Read(); }
static void Main(string[] args) { //The mediator pattern acts like the controller in MVC //where the model and the view communicate through the controller instead of communicating directly //The mediator is essentially an object that encapsulates how one or more objects interact with each other //This helps to reduce the dependencies between objects and promotes loose coupling var mediator = new ConcreteMediator(); var manager = new ConcreteParticipant(mediator); var worker = new ConcreteParticipant2(mediator); mediator.AddParticipant(manager); mediator.AddParticipant(worker); manager.SendMessage("Hey worker, can you send me the report before close of business?"); worker.SendMessage("Hey manager, sure! I will work on that now!"); Console.ReadLine(); }
static void Main(string[] args) { ConcreteMediator imkb = new ConcreteMediator(); ConcreteCollig broker = new ConcreteCollig(imkb); ConcreteCollig2 broker2 = new ConcreteCollig2(imkb); ConcreteCollig3 broker3 = new ConcreteCollig3(imkb); Console.WriteLine("------------------------------------------------\n"); broker.SatisTeklifi("MSFT", 100); broker.SatisTeklifi("GOOG", 50); broker2.AlisTeklifi("MSFT", 100); broker2.SatisTeklifi("NRG", 10); broker3.AlisTeklifi("NRG", 10); broker3.AlisTeklifi("MSFT", 100); broker3.SatisTeklifi("NRG", 10); imkb.SenetleriListele(); Console.ReadKey(); }
static void Main(string[] args) { //list of participants IColleague <string> mark = new ConcreteColleague <string>("Mark"); IColleague <string> cj = new ConcreteColleague <string>("CJ"); IColleague <string> paul = new ConcreteColleague <string>("Paul"); IColleague <string> geno = new ConcreteColleague <string>("Geno"); IColleague <string> tessa = new ConcreteColleague <string>("Tessa"); IColleague <int> account1 = new ConcreteColleague <int>("Kris Account"); IColleague <int> account2 = new ConcreteColleague <int>("Joel Account"); IColleague <int> account3 = new ConcreteColleague <int>("Ronald Account"); Console.WriteLine("\n-- CHAT ROOM --"); IMediator <string> chatRoom1 = new ConcreteMediator <string>(); //participants registers to the mediator chatRoom1.Register(mark); chatRoom1.Register(cj); chatRoom1.Register(paul); chatRoom1.Register(tessa); //participantA sends out a message mark.SendMessage(chatRoom1, "message from MARK in chatroom 1!"); Console.WriteLine("\n-- MONEY TRANSFER --"); IMediator <int> PaymentGateway = new ConcreteMediator <int>(); //participants registers to the mediator PaymentGateway.Register(account1); PaymentGateway.Register(account2); PaymentGateway.Register(account3); //participantB sends out a message account1.SendMessage(PaymentGateway, 999); Console.ReadKey(); }
static void Main(string[] args) { ConcreteMediator mediator = new ConcreteMediator(); ColleagueA colleagueA = new ColleagueA(mediator); ColleagueB colleagueB = new ColleagueB(mediator); mediator.SetColleagueA(colleagueA); mediator.SetColleagueB(colleagueB); colleagueB.Send("Hello A"); colleagueA.Send("Hello B"); CustomerServericeMM customerMM = new CustomerServericeMM(); Customer customer = new Customer(customerMM); Techician techician = new Techician(customerMM); customerMM.SetCustomer(customer); customerMM.SetTechicain(techician); customer.Ask("客户提问"); techician.Answer("技术支持解答"); }
static void Main(string[] args) { //介绍 // 意图:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。 //主要解决:对象与对象之间存在大量的关联关系,这样势必会导致系统的结构变得很复杂,同时若一个对象发生改变,我们也需要跟踪与之相关联的对象,同时做出相应的处理。 //何时使用:多个类相互耦合,形成了网状结构。 //如何解决:将上述网状结构分离为星型结构。 //关键代码:对象 Colleague 之间的通信封装到一个类中单独处理。 //应用实例: 1、中国加入 WTO 之前是各个国家相互贸易,结构复杂,现在是各个国家通过 WTO 来互相贸易。 2、机场调度系统。 3、MVC 框架,其中C(控制器)就是 M(模型)和 V(视图)的中介者。 //优点: 1、降低了类的复杂度,将一对多转化成了一对一。 2、各个类之间的解耦。 3、符合迪米特原则。 //缺点:中介者会庞大,变得复杂难以维护。 //使用场景: 1、系统中对象之间存在比较复杂的引用关系,导致它们之间的依赖关系结构混乱而且难以复用该对象。 2、想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类。 //注意事项:不应当在职责混乱的时候使用 ConcreteMediator mediator = new ConcreteMediator(); ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator); ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator); mediator.ConcreteColleague1 = colleague1; mediator.ConcreteColleague2 = colleague2; colleague1.Send("hello colleague1"); colleague2.Send("hello colleague2"); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***Mediator Pattern Demonstration.***\n"); IMediator mediator = new ConcreteMediator(); //AbstractFriend afriend = new AbstractFriend(mediator);//error //3 persons-Amit,Sohel,Joseph //Amit and Sohel from Friend class Friend friend1 = new Friend(mediator); friend1.Name = "Amit"; Friend friend2 = new Friend(mediator); friend2.Name = "Sohel"; //Joseph is from Stranger class Stranger stranger1 = new Stranger(mediator); stranger1.Name = "Joseph"; //Registering the participants mediator.Register(friend1); mediator.Register(friend2); mediator.Register(stranger1); //Displaying the participant's list mediator.DisplayDetails(); Console.WriteLine("Communication starts among participants..."); friend1.SendMessage(friend2, "Hi Sohel,can we discuss the mediator pattern?"); friend2.SendMessage(friend1, "Hi Amit,Yup, we can discuss now."); stranger1.SendMessage(friend1, " How are you?"); //Another friend who does not register to the mediator Friend friend4 = new Friend(mediator); friend4.Name = "Todd"; /* * Todd is NOT a registered user. * So,he cannot send this message to Joseph. */ friend4.SendMessage(stranger1, "Hello Joseph..."); /* * Todd is NOT a registered user. * So,he cannot receive this message from Amit. */ friend1.SendMessage(friend4, "Hello Todd..."); //An outsider person tries to participate Stranger stranger2 = new Stranger(mediator); stranger2.Name = "Jack"; //mediator.Register(stranger1); //This message cannot reach Joseph, because Jack //is not the registered user. stranger2.SendMessage(stranger1, "Hello friend..."); // Wait for user Console.Read(); }