http://javainsimpleway.com/observer-pattern/
https://www.tutorialspoint.com/design_pattern/observer_pattern.htm
Observer Pattern is one of the behavioural design patterns. It defines a one-to-many dependency between objects so that when one object changes its state, all of its dependents are notified and updated automatically.
Subject : The object which is being watched is called the subject. It contains a list of all observers to whom notification has to be made for any change in the state of Subject. Subject should provide a method using which observers can register/unregister themselves for the notification of subject’s state change. Subject should also provide a method to notify all the observers if any change in its state.
Observer (or) Listener : The objects which are watching the state changes are called observers or listeners. Observer should provide a method to set the object to watch the Subject’s state change. Observer should also provide another method that will be used by Subject to notify Observer for any change in its state.
Java provides inbuilt mechanism for implementing Observer pattern through java.util.Observable class and java.util.Observer interface.
(Event is the subject and listener is the observer. There can be many listeners for one event.)

Subject.java
import java.util.ArrayList;
import java.util.List;
public class Subject {
private List<Observer> observers = new ArrayList<Observer>();
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
notifyAllObservers();
}
public void attach(Observer observer){
observers.add(observer);
}
public void notifyAllObservers(){
for (Observer observer : observers) {
observer.update();
}
}
}Observer.java
public abstract class Observer {
protected Subject subject;
public abstract void update();
}BinaryObserver.java
public class BinaryObserver extends Observer{
public BinaryObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Binary String: " + Integer.toBinaryString( subject.getState() ) );
}
}OctalObserver.java
public class HexaObserver extends Observer{
public HexaObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Hex String: " + Integer.toHexString( subject.getState() ).toUpperCase() );
}
}HexaObserver.java
public class HexaObserver extends Observer{
public HexaObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Hex String: " + Integer.toHexString( subject.getState() ).toUpperCase() );
}
}ObserverPatternDemo.java
public class ObserverPatternDemo {
public static void main(String[] args) {
Subject subject = new Subject();
new HexaObserver(subject);
new OctalObserver(subject);
new BinaryObserver(subject);
System.out.println("First state change: 15");
subject.setState(15);
System.out.println("Second state change: 10");
subject.setState(10);
}
}Output
First state change: 15
Hex String: F
Octal String: 17
Binary String: 1111
Second state change: 10
Hex String: A
Octal String: 12
Binary String: 1010
