SOLID & Design Patterns Interview Question #39
What is the Mediator Pattern?
Structural, Creational & Behavioral Patterns Junior Beginner
Quick Interview Answer
The Mediator Pattern centralizes communication between collaborating objects so they do not need direct references to one another. Participants communicate through a mediator, which coordinates the interaction.
Detailed Explanation
The Mediator Pattern centralizes communication between collaborating objects so they do not need direct references to one another. Participants communicate through a mediator, which coordinates the interaction.
In application architecture, mediator-style request dispatch can reduce controller-to-service coupling. However, excessive mediator usage can hide control flow and turn the mediator layer into an unnecessary indirection.
Code Example
public interface IMediator
{
Task<TResponse> SendAsync<TResponse>(
IRequest<TResponse> request);
}
public record GetOrderQuery(int Id)
: IRequest<OrderDto>;