Bird
0
0

Which of the following is the correct way to register an observer in the Observer pattern?

easy🧠 Conceptual Q12 of 15
LLD - Behavioral Design Patterns — Part 1

Which of the following is the correct way to register an observer in the Observer pattern?

subject = Subject()
observer = ConcreteObserver()
# What code registers the observer?
Asubject.addObserver(observer)
Bobserver.subscribe(subject)
Cobserver.register(subject)
Dsubject.attach(observer)
Step-by-Step Solution
Solution:
  1. Step 1: Recall common Observer pattern method names

    Typically, the subject has a method named attach or addObserver to register observers.
  2. Step 2: Identify the most standard method

    While addObserver is used in some languages, attach is the classic and widely accepted method name.
  3. Final Answer:

    subject.attach(observer) -> Option D
  4. Quick Check:

    Register observer = subject.attach(observer) [OK]
Quick Trick: Subject.attach(observer) is classic registration [OK]
Common Mistakes:
MISTAKES
  • Calling register on observer instead of subject
  • Using subscribe which is not standard here
  • Confusing addObserver with observer methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes