观察者模式:定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。 当你试图勾勒观察者模式时,可以利用报纸订阅服务,以及出版者和订阅者比拟这一切。
设计原则:为了交互对象之间的松耦合设计为努力。
工程名称:SubjectAndObsever(eclipse) 下载目录:http://www.cnblogs.com/jrsmith/admin/Files.aspx , SubjectAndObsever.zip
1 package com.jyu.interfaces; 2 3 /** 4 * 主题接口 5 * @author JRSmith 6 * 7 */ 8 public interface Subject { 9 10 /**注册观察者*/ 11 public void registerObsever(Obsever obsever); 12 /**撤销观察者*/ 13 public void removeObsever(Obsever obsever); 14 /**通知观察者*/ 15 public void notifyObsever(); 16 }
1 package com.jyu.interfaces; 2 3 /** 4 * 观察者接口 5 * @author JRSmith 6 * 7 */ 8 public interface Obsever { 9 /**当气象观测值改变时,主题会把这些状态值当做方法的参数,传送给观察者*/ 10 public void update(float temp,float humidity,float pressure); 11 }
1 package com.jyu.interfaces; 2 3 /** 4 * 布告板接口 5 * @author JRSmith 6 * 7 */ 8 public interface DisplayElement { 9 /**当布告板需要显示时,调用此方法*/ 10 public void display(); 11 }
View Code
1 package com.jyu.implement; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.jyu.interfaces.Obsever; 7 import com.jyu.interfaces.Subject; 8 9 public class WeatherData implements Subject { 10 11 private List<Obsever> obsevers; 12 private float temperature; 13 private float humidity; 14 private float pressure; 15 16 public WeatherData(){ 17 obsevers = new ArrayList<Obsever>(); 18 } 19 20 @Override 21 public void registerObsever(Obsever obsever) { 22 obsevers.add(obsever); 23 } 24 25 @Override 26 public void removeObsever(Obsever obsever) { 27 int i = obsevers.indexOf(obsever); 28 if(i>=0){ 29 obsevers.remove(i); 30 } 31 } 32 33 @Override 34 public void notifyObsever() { 35 int size = obsevers.size(); 36 for(int i = 0; i< size; i++){ 37 Obsever obsever = obsevers.get(i); 38 obsever.update(temperature, humidity, pressure); 39 } 40 } 41 42 /** 43 * 当从气象站得到更新观测值时,我们通知观测者 44 */ 45 public void measurementsChanged(){ 46 notifyObsever(); 47 } 48 49 public void setMeasurements(float temperature,float humidity,float pressure){ 50 this.temperature = temperature; 51 this.humidity = humidity; 52 this.pressure = pressure; 53 measurementsChanged(); 54 } 55 }
View Code
1 package com.jyu.implement; 2 3 import com.jyu.interfaces.DisplayElement; 4 import com.jyu.interfaces.Obsever; 5 import com.jyu.interfaces.Subject; 6 7 public class CurrentConditionDisplay implements Obsever, DisplayElement { 8 9 private float temperature; 10 private float humidity; 11 private Subject weatherData; 12 13 /**构造器需要weatherData对象(也就是主题)作为注册之用*/ 14 public CurrentConditionDisplay(Subject weatherData){ 15 this.weatherData = weatherData; 16 weatherData.registerObsever(this); 17 } 18 19 20 @Override 21 public void update(float temp, float humidity, float pressure) { 22 this.temperature = temp; 23 this.humidity = humidity; 24 display(); 25 } 26 27 @Override 28 public void display() { 29 System.out.println("Currnet conditions:"+temperature+"F degree and "+humidity+"%humidity"); 30 } 31 }
1 package com.jyu.test; 2 3 import com.jyu.implement.CurrentConditionDisplay; 4 import com.jyu.implement.WeatherData; 5 6 public class WeatherStation { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) { 12 WeatherData weatherData = new WeatherData(); 13 14 CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay(weatherData); 15 16 weatherData.setMeasurements(80, 65, 30.4f); 17 } 18 19 }