• O0_633697
    了解作者
  • 4.8KB
    文件大小
  • 文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-18 05:54
    上传日期
设计模式之观察者模式ObserverPattern
ObserverPattern.tar.gz
  • ObserverPattern
  • com
  • subject
  • Subject.java
    194B
  • WeatherData.java
    1.2KB
  • WeatherData.class
    1.7KB
  • Subject.class
    220B
  • observer
  • Display.class
    132B
  • ForecastDisplay.java
    819B
  • ThirdPartDisplay.class
    796B
  • Observer.java
    118B
  • StatisticsDisplay.java
    1.1KB
  • CurrentConditionsDisplay.java
    809B
  • Observer.class
    136B
  • StatisticsDisplay.class
    1.6KB
  • ThirdPartDisplay.java
    778B
  • CurrentConditionsDisplay.class
    1.4KB
  • Display.java
    69B
  • ForecastDisplay.class
    959B
  • test
  • WeatherStation.class
    968B
  • WeatherStation.java
    664B
  • .project
    374B
  • .classpath
    220B
内容介绍
package com.subject; import java.util.ArrayList; import com.observer.Observer; public class WeatherData implements Subject { private ArrayList<Observer> observer; private float temperature; private float humidity; private float pressure; public WeatherData() { observer = new ArrayList<Observer>(); } float getTemperature() { return temperature; } float getHumidity() { return humidity; } float getPressure() { return pressure; } public void setMeasurements(float temp, float hum, float press) { this.temperature = temp; this.humidity = hum; this.pressure = press; measurementChanged(); } void measurementChanged() { notifyObersers(); } @Override public void registerObserver(Observer observer) { // TODO Auto-generated method stub this.observer.add(observer); } @Override public void removerObserver(Observer observer) { // TODO Auto-generated method stub int index = this.observer.indexOf(observer); if (index >= 0) this.observer.remove(observer); } @Override public void notifyObersers() { // TODO Auto-generated method stub for (int i = 0; i < observer.size(); i++) { observer.get(i).update(temperature, humidity, pressure); } } }
评论
    相关推荐