Concrete Mediator (ChatMediator):?
- The ChatMediator class is the concrete mediator that implements
IChatMediator. It manages a list of users and is responsible for
broadcasting messages to all registered users, except the one who sent the
message.
- The mediator decouples the user objects from each other, so they don't need
to know about each other's existence.
public class ChatMediator : IChatMediator
private readonly List<User> _users = new List<User>();
public void RegisterUser(User user) => _users.Add(user);
Follow:
public void SendMessage(string message, User user)
foreach (var u in _users)
// Message should not be sent to the user who sent it
if (u != user)
u.Receive(message);