面向对象系统分析与设计_观察者模式.ppt
06 一月 2023Neusoft Computer Science and Technology Department copy right1OO System Analysis&Design【The Observer Pattern】06 一月 2023Neusoft Computer Science and Technology Department copy right2Weather Monitoring overviewStats ForecastCurrent pulls data WeatherDataobject displays Weather StationOur client providesWhat we implementinternet06 一月 2023Neusoft Computer Science and Technology Department copy right3What is WeatherData object?lIt knows how to talk to the physical Weather Station,to get updated data.(we dont care for this process.)lIt then updates its displays for the three different display elements.gets data updates o它提供了三个方法(get开头),可以分别取得实时的温度、湿度和大气压力,还有一个MeasurementsChanged()方法,当任何天气状况发生变化的时候,这个方法都会自动被触发,当前这个方法只是一个空函数,扩展的代码还需要我们自己去扩充。06 一月 2023Neusoft Computer Science and Technology Department copy right41 客户提供了获取实时的天气状况的方法2 MeasurementsChanged()方法会在天气状况变化时被自动调用。3 系统要实现三种显示模式,分别显示天气状况、天气统计和天气预测,而且这些显示的信息必须跟当前最新的天气状况实时同步。4 系统还必须支持在显示方式上的扩展性,而且使用者可以任意添加和移除不同的显示模式。06 一月 2023Neusoft Computer Science and Technology Department copy right5o/伪代码opublic class WeatherDatao o/实例化显示设备(省略)opublic void MeasurementsChanged()oofloat temp=getTemperature();/取得温度ofloat humidity=getHumidity();/取得湿度ofloat pressure=getPressure();/取得气压ocurrentConditionsDisplay.update(temp,humidity,pressure);/同步显示当前天气状况ostatisticsDisplay.update(temp,humidity,pressure);/同步显示天气统计信息oforecastDisplay.update(temp,humidity,pressure);/同步显示天气预报信息oo06 一月 2023Neusoft Computer Science and Technology Department copy right606 一月 2023Neusoft Computer Science and Technology Department copy right7Our job is:lTo create an app that uses the WheatherData object to update three displays.06 一月 2023Neusoft Computer Science and Technology Department copy right8The kernel of the problemWeatherData object to update if you requiredisplays publishers subscribers to deliver if you subscribe subject object observer objects to notify if you observe 06 一月 2023Neusoft Computer Science and Technology Department copy right9The Observer PatternlThe Observer Pattern defines a one-to-many relationship between objects so that when one object changes state,all of its dependents are notified and updated automatically.06 一月 2023Neusoft Computer Science and Technology Department copy right10Class diagram for Observer PatternEach subject canhave many observersupdate()will be called when the Subjects state changes.These methods will be called When to register as observers,remove themselves from Being observers,or update allthe observers whenever state changes.06 一月 2023Neusoft Computer Science and Technology Department copy right11To develop app from the interfaceoSubject(被观察的对象接口)(被观察的对象接口)n 规定ConcreteSubject的统一接口;n每个Subject可以有多个Observer;oConcreteSubject(具体被观察对象)(具体被观察对象)n维护对所有具体观察者的引用的列表;n状态发生变化时会发送通知给所有注册的观察者。oObserver(观察者接口)(观察者接口)n规定ConcreteObserver的统一接口;n定义了一个update()方法,在被观察对象状态改变时会被调用。oConcreteObserver(具体观察者)(具体观察者)n维护一个对ConcreteSubject的引用;n特定状态与ConcreteSubject同步;n实现Observer接口,通过update()方法接收ConcreteSubject的通知。06 一月 2023Neusoft Computer Science and Technology Department copy right12顺序图06 一月 2023Neusoft Computer Science and Technology Department copy right1306 一月 2023Neusoft Computer Science and Technology Department copy right14Designing the Weather StationExercise:A Spring Tour planoSpring tour plan manage system need to maintain a collection of the participants oNotify all members of the information about the planoEach member have their own arrival at the point of collection06 一月 2023Neusoft Computer Science and Technology Department copy right1506 一月 2023Neusoft Computer Science and Technology Department copy right16Class is overReworking the Weather Station with the Javas built-in Observer Pattern!