Skip to main content
InterviewHubPRO
Section Overview
Q10

Question 10 of 50

Part 1: Creational & Structural Patterns (1 - 10)

20%
Design Patterns (GoF) (50 Qs)•Part 1: Creational & Structural Patterns (1 - 10)

What is the Observer Pattern and how does it implement Pub/Sub?

junior LevelCode Exampledesign-patternsCreational

In Simple Words: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically:

  • Subject: Maintains a list of observers; provides subscribe() and unsubscribe().
  • Observer: Defines an update() method called by the Subject.
  • Foundation of event-driven architectures, DOM event listeners, and RxJS streams.
typescript
class Subject {
  private observers: Observer[] = [];

  subscribe(observer: Observer) { this.observers.push(observer); }
  notify(data: any) { this.observers.forEach(o => o.update(data)); }
}