zoukankan      html  css  js  c++  java
  • 观察者模式

    OO原则:

    • 封装变化
    • 多用组合、少用继承
    • 针对接口编程、不针对实现编程

    观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。

    不要依赖于观察者被通知的次序。

     1 import java.util.ArrayList;
     2 
     3 /**
     4  * Created by senki on 2015/3/22.
     5  */
     6 public class WeatherStation {
     7     public static void main(String[] args) {
     8        WeatherData weatherData=new WeatherStation().new WeatherData();
     9        CurrentConditionsDisplay currentDisplay=new WeatherStation().new CurrentConditionsDisplay(weatherData);
    10         weatherData.setMeasurements(80,65,30.4f);
    11         weatherData.setMeasurements(82,70,29.2f);
    12         weatherData.setMeasurements(78,90,29.2f);
    13 }
    14 
    15     public class WeatherData implements Subject{
    16         private ArrayList observers;
    17         private float temperature;
    18         private float humidity;
    19         private float pressure;
    20 
    21         public WeatherData(){
    22             observers=new ArrayList();
    23         }
    24 
    25         @Override
    26         public void registerObserver(Observer o) {
    27             observers.add(o);
    28         }
    29 
    30         @Override
    31         public void removeObserver(Observer o) {
    32             int i=observers.indexOf(o);
    33             if(i>=0)
    34                 observers.remove(i);
    35         }
    36 
    37         @Override
    38         public void notifyObservers() {
    39           for (int i=0;i<observers.size();i++){
    40               Observer observer=(Observer)observers.get(i);
    41               observer.update(temperature,humidity,pressure);
    42           }
    43         }
    44 
    45         public void measurementsChanged(){
    46             notifyObservers();
    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 
    56 
    57     }
    58 
    59     public class CurrentConditionsDisplay implements Observer,DisplayElement{
    60         private float temperature;
    61         private float humidity;
    62         private Subject weatherData;
    63 
    64         public CurrentConditionsDisplay(Subject weatherData){
    65             this.weatherData=weatherData;
    66             weatherData.registerObserver(this);
    67         }
    68 
    69 
    70         @Override
    71         public void display() {
    72             System.out.println("Current conditions:"+temperature+"F degress and"+humidity+"% humidity");
    73         }
    74 
    75         @Override
    76         public void update(float temp, float humidity, float pressure) {
    77            this.humidity=humidity;
    78             this.temperature=temp;
    79             display();
    80         }
    81     }
    82 
    83     public interface Subject{
    84         public void registerObserver(Observer o);
    85         public void removeObserver(Observer o);
    86         public void notifyObservers();
    87     }
    88     public interface Observer{
    89         public void update(float temp,float humidity,float pressure);
    90     }
    91     public interface DisplayElement{
    92         public void display();
    93     }
    94 }
    观察者模式(推模式)
     1 import java.util.Observable;
     2 import java.util.Observer;
     3 
     4 /**
     5  * Created by senki on 2015/3/22.
     6  */
     7 public class WeatherStation2 {
     8     public class WeatherData extends Observable {
     9         private float temperature;
    10         private float humidity;
    11         private float pressure;
    12 
    13         public WeatherData() {
    14         }
    15 
    16         public void measurementsChanged() {
    17             setChanged();
    18             notifyObservers();
    19         }
    20 
    21         public void setMeasurements(float teperature, float humidity, float presure) {
    22             this.temperature = teperature;
    23             this.humidity = humidity;
    24             this.pressure = presure;
    25             measurementsChanged();
    26         }
    27 
    28         public float getTemperature() {
    29             return temperature;
    30         }
    31 
    32         public float getHumidity() {
    33             return humidity;
    34         }
    35 
    36         public float getPressure() {
    37             return pressure;
    38         }
    39     }
    40 
    41     public class CurrentConditionsDisplay implements Observer, WeatherStation.DisplayElement{
    42         Observable observable;
    43         private float temperature;
    44         private float humidity;
    45 
    46         public CurrentConditionsDisplay(Observable observable){
    47             this.observable=observable;
    48             observable.addObserver(this);
    49         }
    50 
    51         @Override
    52         public void display() {
    53             System.out.println("Current conditions:"+temperature+"F degress and"+humidity+"% humidity");
    54         }
    55 
    56         @Override
    57         public void update(Observable o, Object arg) {
    58             if(o instanceof WeatherData){
    59                 WeatherData weatherData=(WeatherData)o;
    60                 this.temperature=weatherData.getTemperature();
    61                 this.humidity=weatherData.getHumidity();
    62                 display();
    63             }
    64         }
    65     }
    66 
    67     public static void main(String[] args) {
    68         WeatherData weatherData=new WeatherStation2().new WeatherData();
    69         CurrentConditionsDisplay currentDisplay=new WeatherStation2().new CurrentConditionsDisplay(weatherData);
    70         weatherData.setMeasurements(80,65,30.4f);
    71         weatherData.setMeasurements(82,70,29.2f);
    72         weatherData.setMeasurements(78,90,29.2f);
    73     }
    74 
    75 }
    观察者模式(拉模式)
  • 相关阅读:
    PHP发红包程序限制红包的大小
    PHP函数
    HTTP Keep-Alive详解[转]
    PHP中PSR
    一级域名、二级域名和二级目录有什么区别?
    mysqli_query(“set nams utf8”)为FALSE
    Mysql中结果集(mysql_result)与Traversable
    Hbase系列(一)---- Hbase究竟是一个怎样的数据库(基础原理)
    java-快速失败与安全失败
    java-hashmap 工作原理
  • 原文地址:https://www.cnblogs.com/goodlucklzq/p/4357740.html
Copyright © 2011-2022 走看看