java 观察者模式
java 观察者模式是什么?实例如下:
java 观察者模式是什么?
观察者模式:定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。
实例如下:
import java
.util
.*
;
interface Observer {
public void update( int value
);
}
interface Subject {
public void registerObserver( Observer o
);
public void removeObserver( Observer o
);
public void notifyObservers();
}
class SimpleObserver implements Observer {
private int value
;
private Subject subject
;
public SimpleObserver( Subject subject
) {
this.subject
= subject
;
subject
.registerObserver( this );
}
public void update( int value
) {
this.value
= value
;
display();
}
public void display() {
System
.out
.println( "Value: " + value
);
}
}
class SimpleSubject implements Subject {
private ArrayList
< Observer
> observers
;
private int value
= 0;
public SimpleSubject() {
observers
= new ArrayList< Observer
>();
}
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 notifyObservers() {
for ( Observer observer
: observers
) {
observer
.update( value
);
}
}
public void setValue( int value
) {
this.value
= value
;
notifyObservers();
}
}
class Example {
public static void main( String
[] args
) {
SimpleSubject simpleSubject
= new SimpleSubject();
SimpleObserver simpleObserver
= new SimpleObserver( simpleSubject
);
simpleSubject
.setValue( 80 );
}
}
public class Test0525{
public static void main( String
[] args
){
Example
.main( args
);
}
}
----------------------------------------------------------------------------------------
E
:\java
>java Test0525
Value
: 80
import java
.util
.*
;
interface Subject {
public void registerObserver( Observer o
);
public void removeObserver( Observer o
);
public void notifyObservers();
}
interface Observer {
public void update( float temp
, float humidity
, float pressure
);
}
interface DisplayElement {
public void display();
}
class WeatherData implements Subject {
private ArrayList
< Observer
> observers
;
private float temperature
;
private float humidity
;
private float pressure
;
public WeatherData() {
observers
= new ArrayList< Observer
>();
}
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 notifyObservers() {
for ( Observer observer
: observers
) {
observer
.update( temperature
, humidity
, pressure
);
}
}
public void measurementsChanged() {
notifyObservers();
}
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
;
}
}
class CurrentConditionsDisplay implements Observer, DisplayElement
{
private float temperature
;
private float humidity
;
private Subject weatherData
;
public CurrentConditionsDisplay( Subject weatherData
) {
this.weatherData
= weatherData
;
weatherData
.registerObserver( this );
}
public void update( float temperature
, float humidity
, float pressure
) {
this.temperature
= temperature
;
this.humidity
= humidity
;
display();
}
public void display() {
System
.out
.println( "Current conditions: " + temperature
+ "F degrees and " + humidity
+ "% humidity" );
}
}
class ForecastDisplay implements Observer, DisplayElement
{
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" );
}
}
}
class HeatIndexDisplay implements Observer, DisplayElement
{
float heatIndex
= 0.0f;
private WeatherData weatherData
;
public HeatIndexDisplay( WeatherData weatherData
) {
this.weatherData
= weatherData
;
weatherData
.registerObserver( this );
}
public void update( float t
, float rh
, float pressure
) {
heatIndex
= computeHeatIndex( t
, rh
);
display();
}
private float computeHeatIndex( float t
, float rh
) {
float index
= ( float )(( 16.923 + ( 0.185212 * t
) + ( 5.37941 * rh
) - ( 0.100254 * t
* rh
)
+ ( 0.00941695 * ( t
* t
) ) + ( 0.00728898 * ( rh
* rh
) )
+ ( 0.000345372 * ( t
* t
* rh
) ) - ( 0.000814971 * ( t
* rh
* rh
) ) +
( 0.0000102102 * ( t
* t
* rh
* rh
) ) - ( 0.000038646 * ( t
* t
* t
) ) + ( 0.0000291583 *
( rh
* rh
* rh
) ) + ( 0.00000142721 * ( t
* t
* t
* rh
) ) +
( 0.000000197483 * ( t
* rh
* rh
* rh
) ) - ( 0.0000000218429 * ( t
* t
* t
* rh
* rh
) ) +
0.000000000843296 * ( t
* t
* rh
* rh
* rh
) ) -
( 0.0000000000481975 * ( t
* t
* t
* rh
* rh
* rh
) ) );
return index
;
}
public void display() {
System
.out
.println( "Heat index is " + heatIndex
);
}
}
class StatisticsDisplay implements Observer, DisplayElement
{
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
);
}
}
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 );
}
}
class WeatherStationHeatIndex {
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
);
HeatIndexDisplay heatIndexDisplay
= new HeatIndexDisplay( weatherData
);
weatherData
.setMeasurements( 80, 65, 30.4f );
weatherData
.setMeasurements( 82, 70, 29.2f );
weatherData
.setMeasurements( 78, 90, 29.2f );
}
}
public class Test0525{
public static void main( String
[] args
){
WeatherStation
.main( args
);
System
.out
.println();
WeatherStationHeatIndex
.main( args
);
}
}
-----------------------------------------------------------------
E
:\java
>java Test0525
Current conditions
: 80.0F degrees and
65.0% humidity
Avg
/Max
/Min temperature
= 80.0/80.0/80.0
Forecast
: Improving weather on the way
!
Current conditions
: 82.0F degrees and
70.0% humidity
Avg
/Max
/Min temperature
= 81.0/82.0/80.0
Forecast
: Watch out
for cooler
, rainy weather
Current conditions
: 78.0F degrees and
90.0% humidity
Avg
/Max
/Min temperature
= 80.0/82.0/78.0
Forecast
: More of the same
Current conditions
: 80.0F degrees and
65.0% humidity
Avg
/Max
/Min temperature
= 80.0/80.0/80.0
Forecast
: Improving weather on the way
!
Heat index is
82.95535
Current conditions
: 82.0F degrees and
70.0% humidity
Avg
/Max
/Min temperature
= 81.0/82.0/80.0
Forecast
: Watch out
for cooler
, rainy weather
Heat index is
86.90124
Current conditions
: 78.0F degrees and
90.0% humidity
Avg
/Max
/Min temperature
= 80.0/82.0/78.0
Forecast
: More of the same
Heat index is
83.64967