The Observer-Observable pattern:
A design pattern that allows a function to run when a certain event is triggered. It is a reactive design that is different compared to a polling or a time based retrieval triggered design ( think of linux cronjob for example ). It uses a “wait and react” concept.
First let us define what is an Observer and Observable:
1. Observer – The object that will Observe the events and trigger the actions and functions that are defined in the registered Observable. In the design, Observable objects are to be added/registered in the Observer object. The Observer serves as the “Controller” of this design.
2. Observable – The object that contains the function to be executed. But this function will only be called if, a.) It is triggered by the Observer, and b.) If this Observable is registered in this Observer.
Real scenario of an Observer-Observable design:
This design is mostly used on integration of modules. A module/system will just register to another module/system. The registered module/system ( the Observable ) will wait for a trigger from the ( Observer ) where it is registered. The design has a loose coupling and therefore no repeating loop or pooling is implemented between the Observer and Observable.
Implementation in Java
To create the Observable, just extend the Observable to your class. Then under this Observable class, call setChanged(); and notifyObservers(); in the function that will be triggered by the Observer.
To create the Observer, just extend the Observer to your class. Create an update() function in this Observer which will then be called whenever there is a call to the Observable class that contains the setChanged(); and notifyObservers(); functions.
To bind the Observer and Observable, register the Observable in the Observer using the add(); function with the Observable object as the parameter.
Very simple example from the internet.