Suppose we’ve a system like thermometer that measures the room temperature

public class Thermometer {
    
    private int temp = 0;
    
    public void setTemp(int temp) {
        this.temp = temp;
	System.out.println("Subject | temp = " + temp);
    }

    public int getTemp() {
        return temp;
    }
    
}

We want to get notified when every change in temperature happens.
Suppose that many other systems wants to be notified and these systems may varies and we want to make a good solution to handle this without the need to modify our code multiple times to handle new added systems

so, these systems needs two things
to be notified when temperature changes
get the new value of temperature
we should force these systems to implement a method for receiving temperature updates.
so every system should implement Observer interface

public interface Observer {
    public abstract void update(int temp);
}

One of the systems is an Air Conditioner

public class AirConditioner implements Observer {

    @Override
    public void update(int temp) {
	if(temp > 30)
	    System.out.println("AirConditioner | Colding mode");
	else if(temp < 20)
	    System.out.println("AirConditioner | heating mode");
    }
    
}

The other system is Temperature Alarm

public class TemperatureAlarm implements Observer {

    @Override
    public void update(int temp) {
	if(temp > 50)
	    System.out.println("TemperatureAlarm | Caution temprature is " + temp);
    }
    
}

Now we should change our Subject (Thermometer) to include an array/list of observers

    
    private List observers = new ArrayList();
    
    public void addObserver(Observer observer)
    {
        observers.add(observer);
    }
    
    public void removeObserver(Observer observer)
    {
        observers.remove(observer);
    }
    
    public void notifyObservers()
    {
        for(Observer observer : observers)
        {
            observer.update(temp);
        }
    }

our final class should be like this

public class Thermometer {
    
    private List observers = new ArrayList();
    private int temp = 0;
    
    public void addObserver(Observer observer)
    {
        observers.add(observer);
    }
    
    public void removeObserver(Observer observer)
    {
        observers.remove(observer);
    }
    
    public void notifyObservers()
    {
        for(Observer observer : observers)
        {
            observer.update(temp);
        }
    }

    public void setTemp(int temp) {
        this.temp = temp;
	System.out.println("Subject | temp = " + temp);
        notifyObservers();
    }

    public int getTemp() {
        return temp;
    }
    
}

To test our system

	Thermometer thermometer = new Thermometer();
        TemperatureAlarm temperatureAlarm = new TemperatureAlarm();
	AirConditioner airConditioner = new AirConditioner();
        thermometer.addObserver(temperatureAlarm);
	thermometer.addObserver(airConditioner);
	
	thermometer.setTemp(40);
	thermometer.setTemp(5);
	thermometer.setTemp(15);
	thermometer.setTemp(60);
	thermometer.setTemp(-10);