Java内置Observer的模式学习

简而言之,观察者模式即为主题--订阅者关系。

主要实现主题类,订阅者即可。如下为用观察者模式实现的天气数据和显示板的示例。

 package ObserverPattern;
 
 import java.util.Observable;
 import java.util.Observer;
 
 /**
  * Observer pattern
  * @author Zheng Haitao
  */
 public class WeatherData extends Observable {
     private String status,pressure,humidity;
     public WeatherData(){
        
     }
     public void onMessureChanged(){
         //no parameters,so adopt "pull" way  to fetch data.
         this.notifyObservers();
     }
     public void setMessurementData(String status,String pressure,String humidity){
         this.humidity = humidity;
         this.pressure = pressure;
         this.status   = status;
         setChanged();
         onMessureChanged();
     }
     public String getStatus(){
         return status;
     }
     public String getPressure(){
         return pressure;
     }
     public String getHumidity(){
         return humidity;
     }
     public static void main(String[] args){
         WeatherData subject = new WeatherData();
        
         bashboard1 b1 = new bashboard1(subject);
         bashboard2 b2 = new bashboard2(subject);
         bashboard3 b3 = new bashboard3(subject);
        
         subject.setMessurementData("sunny", "98", "3.00");
     }
 }
 
 class bashboard1 implements Observer,IDisplay{
     private Observable subject;
     private String pressure;
     public bashboard1(Observable observable){
         this.subject = observable;
         observable.addObserver(this);
     }
     @Override
     public void update(Observable o, Object arg) {
         if (o instanceof WeatherData){
             display();
         }
     }
     public void display(){
         System.out.println("update bashboard1 status: " + pressure);
     }
 }
 class bashboard2 implements Observer,IDisplay {
     private Observable subject;
     private String status;
     public bashboard2(Observable observable){
         this.subject = observable;
         observable.addObserver(this);
     }
     @Override
     public void update(Observable o, Object arg) {
         if (o instanceof WeatherData){
             WeatherData weatherData = (WeatherData)o;
             status = weatherData.getStatus();
             display();
         }
     }
     public void display(){
         System.out.println("update bashboard2 status: " + status);
     }
 }
 class bashboard3 implements Observer,IDisplay {
     private Observable subject;
     private String status;
     private String pressure;
     private String humidity;
     public bashboard3(Observable observable){
         this.subject = observable;
         observable.addObserver(this);
     }
    
     @Override
     public void update(Observable o, Object arg) {
         if (o instanceof WeatherData){
             WeatherData weatherData = (WeatherData)o;
             status = weatherData.getStatus();
             pressure = weatherData.getPressure();
             humidity = weatherData.getHumidity();
             display();
         }
     }
    
     public void display(){
         System.out.println("update bashboard3:" + status +","+ pressure + ","+humidity);
     }
 }
 
 interface IDisplay {
     void display();
 }

你可能感兴趣的:(java)