Bird
0
0

Consider this extended handler class:

medium📝 Analysis Q5 of 15
LLD - Behavioral Design Patterns — Part 1
Consider this extended handler class:
class Handler:
    def __init__(self, next_handler=None):
        self.next_handler = next_handler
    def handle(self, request):
        if request == "A":
            return "Handled A"
        elif self.next_handler:
            return self.next_handler.handle(request)
        else:
            return "Not handled"

class SpecialHandler(Handler):
    def handle(self, request):
        if request == "B":
            return "Handled B"
        else:
            return super().handle(request)

chain = SpecialHandler(Handler())
print(chain.handle("B"))
What is the printed output?
A"Handled A"
B"Handled B"
C"Not handled"
DError at runtime
Step-by-Step Solution
Solution:
  1. Step 1: Identify which handler processes request "B"

    SpecialHandler overrides handle to check for "B" and returns "Handled B" if matched.
  2. Step 2: Confirm chain behavior

    Since request is "B", SpecialHandler handles it directly, so output is "Handled B".
  3. Final Answer:

    "Handled B" -> Option B
  4. Quick Check:

    Subclass handles specific request "B" = "Handled B" [OK]
Quick Trick: Subclass can override handle to process specific requests [OK]
Common Mistakes:
MISTAKES
  • Assuming base handler handles "B"
  • Forgetting to call super() in subclass
  • Expecting runtime errors without cause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes