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(); }
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 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) { var chatroom = new ConcreteChatRoom(); var chatroom2 = new ConcreteChatRoom(); var colleague1 = new ConcreteColleague1(); var colleague2 = new ConcreteColleague2(); chatroom.Name = "Chatroom1"; chatroom2.Name = "Chatroom2"; colleague1.Name = "AlexD"; colleague2.Name = "Frederik"; colleague1.Join(chatroom); colleague2.Join(chatroom); colleague1.Join(chatroom2); colleague1.Send(chatroom); colleague2.Send(chatroom); colleague1.Send(chatroom2); }
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(); }
public void setC2(ConcreteColleague2 c2) { this.c2 = c2; }