在软件的构建构成中,经常存在多个对象相互交互的情况,这样的话,一个类的改变的话,其他关联的对象都需要改变。解决的办法是在各个对象之间增加一个Mediator中介者,使用这个对象来管理这些对象的交互。
第一种:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Mediator { class Program { static void Main(string[] args) { ChatRoom cr = new ChatRoom(); Participator p = new Participator("Shao mingbo",cr); Participator p2 = new Participator("YW",cr); Participator p3 = new Participator("ZZ",cr); Participator p4 = new Participator("Xiao Di",cr); //Say Hello to The One p.Send(p2,"Hello"); Console.WriteLine("--------------"); // Say Hi To All p4.Send("Hi"); } } class Participator { private string _name; private ChatRoom cr; public string Name { get { return _name; } set { _name = value; } } public Participator(string name,ChatRoom cr) { this._name = name; this.cr = cr; cr.RegistParticipator(this); } public void Receive(string from,string msg) { if(cr!=null) Console.WriteLine("{0} 收到来自 {1}的一则消息: {2}", this.Name, from, msg); else Console.WriteLine("{0} 未曾在聊天室内注册!", this._name); } public void Send(Participator p,string msg) { if (cr != null) { cr.RegistParticipator(this); cr.Notify(this.Name,p, msg); } else Console.WriteLine("{0} 未曾在聊天室内注册!",this._name); } public void Send(string msg) { this.Send(null,msg); } } class ChatRoom { private Dictionary<string, participator=""> roomMate = new Dictionary<string, participator="">(); public void RegistParticipator(Participator p) { if (!roomMate.ContainsKey(p.Name)) roomMate.Add(p.Name,p); } public void Notify(string from,Participator p, string msg) { if (p != null) { p.Receive(from, msg); } else { foreach (var receiver in roomMate) { if (receiver.Key != from) { Participator r = receiver.Value; r.Receive(from,msg); } } } } } }
第二种:
/// <summary> /// MainApp startup class for Structural /// Mediator Design Pattern. /// </summary> class MainApp { /// <summary> /// Entry point into console application. /// </summary> static void Main() { 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.ReadKey(); } } /// <summary> /// The 'Mediator' abstract class /// </summary> abstract class Mediator { public abstract void Send(string message, Colleague colleague); } /// <summary> /// The 'ConcreteMediator' class /// </summary> class ConcreteMediator : Mediator { private ConcreteColleague1 _colleague1; private ConcreteColleague2 _colleague2; public ConcreteColleague1 Colleague1 { set { _colleague1 = value; } } public ConcreteColleague2 Colleague2 { set { _colleague2 = value; } } public override void Send(string message, Colleague colleague) { if (colleague == _colleague1) { _colleague2.Notify(message); } else { _colleague1.Notify(message); } } } /// <summary> /// The 'Colleague' abstract class /// </summary> abstract class Colleague { protected Mediator mediator; // Constructor public Colleague(Mediator mediator) { this.mediator = mediator; } } /// <summary> /// A 'ConcreteColleague' class /// </summary> class ConcreteColleague1 : Colleague { // Constructor public ConcreteColleague1(Mediator mediator) : base(mediator) { } public void Send(string message) { mediator.Send(message, this); } public void Notify(string message) { Console.WriteLine("Colleague1 gets message: " + message); } } /// <summary> /// A 'ConcreteColleague' class /// </summary> class ConcreteColleague2 : Colleague { // Constructor public ConcreteColleague2(Mediator mediator) : base(mediator) { } public void Send(string message) { mediator.Send(message, this); } public void Notify(string message) { Console.WriteLine("Colleague2 gets message: " + message); } }
作者:许强1. 本博客中的文章均是个人在学习和项目开发中总结。其中难免存在不足之处 ,欢迎留言指正。 2. 本文版权归作者和博客园共有,转载时,请保留本文链接。
1. 本博客中的文章均是个人在学习和项目开发中总结。其中难免存在不足之处 ,欢迎留言指正。 2. 本文版权归作者和博客园共有,转载时,请保留本文链接。