Mid From PDF GoF Patterns Gang of Four Patterns

Colleague (User):?

  • The User class represents a user in the chat application. Each user has a

name and a reference to the mediator.

  • When a user wants to send a message, they call Send() on the mediator,

which then sends the message to all other registered users.

  • Users only interact with the mediator, and not with each other directly.
public class User
{
private readonly string _name;
private readonly IChatMediator _mediator;
public User(string name, IChatMediator mediator)
{
_name = name;
_mediator = mediator;

_mediator.RegisterUser(this); // Register the user with the

mediator

}
public void Send(string message) =>

_mediator.SendMessage(message, this);

public void Receive(string message) =>

Console.WriteLine($"{_name} received: {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