Bird
0
0

Examine this Observer pattern snippet. What is the likely cause if observers are never notified?

medium📝 Analysis Q6 of 15
LLD - Behavioral Design Patterns — Part 1
Examine this Observer pattern snippet. What is the likely cause if observers are never notified?
class Subject:
    def __init__(self):
        self.observers = []
    def attach(self, observer):
        self.observers.append(observer)
    def notify(self, msg):
        for obs in self.observers:
            obs.update(msg)
AObservers are not attached before notify is called
BObservers list is initialized as a set instead of a list
CThe update method is missing in Subject class
DNotify method does not accept any parameters
Step-by-Step Solution
Solution:
  1. Step 1: Check observer registration

    Observers must be attached before notify is called to receive updates.
  2. Step 2: Analyze code snippet

    Observers are stored in a list and notified by calling update.
  3. Step 3: Identify cause of no notification

    If notify is called before attach, no observers exist to notify.
  4. Final Answer:

    Observers are not attached before notify is called -> Option A
  5. Quick Check:

    Attach observers before notify [OK]
Quick Trick: Attach observers before notify call [OK]
Common Mistakes:
MISTAKES
  • Confusing observer storage type
  • Expecting Subject to have update method
  • Ignoring observer registration order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes