Java设计模式 -- 观察者模式

观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。

当两个对象之间松耦合,他们依然可以交互,但是不太清楚彼此的细节。观察者模式提供了一种对象设计让主题和观察者之间松耦合。

我们来看看一种push方式的 观察者模式。

首先是观察者 主题 等的接口。


//主题
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}

//观察者
public interface Observer {

public void update(float temp, float humidity, float pressure);
}
//other 操作
public interface DisplayElement {

public void display();
}

//主题
public interface Subject
{
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}

//观察者
public interface Observer
{
public void update(float temp, float humidity, float pressure);
}

//other 操作
public interface DisplayElement
{
public void display();
}



实现这些接口。


//实现主题
public class WeatherData implements Subject{
private ArrayList observers;
private float temperature;
private float humidity;
private float pressure;

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

public void notifyObservers() {
// TODO Auto-generated method stub
for(int i=0, len=observers.size(); i < len; i++)
{
Observer observer = (Observer) observers.get(i);
observer.update(temperature, humidity, pressure );
}
}
public void registerObserver(Observer o) {
// TODO Auto-generated method stub
observers.add(o);
}
public void removeObserver(Observer o) {
// TODO Auto-generated method stub
int i = observers.indexOf(o);
if(i >= 0)
{
observers.remove(i);
}
}

public void measurementsChange()
{
notifyObservers();
}

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

//实现观察者
public class CurrentConditionsDisplay implements Observer, DisplayElement{
private float temperature;
private float humidity;
private Subject weatherData;

public CurrentConditionsDisplay(Subject weatherData)
{
this.weatherData = weatherData;
weatherData.registerObserver(this);
}

public void update(float temp, float humidity, float pressure) {
// TODO Auto-generated method stub
this.temperature = temp;
this.humidity = humidity;
display();
}
public void display() {
// TODO Auto-generated method stub
System.out.println("Current conditions: "+ temperature + "F degrees and " + humidity + "% humidity");
}
}




//实现主题
public class WeatherData implements Subject
{

private ArrayList observers;
private float temperature;
private float humidity;
private float pressure;

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

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

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 measurementsChange()
{
notifyObservers();
}

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

//实现观察者
public class CurrentConditionsDisplay implements Observer, DisplayElement
{
private float temperature;
private float humidity;
private Subject weatherData;

public CurrentConditionsDisplay(Subject weatherData)
{
this.weatherData = weatherData;
weatherData.registerObserver(this);
}

public void update(float temp, float humidity, float pressure)
{
this.temperature = temp;
this.humidity = humidity;
display();
}

public void display()
{
System.out.println("Current conditions: "+ temperature + "F degrees and " + humidity + "% humidity");
}
}

实现完了这些接口,测试观察者模式


public class WeatherStation {
public static void main(String[] args)
{
WeatherData weatherData = new WeatherData();

CurrentConditionsDisplay display = new CurrentConditionsDisplay(weatherData);

weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 76, 30.2f);
weatherData.setMeasurements(78, 90, 29.4f);
}
}

你可能感兴趣的:(JAVA,设计模式,Java,F#)