首页 > 代码库 > 面向对象-Observer

面向对象-Observer

package oo;

public interface Subject
{
    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObserver();
}

 

package oo;

import java.util.ArrayList;
public class WeatherData implements Subject{
private ArrayList observers;
private float temperature;
private float humidity;
private float pressure;

public WeatherData()
{
observers=new ArrayList();
}

public void registerObserver(Observer o)
{
    observers.add(o);
}

public void removeObserver(Observer o)
{
    int i=observers.indexOf(o);
    if (i>=0)
    {
        observers.remove(i);
    }
}

public void notifyObserver()
{
    for (int i=0;i<observers.size();i++)
    {
        Observer observer=(Observer)observers.get(i);
        observer.update(temperature, humidity, pressure);
    }
}

public void measurementsChanged()
{
    notifyObserver();
}

public void setMeasurements(float temperature,float humidity,float pressure)
{
    this.temperature=temperature;
    this.humidity=humidity;
    this.pressure=pressure;
    measurementsChanged();
}

public float getTemperature()
{
    return temperature;
}

public float getHumidity()
{
    return humidity;
}

public float getPressure()
{
    return pressure;
}

}



 

package oo;

public interface Observer
{
public void update(float temp,float humidity,float pressure);

}

package oo;

public interface DisplayElement {
   void display();
}

 

package oo;

public class StatisticsDisplay implements Observer,DisplayElement{
    private float maxTemp=0.0f;
    private float minTemp=200;
    private float tempSum=0.0f;
    private int numReadings;
    private WeatherData weatherData;
    public StatisticsDisplay(WeatherData weatherData)
    {
        this.weatherData=http://www.mamicode.com/weatherData;
        weatherData.registerObserver(this);
    }
    public void update(float temp,float humidity,float pressure)
    {
        tempSum+=temp;
        numReadings++;
        
        if (temp>maxTemp)
        {
            maxTemp=temp;
        }
        
        if (temp<minTemp)
        {
            minTemp=temp;
        }
        display();
    }
    public void display()
    {
        System.out.println("Avg/Max/Min temperature="+(tempSum/numReadings)+"/"+maxTemp+"/"+minTemp);
    }
}

 

 

package oo;

public class CurrentConditionsDisplay implements Observer,DisplayElement{
    private float temperature;
    private float humidity;
    private Subject weatherData;
    
    public CurrentConditionsDisplay(Subject weatherData)
    {
        this.weatherData=http://www.mamicode.com/weatherData;
        weatherData.registerObserver(this);
    }
    public void update(float temperature,float humidity,float pressure)
    {
        this.temperature=temperature;
        this.humidity=humidity;
        display();
    }
    public void display()
    {
        System.out.println("Current conditions:"+temperature+"F degrees and"+humidity+"%humidity");
    }

}

 

package oo;

public class HeatIndexDisplay implements Observer,DisplayElement
{
    float heatIndex=0.0f;
    private WeatherData weatherData;
    public void HeateIndexDisplay(WeatherData weatherdata)
    {
        this.weatherData=http://www.mamicode.com/weatherData;
        weatherData.registerObserver(this);
    }
    public void update(float t,float rh,float pressure)
    {
        heatIndex=computeHeatIndex(t,rh);
        display();
    }
    private float computeHeatIndex(float t,float rh)
    {
        float index = (float)((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh)
                + (0.00941695 * (t * t)) + (0.00728898 * (rh * rh))
                + (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) +
                (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 *
                (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) +
                (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) +
                0.000000000843296 * (t * t * rh * rh * rh)) -
                (0.0000000000481975 * (t * t * t * rh * rh * rh)));
        return index;
    }
    public void display()
    {
        System.out.println("Heat index is"+heatIndex);
    }
    

}

 

package oo;

public class ForecastDisplay implements Observer,DisplayElement{
private float currentPressure=29.92f;
private float lastPressure;
public ForecastDisplay(WeatherData weatherData)
{
    weatherData.registerObserver(this);
}

public void update(float temp,float humidity,float pressure)
{
    lastPressure=currentPressure;
    currentPressure=pressure;
    display();
}

public void display()
{
    System.out.println("Forecast:");
    if (currentPressure>lastPressure)
    {
        System.out.println("Improving weather on the way!");
        
    }
    else if (currentPressure==lastPressure)
    {
        System.out.println("More of the same");
    }
    else if(currentPressure<lastPressure)
    {
        System.out.println("Watch out for cooler, rainy weather");
    }
}
}

 

package oo;

public class WeatherStation {
    public static void main(String []args)
{
        WeatherData weatherData=http://www.mamicode.com/new WeatherData();
        CurrentConditionsDisplay currentDisplay=new CurrentConditionsDisplay(weatherData);
        StatisticsDisplay statisticsDisplay=new StatisticsDisplay(weatherData);
        ForecastDisplay forecastDisplay=new ForecastDisplay(weatherData);    
        weatherData.setMeasurements(80,33,32);
        weatherData.setMeasurements(84,54,34);
        weatherData.setMeasurements(23,23,12);
        
}
}

 

package oo;

public class WeatherStationHeadIndex {
public static void main(String[]args)
{
    WeatherData weatherData=http://www.mamicode.com/new WeatherData();
    CurrentConditionsDisplay currentDisplay=new CurrentConditionsDisplay(weatherData);
    StatisticsDisplay statisticsDisplay=new StatisticsDisplay(weatherData);
    ForecastDisplay forecastDisplay=new ForecastDisplay(weatherData);
    HeatIndexDisplay heatIndezDisplay=new HeatIndexDisplay();
    weatherData.setMeasurements(80,34,23);
    weatherData.setMeasurements(34,654,12);
    weatherData.setMeasurements(2,12,543);
}}

 

面向对象-Observer