The observer pattern is a software design pattern in which an object, called the subject, maintains a list of
its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems. Observer is also a key part in the familiar MVC architectural pattern.
Related patterns: Publish–subscribe pattern, mediator, singleton.
The essence of the Observer Pattern is to “Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. “.
Below is an example that takes keyboard input and treats each input line as an event. The example is built upon the library classes java.util.Observer  and java.util.Observable . When a string is supplied from System.in, the method notifyObservers is then called, in order to notify all observers of the event’s occurrence, in the form of an invocation of their ‘update’ methods – in our example,ResponseHandler.update(…).
The file MyApp.java contains a main() method that might be used in order to run the code.
Main test program :