java设计模式-观察者模式
观察者模式定义:
观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。
这个主题对象在状态上发生变化时,会通知所有观察者对象,让它们能够自动更新自己。
观察者模式的组成:
1.主题
2.观察者
程序代码例子:
1 package com.ming.mode.observer; 2 3 4 /** 5 * 创建主题接口对象 6 * @author mingge 7 * 8 */ 9 public interface Subject { 10 11 void registerObserver(Observer observer); 12 void removeObserver(Observer observer); 13 void notifyObservers(); 14 }
1 package com.ming.mode.observer; 2 3 /** 4 * 创建观察者接口 5 * @author mingge 6 * 7 */ 8 public interface Observer { 9 10 void update(float t,float b,float ts); 11 }
1 package com.ming.mode.observer; 2 3 public interface DisplayElement { 4 5 void display(); 6 }
1 package com.ming.mode.observer; 2 3 import java.util.ArrayList; 4 5 6 /** 7 * 创建主题接口实现对象 8 * @author mingge 9 * 10 */ 11 public class WeatherData implements Subject{ 12 13 private ArrayList<Observer> observers; 14 15 private float temperature; 16 17 private float humidity; 18 19 private float pressure; 20 21 public WeatherData() { 22 observers=new ArrayList<Observer>(); 23 } 24 25 @Override 26 public void registerObserver(Observer observer) { 27 observers.add(observer); 28 29 } 30 31 @Override 32 public void removeObserver(Observer observer) { 33 int i=observers.indexOf(observer); 34 if(i>=0){ 35 observers.remove(i); 36 } 37 38 } 39 40 @Override 41 public void notifyObservers() { 42 for(int i=0;i<observers.size();i++){ 43 Observer observer=(Observer)observers.get(i); 44 observer.update(temperature, humidity, pressure); 45 } 46 } 47 48 public void measurementsChanged(){ 49 notifyObservers(); 50 } 51 52 public void setMeasurement(float a,float b,float c){ 53 this.temperature=a; 54 this.humidity=b; 55 this.pressure=c; 56 measurementsChanged(); 57 } 58 59 }
1 package com.ming.mode.observer; 2 3 public class CourrentObserver implements Observer,DisplayElement { 4 5 private float temperature; 6 7 private float humidity; 8 9 private Subject weatherData; 10 11 public CourrentObserver(Subject weatherData1) { 12 this.weatherData=weatherData1; 13 weatherData.registerObserver(this); 14 } 15 16 @Override 17 public void display() { 18 System.out.println("temperature:"+temperature+",humidity:"+humidity); 19 20 } 21 22 @Override 23 public void update(float t, float b, float ts) { 24 this.temperature=t; 25 this.humidity=b; 26 display(); 27 28 } 29 30 }
1 package com.ming.mode.observer; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 WeatherData weatherData=new WeatherData(); 7 CourrentObserver o=new CourrentObserver(weatherData); 8 weatherData.setMeasurement(21, 32, 32); 9 weatherData.setMeasurement(20, 31, 32); 10 } 11 }
java内置观察模式的实现
1 package com.ming.mode.observer.inner; 2 3 import java.util.Observable; 4 5 public class WeatherData extends Observable{ 6 7 private float temperature; 8 9 private float humidity; 10 11 private float pressure; 12 13 public WeatherData() { 14 15 } 16 17 public void measurementsChanged(){ 18 setChanged(); 19 notifyObservers(); 20 } 21 22 public void setMeasurements(float t,float b,float c){ 23 this.temperature=t; 24 this.humidity=b; 25 this.pressure=c; 26 measurementsChanged(); 27 } 28 29 public float getTemperature() { 30 return temperature; 31 } 32 33 public void setTemperature(float temperature) { 34 this.temperature = temperature; 35 } 36 37 public float getHumidity() { 38 return humidity; 39 } 40 41 public void setHumidity(float humidity) { 42 this.humidity = humidity; 43 } 44 45 public float getPressure() { 46 return pressure; 47 } 48 49 public void setPressure(float pressure) { 50 this.pressure = pressure; 51 } 52 53 54 55 }
1 package com.ming.mode.observer.inner; 2 3 import java.util.Observable; 4 import java.util.Observer; 5 6 import com.ming.mode.observer.DisplayElement; 7 8 public class CurrentConditionsDisplay implements Observer,DisplayElement{ 9 10 private Observable observable; 11 12 private float temperature; 13 14 private float humidity; 15 16 public CurrentConditionsDisplay(Observable observable){ 17 this.observable=observable; 18 observable.addObserver(this); 19 } 20 21 22 @Override 23 public void display() { 24 System.out.println("temperature:"+temperature+",humidity:"+humidity); 25 26 } 27 28 @Override 29 public void update(Observable o, Object arg) { 30 if(o instanceof WeatherData){ 31 WeatherData w=(WeatherData)o; 32 this.temperature=w.getTemperature(); 33 this.humidity=w.getHumidity(); 34 display(); 35 } 36 } 37 38 39 }
1 package com.ming.mode.observer.inner; 2 public class Test { 3 4 public static void main(String[] args) { 5 WeatherData weatherData=new WeatherData(); 6 CurrentConditionsDisplay o=new CurrentConditionsDisplay(weatherData); 7 weatherData.setMeasurements(21, 32, 32); 8 weatherData.setMeasurements(20, 31, 32); 9 System.out.println("ok"); 10 } 11 }