Para mais detalhes Vejam as classes abaixo:
Interface Observer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package observer;
/**
*
* @author toucatronic
*/
public interface Observer {
public void update(int temp);
}
Classe Usuario
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package modelo;
import observer.Observer;
/**
*
* @author toucatronic
*/
public class Usuario implements Observer{
String nome;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Override
public void update(int temp) {
System.out.println(getNome() +" - A temperatura mudou para "+temp);
}
}
Classe Forno
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package modelo;
import java.util.ArrayList;
import observer.Observer;
/**
*
* @author toucatronic
*/
public class Forno {
int temperatura;
ArrayList<Observer> listaObserver = new ArrayList<>();
public void mudaTemperatura(int estado) {
this.temperatura = estado;
notifica();
}
public void addObserver(Observer observer){
listaObserver.add(observer);
}
public void notifica(){
for (Observer observer : listaObserver) {
observer.update(temperatura);
}
}
}
Abaixo esta o projeto no netbeans 7.2.1 com a classe main