Decouple complex object interactions by introducing a mediator โ promoting loose coupling, centralized control, and modularity in communication-heavy systems.
๐ง What is the Mediator Pattern?
The Mediator Pattern is a behavioral design pattern that defines an object (Mediator) that encapsulates how a set of objects interact.
Instead of objects referring to each other directly, they communicate via a central mediator object.
๐งฉ Motivation
Problem | Mediator Solves |
---|---|
Many-to-many communication between components | Introduces centralized coordination |
Strong coupling between classes | Promotes loose coupling |
Hard-to-maintain interdependent code | Simplifies interaction logic |
โ Real-World Use Cases
Use Case | Description |
---|---|
๐จ Chatroom App | Users send messages via a central chatroom mediator |
๐ฌ Air Traffic Control | Flights communicate via control tower (mediator) |
๐ฌ UI Form Field Coordination | Fields affect each other via mediator (e.g., enable/disable) |
๐ฎ Game Entity Coordination | Game objects interact via scene manager |
๐ Pattern Components
Component | Responsibility |
---|---|
Mediator | Defines interface for coordinating communication |
ConcreteMediator | Implements coordination logic |
Colleague | Participates in communication via Mediator |
ConcreteColleague | Implements behavior and delegates interaction to Mediator |
๐ UML Class Diagram
+------------------+
| Mediator | <<interface>>
+------------------+
| +send(msg, user) |
+------------------+
โฒ
|
+------------------------+
| ConcreteMediator |
+------------------------+
| - List<Colleague> |
| +send(msg, user) |
+------------------------+
โฒ
|
+------------------+
| Colleague |
+------------------+
| - mediator |
| +send(msg) |
| +receive(msg) |
+------------------+
โฒ
|
+---------------------+
| ConcreteColleague |
+---------------------+
๐ป Java Implementation โ Chatroom Example
โ
ChatMediator.java
public interface ChatMediator {
void sendMessage(String message, User sender);
void addUser(User user);
}
โ
User.java
public abstract class User {
protected ChatMediator mediator;
protected String name;
public User(ChatMediator mediator, String name) {
this.mediator = mediator;
this.name = name;
}
public abstract void send(String message);
public abstract void receive(String message);
}
โ
ConcreteUser.java
public class ConcreteUser extends User {
public ConcreteUser(ChatMediator mediator, String name) {
super(mediator, name);
}
@Override
public void send(String message) {
System.out.println("๐ค " + name + " sends: " + message);
mediator.sendMessage(message, this);
}
@Override
public void receive(String message) {
System.out.println("๐ฉ " + name + " receives: " + message);
}
}
โ
ChatRoomMediator.java
public class ChatRoomMediator implements ChatMediator {
private final List<User> users = new ArrayList<>();
@Override
public void sendMessage(String message, User sender) {
for (User user : users) {
if (user != sender) {
user.receive(sender.name + ": " + message);
}
}
}
@Override
public void addUser(User user) {
users.add(user);
}
}
โ
Client.java
public class MediatorDemo {
public static void main(String[] args) {
ChatMediator mediator = new ChatRoomMediator();
User alice = new ConcreteUser(mediator, "Alice");
User bob = new ConcreteUser(mediator, "Bob");
User charlie = new ConcreteUser(mediator, "Charlie");
mediator.addUser(alice);
mediator.addUser(bob);
mediator.addUser(charlie);
alice.send("Hi everyone!");
bob.send("Hello Alice!");
}
}
๐งช Output
๐ค Alice sends: Hi everyone!
๐ฉ Bob receives: Alice: Hi everyone!
๐ฉ Charlie receives: Alice: Hi everyone!
๐ค Bob sends: Hello Alice!
๐ฉ Alice receives: Bob: Hello Alice!
๐ฉ Charlie receives: Bob: Hello Alice!
โจ Benefits
Benefit | Description |
---|---|
โ Loose Coupling | Participants donโt reference each other directly |
โ Centralized Control | All interaction logic lives in the mediator |
โ Easy to Modify Behavior | Add/remove participants or change rules easily |
โ Improves Maintainability | Simplifies debugging and testing communication |
โ ๏ธ When Not to Use
- Too few components โ overhead not worth it
- Simple one-way communication โ
Observer
may be a better fit - When performance is critical โ centralization may cause latency
โ Summary
Aspect | Value |
---|---|
Pattern Type | Behavioral |
Key Intent | Centralized communication control |
Java Features Used | Interfaces, Polymorphism, Composition |
Real-World Analogy | Control Tower, Chatroom, Form Manager |
๐ง Want More?
Would you like:
- โ Event-driven Mediator with Pub/Sub?
- โ UI Form coordination example (enable/disable logic)?
- โ Mediator + Observer hybrid design?
Just say the word and Iโll extend it accordingly.
Top comments (0)