Looking at what is going on around the world, I always feel, the reality is more exciting than fiction. Lots of events! We react to some of them while ignoring the rest. But, how do we come to know about them, in the first place?
Like many of us, I too subscribed to a news portal. They run channels on matters like politics, sports and etc., I subscribed to the current affairs channel. They gather events from across the world and delivers them directly to my mailbox.
This is a great arrangement! As an observer of current affairs, I get notified of the events as and when they occur. I can carry my other activities without me actively spending time observing the events. Just that I may have to react when I receive the notification from the portal.
This is a nice example of many-to-many asynchronous communication. The Gang of Four explored this as a behavioral design pattern.
The Observer Pattern
The pattern consists of many Observers, Observables, Events and a Subject. The observers subscribe to the subject for events of their interest. The subject notifies the observers as and when an event occurs in the observables.
This is just like how I receive notifications from the new portal. The portal acts as the subject, the whole world is observable, the current affairs are events and I am an observer. Simple, isn’t it?
The beauty is that the observers and observables do not know each other. They do not block each other. There is no limit on the number of events or observers. There is no compulsion on the observer to react to every event.
The pattern is at the center of Event-Driven Architecture, Message-Oriented Architecture and became even more famous with the Reactive Manifesto and its family of frameworks like RxJava, RsJS, RxPy and etc.,