Bird
0
0

What is wrong with this Visitor pattern code snippet?

medium📝 Analysis Q7 of 15
LLD - Behavioral Design Patterns — Part 2
What is wrong with this Visitor pattern code snippet?
class ElementA {
  accept(visitor) {
    visitor.visitElementA();
  }
}

class Visitor {
  visitElementA(element) {
    console.log('Visited A');
  }
}
AVisitor method should not take parameters
Baccept does not pass element to visitor method
Caccept should return a value
DVisitor class should be abstract
Step-by-Step Solution
Solution:
  1. Step 1: Check accept method call

    accept calls visitor.visitElementA() without passing 'this' element.
  2. Step 2: Verify visitor method signature

    visitElementA expects an element parameter, but none is passed.
  3. Final Answer:

    accept does not pass element to visitor method -> Option B
  4. Quick Check:

    Visitor method needs element argument [OK]
Quick Trick: Visitor methods usually require element parameter [OK]
Common Mistakes:
MISTAKES
  • Thinking visitor methods take no parameters
  • Assuming accept must return value
  • Believing Visitor must be abstract

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes