Observer Pattern Java built-in usage
Java built-inThe observer mode of is throughInherit parent class, ImplementationObserver ModeThe following are the main functions:
Observerable (observed ):IsParent class),AddObserver (), Add the observer;DeleteObserver (), Delete the observer;
Yyobservers ()To notify the observer;SetChanged (), Confirm the change;
Observer (Observer ):IsInterface),Update ()To update the observer data;
SetChanged ()-> policyobservers (), Must be used firstSetChanged (), And then useYyobservers (), That is, confirm the submission before notifying the observer;
Updated observer InterfaceUpdate (Observerable o, Object arg)That is, you can use push or pull );
If yyobservers (arg) is used to pass the parameter, It is the push method. If there is no parameter, It is the pull (pull) method, that is, it is obtained using the get () method;
Observer'sNotification (sort y) OrderYesInbound and Outbound.
Observerable (observed) code:
/*** @ Time May 22, 2014 */package observer. java; import java. util. observable;/*** @ author C. l. wang **/public class WeatherData extends Observable {private float temperature; private float humidity; private float pressure; public WeatherData () {}public void measurementsChanged () {setChanged (); policyobservers ();} public void setMeasurements (float temperature, float humidity, float pressure) {this. temperature = temperature; this. humidity = humidity; this. pressure = pressure; measurementsChanged ();} public float getTemperature () {return temperature;} public float getHumidity () {return humidity;} public float getPressure () {return pressure ;}}
Observer (Observer) code:
/*** @ Time May 22, 2014 */package observer. java; import java. util. observable; import java. util. observer;/*** @ author C. l. wang **/public class CurrentConditionsDisplay implements Observer, DisplayElement {Observable observable; private float temperature; private float humidity; public CurrentConditionsDisplay (Observable observable) {this. observable = observable; observable. addObserver (this);}/* (non-Javadoc) * @ see observer. java. displayElement # display () */@ Overridepublic void display () {// TODO Auto-generated method stubSystem. out. println (Current conditions: + temperature + F degrees and + humidity + % humidity);}/* (non-Javadoc) * @ see java. util. observer # update (java. util. observable, java. lang. object) * // @ Overridepublic void update (Observable o, Object arg) {// TODO Auto-generated method stubif (o instanceof WeatherData) {WeatherData weatherData = (WeatherData) o; this. temperature = weatherData. getTemperature (); this. humidity = weatherData. getHumidity (); display ();}}}
Remaining codeNot listed one by one, see: http://blog.csdn.net/caroline_wendy/article/details/26583157
Test code:
package observer.java;public class WeatherStation {public static void main(String[] args) {WeatherData weatherData = new WeatherData();CurrentConditionsDisplay currentConditions = 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);}}
Output:
Forecast: Improving weather on the way!Avg/Max/Min temperature = 80.0/80.0/80.0Current conditions: 80.0F degrees and 65.0% humidityForecast: Watch out for cooler, rainy weatherAvg/Max/Min temperature = 81.0/82.0/80.0Current conditions: 82.0F degrees and 70.0% humidityForecast: More of the sameAvg/Max/Min temperature = 80.0/82.0/78.0Current conditions: 78.0F degrees and 90.0% humidity
Note: The order of notifications is first Inbound and then outbound.