Design: Mediator Pattern Implementation
Design the mediator pattern structure and communication flow between components. Out of scope: persistence, distributed system scaling, UI design.
Functional Requirements
Non-Functional Requirements
Jump into concepts and practice - no test required
+-------------------+
| Mediator |
| (central hub) |
+---------+---------+
|
+---------------+---------------+
| | |
+---+---+ +---+---+ +---+---+
|Comp A | |Comp B | |Comp C |
+-------+ +-------+ +-------+Mediator pattern in system design?notify(Component sender, String event). Others lack sender info or use incorrect method names.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'));mediator.notify('Component1', 'B'), so it matches the second condition and returns 'Handled B'.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');start() or stop() directly on the sender component.