back home

Observer Pattern

[behavioral-patterns][design-patterns]

The Observer pattern suggests that you add a subscription mechanism to the publisher class so individual objects can subscribe to or unsubscribe from a stream of events coming from that publisher.

We have two entities - a subscriber and a subject/publisher. The subscriber initiates contact by subscribing to the publisher. The publisher then stores a reference to the subscriber and every time the publisher has some changes, it pushes them to the subscriber by a previously declared interface - a callback for example.