Bird
Raised Fist0

Which modification to the thread-safe observer pattern implementation below correctly supports this requirement without breaking thread safety or notification correctness?

hard🎤 Interviewer Follow-up Q15 of Q15
OOP & Design Patterns - Observer Pattern - Event System, Publish-Subscribe
Suppose you want to extend the observer pattern to allow observers to receive multiple notifications for the same event type without unsubscribing (i.e., observers can be registered multiple times for the same event). Which modification to the thread-safe observer pattern implementation below correctly supports this requirement without breaking thread safety or notification correctness? Options: A) Change the observers data structure from a set to a list per event type and allow duplicates. B) Keep using a set but add a counter for each observer to track multiple subscriptions. C) Use a dictionary mapping observers to their subscription counts per event type. D) Use a queue per event type to enqueue notifications and process them asynchronously.
AKeep using a set but add a counter for each observer to track multiple subscriptions.
BChange the observers data structure from a set to a list per event type and allow duplicates.
CUse a dictionary mapping observers to their subscription counts per event type.
DUse a queue per event type to enqueue notifications and process them asynchronously.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    Observers can subscribe multiple times to the same event type and should receive multiple notifications accordingly.
  2. Step 2: Evaluate data structure changes

    Using a set alone disallows duplicates. A list allows duplicates but is not thread-safe and inefficient for removals. A dictionary mapping observers to counts tracks multiple subscriptions safely.
  3. Step 3: Choose the best approach

    Adding a counter per observer in the set (Keep using a set but add a counter for each observer to track multiple subscriptions.) or using a dictionary (Use a dictionary mapping observers to their subscription counts per event type.) can work, but adding a counter per observer in the set is simpler and keeps thread safety with minimal changes.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Counting subscriptions per observer preserves multiple notifications safely [OK]
Quick Trick: Track subscription counts to allow multiple notifications [OK]
Common Mistakes:
MISTAKES
  • Using list causes concurrency issues and inefficient removals
Trap Explanation:
PITFALL
  • Lists allow duplicates but break thread safety and efficient unsubscribe; sets disallow duplicates.
Interviewer Note:
CONTEXT
  • Tests candidate's ability to adapt observer pattern for advanced subscription semantics.
Master "Observer Pattern - Event System, Publish-Subscribe" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes