Observer Pattern(观察者模式)

观察者模式应用比较广泛,写一个简单的示例,扩展了一下Head First上面例子的应用范围,进一步降低Observer与Subject之间的耦合度。

简单例子,直接给代码:


  1. package test;  
  2. public interface Subject {  
  3.     public void registerObserver(Observer observer);  
  4.     public void unRegisterObserver(Observer observer);  
  5.     public void notifyAllObservers();  
  6.     public void notifyObserver(Observer observer);  
  7. }  
  8.   
  9. package test;  
  10. public interface Observer {  
  11.     public void registerSubject(Subject subject);  
  12.     public void unRegisterSubject(Subject subject);  
  13.     public void update(Subject subject);  
  14. }  
view plain
  1. package test;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. public class WeatherDataSubject implements Subject {  
  5.       
  6.     List<Observer> observers = new ArrayList<Observer>();  
  7.     private float temperature;  
  8.     private float humidity;  
  9.     private float pressure;  
  10.       
  11.     public float getTemperature() {  
  12.         return temperature;  
  13.     }  
  14.     public void setTemperature(float temperature) {  
  15.         this.temperature = temperature;  
  16.     }  
  17.     public float getHumidity() {  
  18.         return humidity;  
  19.     }  
  20.     public void setHumidity(float humidity) {  
  21.         this.humidity = humidity;  
  22.     }  
  23.     public float getPressure() {  
  24.         return pressure;  
  25.     }  
  26.     public void setPressure(float pressure) {  
  27.         this.pressure = pressure;  
  28.     }  
  29.       
  30.     @Override  
  31.     public void registerObserver(Observer observer) {  
  32.         // TODO Auto-generated method stub  
  33.         if(!observers.contains(observer))  
  34.             observers.add(observer);  
  35.     }  
  36.     @Override  
  37.     public void unRegisterObserver(Observer observer) {  
  38.         // TODO Auto-generated method stub  
  39.         if(observers.contains(observer))  
  40.             observers.remove(observer);  
  41.     }  
  42.     @Override  
  43.     public void notifyAllObservers() {  
  44.         // TODO Auto-generated method stub  
  45.         for(Observer o:observers)  
  46.             o.update(this);  
  47.     }  
  48.     @Override  
  49.     public void notifyObserver(Observer observer) {  
  50.         // TODO Auto-generated method stub  
  51.         if(observers.contains(observer))  
  52.             observer.update(this);  
  53.     }  
  54.       
  55.     public void setWeatherDataParameters(float temperature,float humidity,float pressure)  
  56.     {  
  57.         this.setTemperature(temperature);  
  58.         this.setHumidity(humidity);  
  59.         this.setPressure(pressure);  
  60.         this.notifyAllObservers();  
  61.     }  
  62. }  
view plain
  1. package test;  
  2. public class DisplayPanel implements Observer {  
  3.       
  4.     private float temperature;  
  5.     private float humidity;  
  6.     private float pressure;  
  7.       
  8.     public float getTemperature() {  
  9.         return temperature;  
  10.     }  
  11.     public void setTemperature(float temperature) {  
  12.         this.temperature = temperature;  
  13.     }  
  14.     public float getHumidity() {  
  15.         return humidity;  
  16.     }  
  17.     public void setHumidity(float humidity) {  
  18.         this.humidity = humidity;  
  19.     }  
  20.     public float getPressure() {  
  21.         return pressure;  
  22.     }  
  23.     public void setPressure(float pressure) {  
  24.         this.pressure = pressure;  
  25.     }  
  26.     @Override  
  27.     public void registerSubject(Subject subject) {  
  28.         // TODO Auto-generated method stub  
  29.         subject.registerObserver(this);  
  30.     }  
  31.     @Override  
  32.     public void unRegisterSubject(Subject subject) {  
  33.         // TODO Auto-generated method stub  
  34.         subject.unRegisterObserver(this);  
  35.     }  
  36.     @Override  
  37.     public void update(Subject subject) {  
  38.         // TODO Auto-generated method stub  
  39.         if(subject instanceof WeatherDataSubject)  
  40.         {  
  41.             WeatherDataSubject s = (WeatherDataSubject)subject;  
  42.             this.setTemperature(s.getTemperature());  
  43.             this.setHumidity(s.getHumidity());  
  44.             this.setPressure(s.getPressure());  
  45.         }  
  46.           
  47.         this.display();  
  48.     }  
  49.     public void display() {  
  50.         System.out.println("Temperature:"+this.getTemperature());  
  51.         System.out.println("Humidity:"+this.getHumidity());  
  52.         System.out.println("Pressure:"+this.getPressure());  
  53.     }  
  54. }  
view plain
  1. package test;  
  2. public class Driver {  
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         WeatherDataSubject subject1=new WeatherDataSubject();  
  9.         WeatherDataSubject subject2=new WeatherDataSubject();  
  10.           
  11.         Observer panel1 = new DisplayPanel();  
  12.         Observer panel2 = new DisplayPanel();  
  13.         panel1.registerSubject(subject1);  
  14.         panel1.registerSubject(subject2);  
  15.         panel2.registerSubject(subject2);  
  16.           
  17.         subject1.setWeatherDataParameters(15.0f, 0.32f, 1076.23f);  
  18.         subject2.setWeatherDataParameters(13.5f, 0.42f, 1087.11f);  
  19.         System.out.println("Subject1:======================");  
  20.         subject1.notifyAllObservers();  
  21.         System.out.println("Subject2:======================");  
  22.         subject2.notifyAllObservers();  
  23.     }  
  24. }  

你可能感兴趣的:(Observer Pattern(观察者模式))