Bird
Raised Fist0

In a typical observer pattern, which method signature correctly defines the function to update observers when the subject's state changes?

easy🧠 Conceptual Q3 of Q15
LLD - Design — Online Shopping Cart
In a typical observer pattern, which method signature correctly defines the function to update observers when the subject's state changes?
Adef notify(self):
Bdef update(self, subject):
Cdef subscribe(self, observer):
Ddef add_observer(self, observer):
Step-by-Step Solution
Solution:
  1. Step 1: Understand observer pattern roles

    The observer has an update method that the subject calls to notify changes.
  2. Step 2: Identify correct method signature

    The update method typically takes the subject as a parameter to access its state.
  3. Final Answer:

    def update(self, subject): -> Option B
  4. Quick Check:

    Update method with subject parameter [OK]
Quick Trick: Update method usually takes subject as parameter [OK]
Common Mistakes:
MISTAKES
  • Confusing notify with update method
  • Using add_observer as notification method
  • Omitting subject parameter in update

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes