Master Object-Oriented Design in Java!
Subscribe to Ram N Java for intuitive programming tutorials, design pattern implementations, and software architecture guides!
🔔 Subscribe to Ram N JavaWhat is the Mediator Design Pattern?
The Mediator Design Pattern is a behavioral design pattern that reduces chaotic dependencies between objects. Instead of allowing objects to communicate directly with one another, the pattern restricts direct connections and forces them to collaborate solely through a central mediator object.
A classic real-world example is a Chat Room application. In a multi-user chat, if every user had to maintain a direct reference to every other participant to send messages, the system would quickly become tightly coupled and impossible to scale.
How the Chat Room Architecture Works
By introducing a mediator, communication flows through a centralized coordinator:
- Colleague (User): Each chat participant only knows about the chat room mediator, not about the other users directly.
- Mediator (ChatRoom): The central hub responsible for receiving messages from any user and routing them to the intended recipients or broadcasting them across the room.
This transforms a complex many-to-many network of dependencies into a clean, maintainable one-to-many relationship.
Key Benefits of Using a Mediator
Applying this pattern in messaging and UI coordination provides clear architectural advantages:
- Loose Coupling: Individual colleague classes are decoupled and can be reused or modified independently.
- Centralized Control: Interaction logic between components is consolidated into a single mediator class, making communication rules easy to update and audit.
- Simplified Maintenance: Adding new participants or altering routing logic requires changes only within the mediator, leaving existing user classes untouched.
Related Tutorials from Ram N Java
Click here to watch in Youtube : https://www.youtube.com/watch?v=C6LcKHEzruE
Click the below Image to Enlarge
| Mediator Design pattern - Implementation [Chat Room] |
| Mediator Design pattern - Implementation [Chat Room] |
ChatMediator.java
public interface ChatMediator
{
public void sendMessage(String message, User user);
public void addUser(User user);
}
ChatMediatorImpl.java
import java.util.ArrayList;
import java.util.List;
public class ChatMediatorImpl implements ChatMediator
{
private List<User> users;
public ChatMediatorImpl()
{
this.users = new ArrayList<User>();
}
@Override
public void addUser( User user )
{
this.users.add(user);
}
@Override
public void sendMessage( String message, User user )
{
for( User u : this.users )
{
// Message should not be received by the user who is sending it
if( u != user )
{
u.receive(message);
}
}
}
}
User.java
public abstract class User
{
protected ChatMediator chatMediator;
protected String name;
public User( ChatMediator chatMediator, String name )
{
this.chatMediator = chatMediator;
this.name = name;
}
public abstract void send( String message );
public abstract void receive( String message );
}
UserImpl.java
public class UserImpl extends User
{
public UserImpl( ChatMediator chatMediator, String name )
{
super(chatMediator, name);
}
@Override
public void send( String message )
{
System.out.println(this.name + ": Sending Message=" + message+"\n");
chatMediator.sendMessage(message, this);
}
@Override
public void receive( String message )
{
System.out.println(this.name + ": Received Message:" + message);
}
}
ChatClient.java
public class ChatClient
{
public static void main( String[] args )
{
ChatMediator chatMediator = new ChatMediatorImpl();
User ram = new UserImpl(chatMediator, "Ram");
User david = new UserImpl(chatMediator, "David");
User juli = new UserImpl(chatMediator, "Juli");
User steve = new UserImpl(chatMediator, "Steve");
chatMediator.addUser(ram);
chatMediator.addUser(david);
chatMediator.addUser(juli);
chatMediator.addUser(steve);
steve.send("Hello Everybody...Shall we start the meeting?");
}
}
Output
Steve: Sending Message=Hello Everybody...Shall we start the meeting?
Ram: Received Message:Hello Everybody...Shall we start the meeting?
David: Received Message:Hello Everybody...Shall we start the meeting?
Juli: Received Message:Hello Everybody...Shall we start the meeting?