Bird
0
0

Find the bug in this command pattern example:

medium📝 Analysis Q7 of 15
LLD - Behavioral Design Patterns — Part 1
Find the bug in this command pattern example:
class Command:
    def execute(self):
        pass

class LightOnCommand(Command):
    def __init__(self, light):
        self.light = light
    def execute(self):
        self.light.turn_on()

class Light:
    def turn_on(self):
        print("Light is on")

light = Light()
command = LightOnCommand(light)
command.execute
ACommand class should not have an execute method
BLight class lacks a turn_off method
Cexecute method is not called (missing parentheses)
DLightOnCommand should inherit from Light
Step-by-Step Solution
Solution:
  1. Step 1: Check how execute is used

    execute is referenced but not called because parentheses are missing.
  2. Step 2: Verify other options

    turn_off is not required here; Command having execute is correct; inheritance is correct.
  3. Final Answer:

    execute method is not called (missing parentheses) -> Option C
  4. Quick Check:

    Method call needs parentheses [OK]
Quick Trick: Always call methods with parentheses () [OK]
Common Mistakes:
MISTAKES
  • Ignoring missing parentheses on method call
  • Thinking inheritance is wrong
  • Expecting unrelated methods in Light

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes