Bird
0
0

In the following Mediator implementation, what is the main issue?

medium📝 Analysis Q14 of 15
LLD - Behavioral Design Patterns — Part 2
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');
AComponent class lacks notify method
BMediator calls methods on sender directly, creating tight coupling
Cnotify method does not handle unknown events
DMissing constructor in Mediator class
Step-by-Step Solution
Solution:
  1. Step 1: Review Mediator's notify method behavior

    The Mediator calls start() or stop() directly on the sender component.
  2. Step 2: Identify design issue

    This direct call creates tight coupling between Mediator and Component, defeating the purpose of loose coupling in Mediator pattern.
  3. Final Answer:

    Mediator calls methods on sender directly, creating tight coupling -> Option B
  4. Quick Check:

    Tight coupling breaks Mediator pattern goal = A [OK]
Quick Trick: Mediator should avoid calling sender methods directly [OK]
Common Mistakes:
MISTAKES
  • Ignoring tight coupling caused by direct calls
  • Thinking missing notify in Component is an error
  • Assuming constructor absence causes failure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes