Bird
0
0

In a system using the Visitor pattern, you want to perform two different operations on elements without changing their classes. What is the best design approach?

hard📝 Trade-off Q9 of 15
LLD - Behavioral Design Patterns — Part 2
In a system using the Visitor pattern, you want to perform two different operations on elements without changing their classes. What is the best design approach?
ACreate two separate Visitor classes, each implementing one operation
BAdd both operations as methods in the element classes
CUse a single Visitor class with combined logic for both operations
DDuplicate element classes for each operation
Step-by-Step Solution
Solution:
  1. Step 1: Consider multiple operations without modifying elements

    Visitor pattern supports multiple operations via separate Visitor classes.
  2. Step 2: Choose best design

    Creating two Visitor classes keeps operations separate and elements unchanged.
  3. Final Answer:

    Create two separate Visitor classes, each implementing one operation -> Option A
  4. Quick Check:

    Multiple operations = multiple Visitors [OK]
Quick Trick: Use separate Visitors for different operations [OK]
Common Mistakes:
MISTAKES
  • Adding operations to elements
  • Combining operations in one Visitor
  • Duplicating element classes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes