# Observer Pattern

> One object keeps a list of others. It tells them all when it changes.

**Field:** Software Development  
**Category:** Architecture  
**Also known as:** Observer, Listener Pattern, Event Listener

## Think of it like

Think of it like a shop's mailing list. When new stock comes in, all on the list get told.

## Definition

The observer pattern lets one object tell many others when something about it changes. The object being watched keeps a list of observers. Any part of the program can add itself to that list. When a change happens, the object calls each observer in turn. Event listeners in a web browser are the best-known use of it.

## Example

Lucy's page has a button. Two pieces of code each call addEventListener on it. When the button is clicked, both run.

## Example code

```javascript
button.addEventListener('click', () => save());
button.addEventListener('click', () => track('saved'));
// one click: both functions run
```

Two observers sign up to the same button, and one click runs both.

## Why it matters

The watched object does not need to know who is listening. So new listeners can join with ease.

## Common mistake

Adding observers and never removing them. Each one keeps old objects in memory, which causes leaks.

## Three principles

- **One tells many.** On every change.
- **Sign up to listen.** Add to the list.
- **Remember to leave.** Or memory leaks.

## Related terms

- [Design Pattern](https://learn.benibix.com/term/development/design-pattern)
- [Publish-Subscribe (Pub/Sub)](https://learn.benibix.com/term/development/publish-subscribe)
- [Callback Function](https://learn.benibix.com/term/development/callback)
- [Memory Leak](https://learn.benibix.com/term/development/memory-leak)

---

Source: https://learn.benibix.com/term/development/observer-pattern
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
