Observer is a very common pattern. You typically use this pattern when you'reimplementing an application with a Model/View/Controller architecture. TheModel/View part of this design is intended to decouple the presentation of data fromthe data itself.Consider, for example, a case where data is kept in a database and can bedisplayed in multiple formats, as a table or a graph. The Observer pattern suggeststhat the display classes register themselves with the class responsible formaintaining the data, so they can be notified when the data changes, and so theycan update their displays.The Java API uses this pattern in the event model of its AWT/Swing classes. It alsoprovides direct support so this pattern can be implemented for other purposes.The Java API provides an Observable class that can be subclassed by objectsthat want to be observed. Among the methods Observable provides are:1.addObserver(Observer o) is called by Observable objects toregister themselves.2.setChanged() marks the Observable object as having changed3.hasChanged() tests if the Observable object has changed.4.notifyObservers() notifies all observers if the Observable objecthas changed, according to hasChanged().To go along with this, an Observer interface is provided, containing a singlemethod that is called by an Observable object when it changes (providing the Observer has registered itself with the Observable class, of course):public void update(Observable o, Object arg)The following example demonstrates how an Observer pattern can be used to notifya display class for a sensor such as temperature has detected a change:import java.util.*;class Sensor extends Observable {private int temp = 68;void takeReading(){double d;d =Math.random();if(d>0.75){temp++;setChanged();}else if (d<0.25){temp--;setChanged();}System.out.print("[Temp: " + temp + "]");}public int getReading(){return temp;}}public class Display implements Observer {public void update(Observable o, Object arg){System.out.print("New Temp: " + ((Sensor) o).getReading());}public static void main(String []ac){Sensor sensor = new Sensor();Display display = new Display();// register observer with observable classsensor.addObserver(display);// Simulate measuring temp over timefor(int i=0; i < 20; i++){sensor.takeReading();sensor.notifyObservers();System.out.println();}}}