Bird
0
0

In the following code, what is the main issue that prevents observers from receiving notifications?

medium📝 Analysis Q14 of 15
LLD - Design — Online Shopping Cart
In the following code, what is the main issue that prevents observers from receiving notifications?
class Subject:
    def __init__(self):
        self.observers = set()
    def subscribe(self, observer):
        self.observers.add(observer)
    def notify(self, message):
        for obs in self.observers:
            obs.receive(message)

class Observer:
    def update(self, message):
        print(f"Got: {message}")

subject = Subject()
obs1 = Observer()
subject.subscribe(obs1)
subject.notify("Update")
AObservers are stored in a set instead of a list
BMethod notify calls obs.receive but Observer has update method
Csubscribe method uses add instead of append
DObserver class is missing
Step-by-Step Solution
Solution:
  1. Step 1: Check method called in notify

    notify() calls obs.receive(message).
  2. Step 2: Check Observer class methods

    Observer defines update(message), but no receive() method.
  3. Final Answer:

    Method notify calls obs.receive but Observer has update method -> Option B
  4. Quick Check:

    Method name mismatch causes AttributeError [OK]
Quick Trick: Check method names called vs defined in observers [OK]
Common Mistakes:
  • Confusing set vs list for storing observers
  • Ignoring method name mismatches
  • Assuming missing class when it exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes