标签:[] 这一 temp notify font etc 观察者 pre sch
一:原理就是观察者设计模式的原理直接上图和代码
1 public class WeatherData extends Observable { 2 private float temperature; 3 private float humidity; 4 private float pressure; 5 6 public WeatherData() { 7 } 8 9 public void measurementsChanged() { 10 setChanged(); 11 notifyObservers(); 12 } 13 14 public void setMeasurements(float temperature, float humidity, float pressure) { 15 this.temperature = temperature; 16 this.humidity = humidity; 17 this.pressure = pressure; 18 measurementsChanged(); 19 } 20 21 public float getTemperature() { 22 return temperature; 23 } 24 25 public float getHumidity() { 26 return humidity; 27 } 28 29 public float getPressure() { 30 return pressure; 31 } 32 }
public interface DisplayElement {
public void display();
}
package com.DesignPatterns.ab.observer2; import java.util.Observable; import java.util.Observer; public class CurrentConditionsDisplay implements Observer, DisplayElement { Observable observable; private float temperature; private float humidity; public CurrentConditionsDisplay(Observable observable) { this.observable = observable; observable.addObserver(this); } public void update(Observable obs, Object arg) { if (obs instanceof WeatherData) { WeatherData weatherData = (WeatherData) obs; this.temperature = weatherData.getTemperature(); this.humidity = weatherData.getHumidity(); display(); } } public void display() { System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); } } |
法(update),
|
其它的类都由jar包给实现类的。
标签:[] 这一 temp notify font etc 观察者 pre sch
原文地址:https://www.cnblogs.com/qingruihappy/p/9693367.html