DevPrep

All topics › Design patterns

Observer

Junior PRO

Essence

Observer is a behavioural pattern: a Subject (publisher) keeps a list of subscribers and notifies them when its state changes; the observers know nothing about each other and the subject knows nothing about their concrete types. It is one-to-many feedback without tight coupling. Its one-to-one counterpart is the delegate.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store