ITeye博客

行为型模式-观察者模式(Observer)

zhoumin007 · 2007-06-30 23:17 · Markdown

一:定义:

Observer:Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

二:引入

一个气象观测站的例子:

 

 

public class WeatherData ...{
    private CurrentConditionsDisplay currentConditionsDisplay;
    private ForecastDisplay forecastDisplay;
    private StatisticsDisplay statisticsDisplay;
    public WeatherData(CurrentConditionsDisplay currentConditionsDisplay,ForecastDisplay forecastDisplay,StatisticsDisplay statisticsDisplay)
    ...{
        this.currentConditionsDisplay=currentConditionsDisplay;
        this.forecastDisplay=forecastDisplay;
        this.statisticsDisplay=statisticsDisplay;
    }
    public void mesurementsChanged()
    ...{
        float temp=getTemperature();
        float humidity=getHumidity();
        float pressure=getPressure();
        
        currentConditionsDisplay.update(temp,humidity,pressure);
        forecastDisplay.update(temp,humidity,pressure);
        statisticsDisplay.update(temp,humidity,pressure);
    }
    
    public float getTemperature()
    ...{
        return 30;
    }
    
    public float getHumidity()
    ...{
        return 60;
    }
    
    public float getPressure()
    ...{
        return 78;
    }
}



public class CurrentConditionsDisplay extends Display ...{
    public  void update(float temp,float humidity,float pressure)
    ...{
        System.out.println("CurrentConditionsDisplay  temp="+temp+",humidity="+humidity+",pressure="+pressure);
    }
}


public class Client ...{

    public static void main(String[] args) ...{
         CurrentConditionsDisplay currentConditionsDisplay=new CurrentConditionsDisplay();
         ForecastDisplay forecastDisplay=new ForecastDisplay();
         StatisticsDisplay statisticsDisplay=new StatisticsDisplay();
         
         WeatherData weatherData=new WeatherData(currentConditionsDisplay,forecastDisplay,statisticsDisplay);
         weatherData.mesurementsChanged();
    }

}

问题:

耦合太紧,如果增加新的Display,需修改接口和实现。

observer模式:

public interface Subject ...{
    public void registerObserver(Observer observer);
    public void removeObserver(Observer observer);
    public void notifyObservers();
}

public class WeatherData implements Subject ...{
    private List observerList=new ArrayList();
    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData(float temperature,float humidity,float pressure)
    ...{
        this.temperature=temperature;
        this.humidity=humidity;
        this.pressure=pressure;
    }
    public void mesurementsChanged()
    ...{
        notifyObservers();
        
    }
        
    public void registerObserver(Observer observer) ...{
        observerList.add(observer);
        
    }
    public void removeObserver(Observer observer) ...{
        observerList.remove(observer);        
    }
    
    public void notifyObservers() ...{
        for(Iterator iter=observerList.iterator();iter.hasNext();)
        ...{
            Observer observer=(Observer)iter.next();
            observer.update(temperature,humidity,pressure);
        }
        
    }
    public float getHumidity() ...{
        return humidity;
    }
    public void setHumidity(float humidity) ...{
        this.humidity = humidity;
    }
    public float getPressure() ...{
        return pressure;
    }
    public void setPressure(float pressure) ...{
        this.pressure = pressure;
    }
    public float getTemperature() ...{
        return temperature;
    }
    public void setTemperature(float temperature) ...{
        this.temperature = temperature;
    }    
}

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

public class CurrentConditionsDisplay implements Observer ...{
    public  void update(float temp,float humidity,float pressure)
    ...{
        System.out.println("CurrentConditionsDisplay  temp="+temp+",humidity="+humidity+",pressure="+pressure);
    }
}

public class Client ...{

    public static void main(String[] args) ...{
        WeatherData weatherData=<spa>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。