Pattern · Core

Observer

Define a one-to-many dependency: when one object changes, all its subscribers are notified automatically — without it knowing who they are.

Asked atAmazonMetaGoogle
step 1 / 7
1/2The wiring
observers*
NewsAgency
- subs: List
+ subscribe(s)
+ publish(news)
«interface»
Subscriber
+ update(news)
EmailSub
+ update()
SmsSub
+ update()
AppSub
+ update()
The wiring
1interface Subscriber { void update(String news); }
2class NewsAgency { List<Subscriber> subs; void publish(String n){...} }
3class EmailSub implements Subscriber { ... } // + SmsSub, AppSub
State
knowsSubscriber interface only

The NewsAgency (Subject) keeps a list of Subscribers and exposes subscribe() + publish(). It holds them as the interface, never concrete types.