Wzorzec Observer definiuje zależność jeden-do-wielu, gdzie wiele obiektów (obserwatorów) jest automatycznie powiadamianych, gdy inny obiekt (subject) zmienia stan. Umożliwia on zdecentralizowaną, opartą na zdarzeniach komunikację — szeroko stosowaną w interfejsach użytkownika, systemach zdarzeń i programowaniu reaktywnym.
Co robi wzorzec Observer
OBSERVER → a SUBJECT maintains a list of OBSERVERS and NOTIFIES them on state changes:
→ observers SUBSCRIBE to the subject
→ when the subject changes → it notifies ALL subscribed observers automatically
→ observers react to the notification (update themselves)
→ one-to-many: one subject, many observers; DECOUPLED (subject doesn't know observer details)
