Mid From PDF GoF Patterns Gang of Four Patterns

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);
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);

}
}
}
}

More from Design Patterns in C#

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details