Behavioral

Observer

Defines a subscription mechanism to notify multiple objects about events.

Event systemsUI updatesPub/sub messaging

Understanding Observer

The Observer pattern defines a one-to-many dependency between objects. When one object changes state, all its dependents are notified automatically. Also known as Pub/Sub.

How It Works

Subject
Publisher
state: "old"
Observers
Observer A
Observer B
Observer C
Subject holds state
1

Subject

Subject maintains state and list of observers.

1 / 4

Basic Implementation

Simple subject with multiple observers:

main.go
Loading editor...

Real-World Example: Stock Market

Stock price notifications with traders and alert bots:

main.go
Loading editor...