本文主要是介绍《Head First设计模式》学习-Observer模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《Head First设计模式》学习-Observer模式
《Head First设计模式》学习-Observer模式 | | | 下载源代码 | 〖 作者:诗特林 〗〖 大小:3K 〗〖 发布日期:2008-01-21 〗〖 浏览:1 〗 |
<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-1832179689702023&dt=1200993351167&lmt=1200898694&format=728x90_as&output=html&correlator=1200993351167&url=http%3A%2F%2Fwww.java3z.com%2Fcwbwebhome%2Farticle%2Farticle2%2F21045.html%3Fid%3D1789&ad_type=text_image&ref=http%3A%2F%2Fwww.baidu.com%2Fs%3Flm%3D0%26si%3D%26rn%3D10%26ie%3Dgb2312%26ct%3D0%26wd%3Dobserver%2B%2B%2B%25C4%25A3%25CA%25BD%26pn%3D10%26ver%3D0%26cl%3D3&cc=100&ga_vid=227957719.1200993351&ga_sid=1200993351&ga_hid=515322661&flash=9&u_h=1024&u_w=1280&u_ah=994&u_aw=1280&u_cd=32&u_tz=-480&u_java=true" frameborder="0" width="728" scrolling="no" height="90" allowtransparency="allowtransparency"> | 一、要完成的任务 此系统中的三个部分是气象站(获取实际气象数据的物理装置)、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); } } | |
这篇关于《Head First设计模式》学习-Observer模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!