Bird
Raised Fist0

Identify the issue in this Visitor pattern implementation:

medium📝 Analysis Q6 of Q15
LLD - Behavioral Design Patterns — Part 2
Identify the issue in this Visitor pattern implementation:
class ElementD {
  accept(visitor) {
    visitor.visit(this);
  }
}

class Visitor {
  visitElementD(element) {
    console.log('Visited ElementD');
  }
}
AElementD class must inherit from Visitor.
Baccept method should not pass 'this' to visitor.
CVisitor class should not have visit methods.
DVisitor method name does not match the call in accept.
Step-by-Step Solution
Solution:
  1. Step 1: Check accept method call

    accept calls visitor.visit(this).
  2. Step 2: Check Visitor methods

    Visitor defines visitElementD, but no visit method.
  3. Step 3: Identify mismatch

    Method called in accept does not exist in Visitor, causing error.
  4. Final Answer:

    Visitor method name does not match the call in accept. -> Option D
  5. Quick Check:

    Method names must match [OK]
Quick Trick: accept calls must match visitor method names [OK]
Common Mistakes:
MISTAKES
  • Assuming 'visit' method exists by default
  • Passing wrong parameters
  • Misunderstanding inheritance requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes