codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎭

Design Patterns

24 / 100 topics
19Introduction to Behavioral Patterns20Chain of Responsibility Pattern21Command Pattern22Interpreter Pattern23Iterator Pattern24Mediator Pattern25Memento Pattern26Observer Pattern27State Pattern28Strategy Pattern29Template Method Pattern30Visitor Pattern33Practical Exercises for Behavioral Patterns
Tutorials/Design Patterns/Mediator Pattern
🎭Design Patterns

Mediator Pattern

Updated 2026-05-15
10 min read

Mediator Pattern

Introduction

The Mediator pattern is one of the Behavioral design patterns, which focuses on how objects communicate with each other. It defines an object that encapsulates how a set of objects interact. This pattern promotes loose coupling between interacting objects by centralizing their communication through a mediator object.

In real-world scenarios, imagine a group of people in a room who need to communicate with each other. Instead of everyone talking directly to one another, they might use a facilitator or a moderator to relay messages. The Mediator pattern works similarly by providing a centralized point for objects to interact without needing to reference each other directly.

Concept

The key components involved in the Mediator pattern are:

  1. Mediator: Defines an interface for communication between Colleague objects.
  2. ConcreteMediator: Implements the Mediator interface and coordinates interactions between Colleague objects.
  3. Colleague: Each colleague object holds a reference to the mediator and communicates with other colleagues only through the mediator.

The benefits of using the Mediator pattern include:

  • Loose Coupling: Reduces dependencies between colleagues, making it easier to modify or extend individual components without affecting others.
  • Centralized Control: All interactions are managed by the mediator, making it easier to understand and manage complex communication patterns.
  • Flexibility: Easier to change interaction rules since they are encapsulated in the mediator.

Examples

Let's explore a practical example of the Mediator pattern using JavaScript. We'll create a simple chat application where multiple users can send messages to each other through a central mediator.

Step 1: Define the Colleague Interface

First, we define an interface for our colleagues. In this case, it will be a User class that holds a reference to the mediator and provides methods for sending and receiving messages.

JavaScript
1class User {
2 constructor(name, mediator) {
3 this.name = name;
4 this.mediator = mediator;
5 }
6
7 send(message) {
8 this.mediator.send(message, this);
9 }
10
11 receive(message, from) {
12 console.log(`${from.name} to ${this.name}: ${message}`);
13 }
14}

Step 2: Define the Mediator

Next, we define the ChatMediator class that implements the mediator interface. This class will manage the interactions between users.

JavaScript
1class ChatMediator {
2 constructor() {
3 this.users = [];
4 }
5
6 addUser(user) {
7 this.users.push(user);
8 }
9
10 send(message, sender) {
11 this.users.forEach(user => {
12 if (user !== sender) {
13 user.receive(message, sender);
14 }
15 });
16 }
17}

Step 3: Create Users and Mediator

Now, let's create some users and a mediator, and add the users to the mediator.

JavaScript
1const mediator = new ChatMediator();
2
3const user1 = new User('Alice', mediator);
4const user2 = new User('Bob', mediator);
5const user3 = new User('Charlie', mediator);
6
7mediator.addUser(user1);
8mediator.addUser(user2);
9mediator.addUser(user3);

Step 4: Send Messages

Finally, let's simulate sending messages between users.

JavaScript
1user1.send('Hello everyone!');
2user2.send('Hi Alice!');
3user3.send('Hey there!');

When you run the above code, you should see the following output:

Output
Alice to Bob: Hello everyone!
Alice to Charlie: Hello everyone!
Bob to Alice: Hi Alice!
Bob to Charlie: Hi Alice!
Charlie to Alice: Hey there!
Charlie to Bob: Hey there!

What's Next?

In this tutorial, we explored the Mediator pattern and how it can be used to manage interactions between objects in a decoupled manner. In the next section, we will delve into another Behavioral design pattern called the Memento Pattern, which provides a way to save and restore an object's state without exposing its internal structure.

Feel free to experiment with the Mediator pattern in your projects and see how it can simplify complex communication scenarios!


PreviousIterator PatternNext Memento Pattern

Recommended Gear

Iterator PatternMemento Pattern