/// <summary> /// 中介者模式 Mediator Pattern 亦称: 调解人、控制器、Intermediary、Controller、Mediator /// geovindu,Geovin Du, Edit /// </summary> interface IMediator { /// <summary> /// /// 注册 /// </summary> /// <param name="friend"></param> void Register(AbstractFriend friend); /// <summary> /// /// 发送 /// </summary> /// <param name="fromFriend"></param> /// <param name="toFriend"></param> /// <param name="msg"></param> void Send(AbstractFriend fromFriend, AbstractFriend toFriend, string msg); /// <summary> /// 显示详情 /// </summary> void DisplayDetails(); } /// <summary> /// /// /// </summary> class ConcreteMediator : IMediator { /// <summary> /// /// </summary> List<AbstractFriend> participants = new List<AbstractFriend>(); /// <summary> /// 注册 /// </summary> /// <param name="friend"></param> public void Register(AbstractFriend friend) { participants.Add(friend); } /// <summary> /// /// </summary> public void DisplayDetails() { Console.WriteLine("目前已登记的参加者名单如下:"); foreach (AbstractFriend friend in participants) { Console.WriteLine($"{friend.Name}"); } } /// <summary> /// /// </summary> /// <param name="fromFriend"></param> /// <param name="toFriend"></param> /// <param name="msg"></param> public void Send(AbstractFriend fromFriend, AbstractFriend toFriend, string msg) { if (participants.Contains(fromFriend)) { if (participants.Contains(toFriend) && toFriend.Status == "On") { Console.WriteLine($"\n[{fromFriend.Name}] 邮件: {msg} 最后一条信息发布时间: {DateTime.Now}"); System.Threading.Thread.Sleep(1000); toFriend.ReceiveMessage(fromFriend, msg); } else { Console.WriteLine($"\n{fromFriend.Name},此时,你无法发信息给{toFriend.Name} ,因为他不是注册用户就是离线了."); } } else { Console.WriteLine($"\n一个外面的朋友是: {fromFriend.Name} 属于 [{fromFriend.GetType()}] 是想传达信息给 {toFriend.Name}."); } } } /// <summary> /// /// /// </summary> abstract class AbstractFriend { /// <summary> /// /// </summary> IMediator mediator; /// <summary> /// /// </summary> public string Name { get; set; } /// <summary> /// /// </summary> public string Status { get; set; } /// <summary> /// /// </summary> /// <param name="mediator"></param> public AbstractFriend(IMediator mediator) { this.mediator = mediator; } /// <summary> /// 发送信息 /// </summary> /// <param name="toFriend"></param> /// <param name="msg"></param> public void SendMessage(AbstractFriend toFriend, string msg) { mediator.Send(this, toFriend, msg); } /// <summary> /// 接收信息 /// </summary> /// <param name="fromFriend"></param> /// <param name="msg"></param> public void ReceiveMessage(AbstractFriend fromFriend, string msg) { Console.WriteLine($"{this.Name} 有一条接收信息,来自于 {fromFriend.Name} 说得是: {msg} "); } } /// <summary> /// /// </summary> class Friend : AbstractFriend { /// <summary> /// /// </summary> /// <param name="mediator"></param> public Friend(IMediator mediator) : base(mediator) { } } /// <summary> /// /// </summary> class Stranger : AbstractFriend { /// <summary> /// /// </summary> /// <param name="mediator"></param> public Stranger(IMediator mediator) : base(mediator) { } }
/// <summary> /// 中介者模式 Mediator Pattern 亦称: 调解人、控制器、Intermediary、Controller、Mediator /// geovindu,Geovin Du, Edit /// </summary> public class Person { /// <summary> /// /// </summary> public string Name; /// <summary> /// /// </summary> public ChatRoom Room; /// <summary> /// /// </summary> private List<string> chatLog = new List<string>(); /// <summary> /// /// </summary> /// <param name="name"></param> public Person(string name) => Name = name; /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="message"></param> public void Receive(string sender, string message) { string s = $"{sender}: '{message}'"; Console.WriteLine($"[{Name}'s 聊天会话] {s}"); chatLog.Add(s); } /// <summary> /// /// </summary> /// <param name="message"></param> public void Say(string message) => Room.Broadcast(Name, message); /// <summary> /// /// </summary> /// <param name="who"></param> /// <param name="message"></param> public void PrivateMessage(string who, string message) { Room.Message(Name, who, message); } } /// <summary> /// /// </summary> public class ChatRoom { /// <summary> /// /// </summary> private List<Person> people = new List<Person>(); /// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="message"></param> public void Broadcast(string source, string message) { foreach (var p in people) if (p.Name != source) p.Receive(source, message); } /// <summary> /// /// </summary> /// <param name="p"></param> public void Join(Person p) { string joinMsg = $"{p.Name} 加入聊天室"; Broadcast("房间", joinMsg); p.Room = this; people.Add(p); } /// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="destination"></param> /// <param name="message"></param> public void Message(string source, string destination, string message) { people.FirstOrDefault(p => p.Name == destination)?.Receive(source, message); } }
调用:
//中介者模式 Mediator Pattern Console.WriteLine("***中介者模式 Mediator Pattern.***\n"); IMediator mediator = new ConcreteMediator(); Friend friend1 = new Friend(mediator); friend1.Name = "geovindu"; friend1.Status = "On"; Friend friend2 = new Friend(mediator); friend2.Name = "涂聚文"; friend2.Status = "On"; Stranger stranger1 = new Stranger(mediator); stranger1.Name = "涂年生"; stranger1.Status = "On"; mediator.Register(friend1); mediator.Register(friend2); mediator.Register(stranger1); mediator.DisplayDetails(); Console.WriteLine("交流始于参与者之间..."); friend1.SendMessage(friend2, "你好 涂聚文,我们能讨论一下中介模式吗?"); friend2.SendMessage(friend1, "你好 geovindu,是的,我们现在就可以讨论."); stranger1.SendMessage(friend1, " 你好吗?"); Friend friend4 = new Friend(mediator); friend4.Name = "李心"; friend4.SendMessage(stranger1, "你好涂年生..."); friend1.SendMessage(friend4, "你好,李心..."); Stranger stranger2 = new Stranger(mediator); stranger2.Name = "杰克"; stranger2.SendMessage(stranger1, "你好,朋友..."); Console.WriteLine("涂聚文正在离线."); friend2.Status = "Off"; friend1.SendMessage(friend2, "你好,涂聚文, 我有一个礼物送给你!"); Console.WriteLine("涂聚文,又开始上线了."); friend2.Status = "On"; stranger1.SendMessage(friend2, "你好,涂聚文,geovindu 正找过你."); Console.WriteLine(); // var room = new ChatRoom(); var john = new Person("geovindu"); var jane = new Person("涂聚文"); room.Join(john); room.Join(jane); john.Say("hi 涂聚文"); jane.Say("oh, hey geovindu"); var simon = new Person("李重心"); room.Join(simon); simon.Say("各位好!"); jane.PrivateMessage("李重心", "很高兴你能加入我们!"); Console.Read();
输出:
***中介者模式 Mediator Pattern.*** 目前已登记的参加者名单如下: geovindu 涂聚文 涂年生 交流始于参与者之间... [geovindu] 邮件: 你好 涂聚文,我们能讨论一下中介模式吗? 最后一条信息发布时间: 2022/10/11 21:34:49 涂聚文 有一条接收信息,来自于 geovindu 说得是: 你好 涂聚文,我们能讨论一下中介模式吗? [涂聚文] 邮件: 你好 geovindu,是的,我们现在就可以讨论. 最后一条信息发布时间: 2022/10/11 21:34:50 geovindu 有一条接收信息,来自于 涂聚文 说得是: 你好 geovindu,是的,我们现在就可以讨论. [涂年生] 邮件: 你好吗? 最后一条信息发布时间: 2022/10/11 21:34:51 geovindu 有一条接收信息,来自于 涂年生 说得是: 你好吗? 一个外面的朋友是: 李心 属于 [MediatorPatternModifiedDemo.Friend] 是想传达信息给 涂年生. geovindu,此时,你无法发信息给李心 ,因为他不是注册用户就是离线了. 一个外面的朋友是: 杰克 属于 [MediatorPatternModifiedDemo.Stranger] 是想传达信息给 涂年生. 涂聚文正在离线. geovindu,此时,你无法发信息给涂聚文 ,因为他不是注册用户就是离线了. 涂聚文,又开始上线了. [涂年生] 邮件: 你好,涂聚文,geovindu 正找过你. 最后一条信息发布时间: 2022/10/11 21:34:52 涂聚文 有一条接收信息,来自于 涂年生 说得是: 你好,涂聚文,geovindu 正找过你. [geovindu's 聊天会话] 房间: '涂聚文 加入聊天室' [涂聚文's 聊天会话] geovindu: 'hi 涂聚文' [geovindu's 聊天会话] 涂聚文: 'oh, hey geovindu' [geovindu's 聊天会话] 房间: '李重心 加入聊天室' [涂聚文's 聊天会话] 房间: '李重心 加入聊天室' [geovindu's 聊天会话] 李重心: '各位好!' [涂聚文's 聊天会话] 李重心: '各位好!' [李重心's 聊天会话] 涂聚文: '很高兴你能加入我们!'