Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 335|回复: 0

[设计模式学习]《Head First设计模式》学习-Observer模式

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-6 00:02:23 | 显示全部楼层 |阅读模式
    一、要完成的任务
        此系统中的三个部分是气象站(获取实际气象数据的物理装置)、WeatherData对象(追踪来自气象站的数据,并更新布告板)和布告板(显示目前天气状况给用户看)。
       
         
         
       
       
        二、Observer模式
            
         
            
        1、定义观察者模式
            
        观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。
            
       
         
         
       
       
        2.设计气象站
            
       
         
         
       

       
       
        三、代码实现
            
         
            
        1.定义接口
            
         
            
        (1)Subject接口
        Subject.java
       
         package
          com.sterning.ch2_observer;


         public
          
         interface
          Subject
         
         {
         public void registerObserver(Observer o);
         public void removeObserver(Observer o);
         public void notifyObservers();
    }
         

         
       
       
        (2)Observer接口
        Observer.java
       
         package
          com.sterning.ch2_observer;


         public
          
         interface
          Observer
         
         {
         public void update(float temp,float humidity,float pressure);
    }
         

         
        (3)Displayment接口
        Displayment.java
       
         package
          com.sterning.ch2_observer;


         public
          
         interface
          Displayment
         
         {
         public void display();
    }
         

         
        2.实现接口
            
         
           (1)WeatherData
        WeatherData.java
       
         package
          com.sterning.ch2_observer;


         import
          java.util.ArrayList;


         public
          
         class
          WeatherData
         implements
          Subject
         
         {
         private ArrayList observers;
         private float temperature;
         private float humidity;
         private float pressure;
         
        public WeatherData(){
             observers=new ArrayList();
         }
         
        public void notifyObservers() {
            for(int i=0;i<observers.size();i++){
                 Observer observer=(Observer)observers.get(i);
                 observer.update(temperature, humidity, pressure);
             }
         }

        public void registerObserver(Observer o) {
             observers.add(o);
         }

        public void removeObserver(Observer o) {
             int i=observers.indexOf(o);
            if(i>=0){
                 observers.remove(i);
             }
         }
        public void measurementsChanged(){
             notifyObservers();
         }
        public void setMeasurements(float temperature,float humidity,float pressure){
             this.temperature=temperature;
             this.humidity=humidity;
             this.pressure=pressure;
             measurementsChanged();
         }
    }
         

         
       
       
        (2)CurrentConditionsDisplay
        CurrentConditionsDisplay.java
       
         package
          com.sterning.ch2_observer;


         public
          
         class
          CurrentConditionsDisplay
         implements
          Observer, Displayment
         
         {
         private float temperature;
         private float humidity;
         private Subject weatherData;
         
        public CurrentConditionsDisplay(Subject weatherData){
             this.weatherData=weatherData;
             weatherData.registerObserver(this);
         }
         
        public void update(float temp, float humidity, float pressure) {
             this.temperature=temp;
             this.humidity=humidity;
             display();
         }

        public void display() {
             System.out.println("Current conditions:"+temperature+"F degrees and "+humidity+"% humidity");
         }

    }
         

         
       
      
        (
        3
        )
        StatisticsDisplay
        StatisticsDisplay.java
       
         package
          com.sterning.ch2_observer;


         import
          java.util.
         *
         ;


         public
          
         class
          StatisticsDisplay
         implements
          Observer, Displayment
         
         {
         private float maxTemp = 0.0f;
         private float minTemp = 200;
         private float tempSum= 0.0f;
         private int numReadings;
         private WeatherData weatherData;

        public StatisticsDisplay(WeatherData weatherData) {
             this.weatherData = weatherData;
             weatherData.registerObserver(this);
         }

        public void update(float temp, float humidity, float pressure) {
             tempSum += temp;
             numReadings++;

            if (temp > maxTemp) {
                 maxTemp = temp;
             }
      
            if (temp < minTemp) {
                 minTemp = temp;
             }

             display();
         }

        public void display() {
             System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings)
                 + "/" + maxTemp + "/" + minTemp);
         }
    }
         

          
        (4)ForecastDisplay
        ForecastDisplay.java
       
         package
          com.sterning.ch2_observer;


         import
          java.util.
         *
         ;


         public
          
         class
          ForecastDisplay
         implements
          Observer, Displayment
         
         {
         private float currentPressure = 29.92f;  
         private float lastPressure;
         private WeatherData weatherData;

        public ForecastDisplay(WeatherData weatherData) {
             this.weatherData = weatherData;
             weatherData.registerObserver(this);
         }

        public void update(float temp, float humidity, float pressure) {
                     lastPressure = currentPressure;
             currentPressure = pressure;

             display();
         }

        public void display() {
             System.out.print("Forecast: ");
            if (currentPressure > lastPressure) {
                 System.out.println("Improving weather on the way!");
            } else if (currentPressure == lastPressure) {
                 System.out.println("More of the same");
            } else if (currentPressure < lastPressure) {
                 System.out.println("Watch out for cooler, rainy weather");
             }
         }
    }
         

         
       
       
        3.实现气象站
            
        WeatherStation.java
       
         package
          com.sterning.ch2_observer;


         public
          
         class
          WeatherStation
         
         {
        public static void main(String[] args){
             WeatherData weatherData=new WeatherData();
             
             CurrentConditionsDisplay currentDisplay=new CurrentConditionsDisplay(weatherData);
             StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
             ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
             
             weatherData.setMeasurements(80, 65, 30.4f);
             weatherData.setMeasurements(82, 70, 29.2f);
             weatherData.setMeasurements(78, 90, 29.2f);        
             
         }
    }
         

         
       


       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/11/6/000222625.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 11:37 , Processed in 0.398001 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表