Design mode 2: observer mode (push)

Posted by Dodon on Sun, 03 May 2020 10:06:53 +0200

The observer pattern (in this paper, the subject actively pushes messages to the observer and the user-defined observer pattern is not a java built-in pattern) defines one to many dependencies between objects, so that when an object changes state, all its dependencies will be notified and automatically updated.

Design principle: in order to achieve loose coupling design between interactive objects.

Subject interface:

package com.sun;

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

The implementation class WeatherData of the Subject interface:

package com.sun;

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();
    }
	@Override
	public void registerObserver(Observer o) {
        observers.add(o);
	}

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

	@Override
	public void notifyObservers() {
		for (int i = 0; i < observers.size(); i++) {
			Observer oberver = (Observer)observers.get(i);
			oberver.update(temperature, humidity, pressure);
		}
	}
	
	//When we get updated observations from the weather station, we inform the observer
    public void measurementsChanged(){
    	notifyObservers();
    }
    //Use this method to test the billboard
    public void setMeasurements(float temperature,float humidity,float pressure){
    	this.temperature = temperature;
    	this.humidity = humidity;
    	this.pressure = pressure;
    	measurementsChanged();
    }
    
}

Observer interface:

package com.sun;

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

One of the publishing board implementation classes of the Object interface:

package com.sun;

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

}

Interface of DisplayElement bulletin board display:

package com.sun;

public interface DisplayElement {
   public void display();
}

Test class WeatherStation:

package com.sun;

public class WeatherStation {
	public static void main(String[] args) {
		WeatherData weatherData = new WeatherData();
		CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);
		weatherData.setMeasurements(80, 65, 30.4f);
		weatherData.setMeasurements(82, 70, 29.2f);
	}
}

Topics: Java