Mediator
Context: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly.
public interface IMediator{ void Notify(object sender, string ev);}
public class ConcreteMediator : IMediator{ private ComponentA _a; private ComponentB _b; public void RegisterA(ComponentA a) => _a = a; public void RegisterB(ComponentB b) => _b = b; public void Notify(object sender, string ev) { if (ev == "A") _b.DoB(); else if (ev == "B") _a.DoA(); }}Real-world usage example
Section titled “Real-world usage example”Chat room: A chat mediator handles message routing between users. Users don’t reference each other directly; they send to the mediator, which broadcasts.
Example: In .NET, the MediatR library implements mediator pattern for in‑process messaging. Also, IHttpClientFactory acts as a mediator for creating HttpClient instances.