import java.util.*; class GTM{ public interface TemperatureObserver { void observed(double temperature); void noRead(); boolean checkReading(double temp); } public List observers = new ArrayList(); public void startMonitoring(final TemperatureSensor tr, final long pause){ Thread monitorThread = new Thread(){ public void run(){ while(true){ try{ Thread.sleep(pause);}catch(InterruptedException ie){}; try{ double temp = tr.readTemperature(); for(TemperatureObserver to:observers) if (to.checkReading(temp) ) to.observed( temp ); }catch(TemperatureSensor.SensorError re){ for(TemperatureObserver to:observers) to.noRead(); } } } }; monitorThread.start(); } public static GTM getMeOne(){ GTM gm = new GTM(); TemperatureSensor ts = new TemperatureSensor(); gm.startMonitoring(ts, 1000); return gm; } }