/// <summary> /// 中介者模式 Mediator Pattern 亦称: 调解人、控制器、Intermediary、Controller、Mediator /// </summary> public abstract class User { /// <summary> /// /// </summary> protected IGroupChatMediator? _mediator; /// <summary> /// /// </summary> protected string _name; /// <summary> /// /// </summary> /// <param name="name"></param> protected User(string name) { _name = name; } /// <summary> /// /// </summary> public string Name => _name; /// <summary> /// /// </summary> /// <param name="mediator"></param> public void SetMediator(IGroupChatMediator mediator) => _mediator = mediator; /// <summary> /// /// </summary> /// <param name="message"></param> public abstract void Send(string message); /// <summary> /// /// </summary> /// <param name="message"></param> public abstract void Receive(string message); /// <summary> /// /// </summary> /// <returns></returns> public override string ToString() { return $"姓名:{Name}"; } } using Geovin.Du.DuMediator.GroupChat.Components.Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace Geovin.Du.DuMediator.GroupChat.Components { /// <summary> /// 中介者模式 Mediator Pattern 亦称: 调解人、控制器、Intermediary、Controller、Mediator /// </summary> public class ViberUser : User { public ViberUser(string name) : base(name) { } public override void Receive(string message) => Console.WriteLine($"{_name} 收到的信息: '{message}'"); public override void Send(string message) { if (_mediator == null) { return; } Console.WriteLine($"{_name} 发出的信息: '{message}' 给团队"); _mediator.SendMessage(message, this); } } } /// <summary> /// 中介者模式 Mediator Pattern 亦称: 调解人、控制器、Intermediary、Controller、Mediator /// </summary> public interface IGroupChatMediator { /// <summary> /// /// </summary> /// <param name="user"></param> void AddParticipant(User user); /// <summary> /// /// </summary> /// <param name="message"></param> /// <param name="sender"></param> void SendMessage(string message, User sender); } /// <summary> /// 中介者模式 Mediator Pattern 亦称: 调解人、控制器、Intermediary、Controller、Mediator /// </summary> public class ViberGroupChatMediator : IGroupChatMediator { /// <summary> /// /// </summary> private readonly List<User> _chatParticipants = new(); /// <summary> /// /// </summary> /// <param name="users"></param> public ViberGroupChatMediator(params User[] users) { foreach (var user in users) { AddParticipant(user); } Console.WriteLine(); } /// <summary> /// /// </summary> /// <param name="user"></param> public void AddParticipant(User user) { Console.WriteLine($"{user.Name} 是新的聊天参与者."); _chatParticipants.Add(user); user.SetMediator(this); } /// <summary> /// /// </summary> /// <param name="message"></param> /// <param name="sender"></param> public void SendMessage(string message, User sender) { foreach (var user in _chatParticipants.Where(user => user != sender)) { user.Receive(message); } } }
调用:
Geovin.Du.DuMediator.GroupChat.GroupChatExecutor.Execute();
输出:
中介者模式 Mediator Pattern:Group chat demo -------------------------------------------------- 马三 是新的聊天参与者. 张四 是新的聊天参与者. 王五 是新的聊天参与者. 马三 发出的信息: '有人在吗?' 给团队 张四 收到的信息: '有人在吗?' 王五 收到的信息: '有人在吗?' 安琪 是新的聊天参与者. 王五 发出的信息: '我们在.' 给团队 马三 收到的信息: '我们在.' 张四 收到的信息: '我们在.' 安琪 收到的信息: '我们在.'
标签:core,string,Mediator,Pattern,void,user,message,public From: https://www.cnblogs.com/geovindu/p/16990678.html