The Observer pattern, also known as Publish-Subscribe or Event-Listener, is a behavioral GoF design pattern by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides that defines a one-to-many dependency between objects. When one object changes state, all its dependents are notified and updated automatically. The subject does not need to know who its observers are or how many there are, as when a data model updates the UI through events.

GoF-Observer Pattern

Details
Full Name

GoF Observer Pattern (Behavioral)

Also known as

Publish-Subscribe, Event-Listener

Intent:

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

When to Use:

  • When changes to one object require changing others, and you don’t know how many objects need to change

  • When an object should notify others without knowing who they are

Prompt Example:

"Implementiere ein Event-System nach dem GoF-Observer Pattern, damit Änderungen am Datenmodell automatisch die UI aktualisieren."