定義:定義了對(duì)象之間一對(duì)多依賴,這樣一來俺陋,當(dāng)一個(gè)對(duì)象改變狀態(tài)時(shí)庆锦,它的所有依賴都會(huì)收到通知并自動(dòng)更新
類圖
案例:布告板實(shí)時(shí)顯示溫度辫继。
建立主題接口
public interface Subject {
void registerObserver(Observer O);
void removeObserver(Observer O);
void notifyObserver();
}
建立觀察者接口
public interface Observer {
void update(float temp,float humidity,float pessure);
}
建立顯示接口
public interface DisplayElement {
void display();
}
定義被觀察者
public class WeatherData implements Subject {
private ArrayList<Observer> observers;
private float temperature;
private float humidity;
private float pressure;
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 notifyObserver() {
for (Observer observer:observers){
observer.update(temperature,humidity,pressure);
}
}
public void measurementsChanged(){
notifyObserver();
}
public void setMessurements(float temperature,float humidity,float pressure){
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
};
}
定義觀察者
public class CurrentConditionDisplay implements Observer,DisplayElement {
private float temperature;
private float humidity;
private WeatherData weatherData;
CurrentConditionDisplay(WeatherData weatherData){
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void logOut(){
weatherData.removeObserver(this);
}
@Override
public void display() {
System.out.println("Current conditions :" + temperature+"F degrees and " + humidity+"% humdity");
}
@Override
public void update(float temp, float humidity, float pessure) {
this.temperature = temp;
this.humidity = humidity;
display();
}
}
測(cè)試類
public class Test {
public static void main(String[] args){
WeatherData weatherData = new WeatherData();
CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay(weatherData);
weatherData.setMessurements(80,65,30.4f);
weatherData.setMessurements(83,60,30.1f);
currentConditionDisplay.logOut();
weatherData.setMessurements(85,62,30.3f);
}
}
java內(nèi)置的觀察者模式
java.util包內(nèi)包含最基本的Observer接口和Observable類。
修改后的觀察者類
public class CurrentConditionDisplay implements Observer, DisplayElement {
Observable observable;
private float temperature;
private float humidity;
private WeatherData weatherData;
CurrentConditionDisplay(Observable observable){
this.observable = observable;
observable.addObserver(this);
}
@Override
public void display() {
System.out.println("Current conditions :" + temperature+"F degrees and " + humidity+"% humdity");
}
@Override
public void update(Observable obs,Object arg) {
if(obs instanceof WeatherData){
WeatherData weatherData = (WeatherData) obs;
this.temperature = weatherData.getTemperature();
this.humidity = weatherData.getHumidity();
display();
}
}
}
修改后的被觀察者類
public class WeatherData extends Observable {
private float temperature;
private float humidity;
private float pressure;
public void measurementsChanged(){
setChanged();
notifyObservers();
}
public void setMessurements(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;
}
}
java內(nèi)置觀察者的缺點(diǎn):
1锌半、Observable是一個(gè)類禽车,使得被觀察者無法繼承其他的超類。
2刊殉、Observable將關(guān)鍵方法保護(hù)起來哭当。