Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the main purpose of the Mediator pattern?
The Mediator pattern helps reduce direct dependencies between components by centralizing communication through a mediator object. This makes the system easier to manage and change.
Click to reveal answer
beginner
How does the Mediator pattern improve system design?
It promotes loose coupling by preventing components from referring to each other directly. Instead, they communicate through the mediator, which controls interactions and simplifies maintenance.
Click to reveal answer
intermediate
In the Mediator pattern, what role does the mediator object play?
The mediator acts as a central hub that receives messages from components and decides how to forward or handle them, coordinating the interactions between components.
Click to reveal answer
beginner
Give a real-life example that illustrates the Mediator pattern.
Think of an air traffic controller at an airport. Instead of planes communicating directly, they talk to the controller who manages takeoffs and landings, ensuring safe and organized traffic flow.
Click to reveal answer
intermediate
What is a potential downside of using the Mediator pattern?
The mediator can become a complex and large object if it handles too many interactions, which might make it harder to maintain or understand.
Click to reveal answer
What problem does the Mediator pattern primarily solve?
AEncrypting communication between services
BImproving database query speed
CReducing direct dependencies between components
DManaging user authentication
✗ Incorrect
The Mediator pattern reduces direct dependencies by centralizing communication through a mediator.
In the Mediator pattern, components communicate through:
AA central mediator object
BDirect references to each other
CA shared database
DNetwork sockets
✗ Incorrect
Components send messages to a mediator, which coordinates their interactions.
Which of the following is a real-world analogy for the Mediator pattern?
AAn air traffic controller managing planes
BA chef cooking a meal
CA library where books are stored
DA group chat where everyone talks directly
✗ Incorrect
The air traffic controller acts as a mediator coordinating communication between planes.
What is a risk when using the Mediator pattern extensively?
AComponents become tightly coupled
BMediator becomes a bottleneck or too complex
CCommunication speed increases too much
DSystem becomes less maintainable
✗ Incorrect
The mediator can grow complex and hard to maintain if it handles too many responsibilities.
Which design principle does the Mediator pattern promote?
ATight coupling
BCode duplication
CDirect communication
DLoose coupling
✗ Incorrect
Mediator promotes loose coupling by centralizing communication.
Explain the Mediator pattern and how it helps in managing component interactions.
Think about how components avoid talking directly and use a middleman.
You got /4 concepts.
Describe a real-life example that illustrates the Mediator pattern and why it fits the pattern.
Consider a situation where a central figure manages communication between many parties.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the Mediator pattern in system design?
easy
A. To store data persistently in a database
B. To increase direct communication between all components
C. To replace all components with a single monolithic class
D. To centralize communication between components and reduce dependencies
Solution
Step 1: Understand the role of Mediator
The Mediator pattern acts as a central hub to manage communication between components, avoiding direct links between them.
Step 2: Compare options with Mediator's purpose
To centralize communication between components and reduce dependencies correctly states the purpose: centralizing communication and reducing dependencies. Other options describe unrelated or incorrect behaviors.
Final Answer:
To centralize communication between components and reduce dependencies -> Option D
Quick Check:
Mediator centralizes communication = A [OK]
Hint: Mediator centralizes communication, not direct links [OK]
Common Mistakes:
Thinking Mediator increases direct component communication
Confusing Mediator with data storage patterns
Assuming Mediator merges components into one
2. Which of the following is the correct way to define a Mediator interface in a low-level design?
easy
A. interface Mediator { void notify(Component sender, String event); }
B. class Mediator { void notifyAll(); }
C. interface Mediator { void sendMessage(String message); }
D. class Mediator { void receive(Component sender); }
Hint: Mediator notify method includes sender and event [OK]
Common Mistakes:
Omitting sender parameter in notify method
Using generic sendMessage without context
Naming methods incorrectly for Mediator role
3. Given the following code snippet, what will be the output?
class Mediator {
notify(sender, event) {
if (event === 'A') return 'Handled A';
if (event === 'B') return 'Handled B';
return 'Unknown event';
}
}
const mediator = new Mediator();
console.log(mediator.notify('Component1', 'B'));
medium
A. Handled A
B. Error: notify method missing
C. Handled B
D. Unknown event
Solution
Step 1: Analyze notify method logic
The method returns 'Handled A' if event is 'A', 'Handled B' if event is 'B', else 'Unknown event'.
Step 2: Check the call with event 'B'
The call is mediator.notify('Component1', 'B'), so it matches the second condition and returns 'Handled B'.
Final Answer:
Handled B -> Option C
Quick Check:
Event 'B' returns 'Handled B' [OK]
Hint: Match event string exactly in notify method [OK]
Common Mistakes:
Confusing event 'B' with 'A'
Assuming default case triggers for known events
Expecting error due to missing parameters
4. In the following Mediator implementation, what is the main issue?
class Mediator {
notify(sender, event) {
if (event === 'start') {
sender.start();
} else if (event === 'stop') {
sender.stop();
}
}
}
class Component {
start() { console.log('Started'); }
stop() { console.log('Stopped'); }
}
const mediator = new Mediator();
const comp = new Component();
mediator.notify(comp, 'start');
medium
A. Component class lacks notify method
B. Mediator calls methods on sender directly, creating tight coupling
C. notify method does not handle unknown events
D. Missing constructor in Mediator class
Solution
Step 1: Review Mediator's notify method behavior
The Mediator calls start() or stop() directly on the sender component.
Step 2: Identify design issue
This direct call creates tight coupling between Mediator and Component, defeating the purpose of loose coupling in Mediator pattern.
Final Answer:
Mediator calls methods on sender directly, creating tight coupling -> Option B
Quick Check:
Tight coupling breaks Mediator pattern goal = A [OK]
Hint: Mediator should avoid calling sender methods directly [OK]
Common Mistakes:
Ignoring tight coupling caused by direct calls
Thinking missing notify in Component is an error
Assuming constructor absence causes failure
5. You are designing a chat application where multiple users send messages to each other. Which design using the Mediator pattern best fits this scenario?
hard
A. A ChatRoom mediator receives messages from users and forwards them to the intended recipients.
B. Users store messages locally and synchronize with a database periodically.
C. Each user sends messages directly to all other users without a central controller.
D. Users communicate only through email notifications handled by a separate service.
Solution
Step 1: Understand the chat communication needs
Users need a central place to send and receive messages without direct dependencies on each other.
Step 2: Match with Mediator pattern usage
A ChatRoom mediator receives messages from users and forwards them to the intended recipients. describes a ChatRoom mediator that manages message routing, fitting the Mediator pattern perfectly.
Final Answer:
A ChatRoom mediator receives messages from users and forwards them to the intended recipients. -> Option A
Quick Check:
Central message routing = C [OK]
Hint: Mediator centralizes message routing in chat apps [OK]
Common Mistakes:
Choosing direct user-to-user messaging (no mediator)