You are on page 1of 7

3.

Publishers-Subscriber / Observer Design Pattern


Intent: The Observer Pattern defines one-to-many dependency between objects so that when one object changes state; all its dependents are notified and updated automatically. Design a Weather nformation system. The weather !tation will be broadcasting the current weather conditions li"e temperature# humidity and biometric pressure. $reate an application which receives the weather conditions and displays them in different forms. %i.e. displaying the current weather conditions# displaying weather statistics&. 'll displays will be updating the weather conditions in real time# as and when the new conditions are recorded. The concrete!ubject ( WeatherData $oncreteObserver( $urrent$onditionDisplay# )orecastDisplay

Usecase Diagram:

Pattern Instance(

Class diagram(

Sequence Diagram(

Java C de(
!eatherData."ava:
import java.util.Observable; public class WeatherData e*tends Observable + private float temperature; public float getTemperature%& + return temperature; , private float pressure; public float getPressure%& + return pressure; , private float humidity; public float get-umidity%& + return humidity; , public void measurementschanged%& + set$hanged%&; notifyObservers%&; , public void set.easurements%float theTemperature# float the-umidity# float thePressure& + temperature/theTemperature; pressure/thePressure; humidity/the-umidity; measurementschanged%&; , ,

# reCastDis$la%."ava:
import java.util.Observable; import java.util.Observer; public class )ore$astDisplay implements Observer + private float currentPressure / 01.10f;

private float lastPressure; public )ore$astDisplay%Observable observable& + observable.addObserver%this&; , public void update%Observable observable# Object arg& + if %observable instanceof WeatherData& + WeatherData weatherData / %WeatherData&observable; lastPressure / currentPressure; currentPressure / weatherData.getPressure%&; display%&; , , public void display%& + !ystem.out.print%2)orecast( 2&; if %currentPressure 3 lastPressure& + !ystem.out.println%2 mproving weather on the way42&; , else if %currentPressure // lastPressure& + !ystem.out.println%2.ore of the same2&; , else if %currentPressure 5 lastPressure& + !ystem.out.println%2Watch out for cooler# rainy weather2&; , , ,

CurrentC nditi nDis$la%."ava


import java.util.Observable; import java.util.Observer; public class $urrent$onditionDisplay implements Observer + private float temperature; private float humidity; Observable observable; public $urrent$onditionDisplay%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.get-umidity%&; display%&; , , public void display%& + !ystem.out.println%2$urrent conditions( 2 6 temperature 6 2) degrees and 2 6 humidity 6 27 humidity2&; , ,

Client."ava
public class Weather!tation + public static void main%!tring89 args& + WeatherData weatherData / new WeatherData%&; $urrent$onditionDisplay current$onditions / new $urrent$onditionDisplay%weatherData&; )ore$astDisplay forecastDisplay / new )ore$astDisplay%weatherData&; weatherData.set.easurements%:;# <=# >;.?f&; weatherData.set.easurements%:0# @;# 01.0f&; weatherData.set.easurements%@:# 1;# 01.0f&; , ,

Out$ut:Forecast: Improving weather on the way! Current conditions: 80.0F degrees and 65.0% humidity Forecast: Watch out or coo!er" rainy weather Current conditions: 8#.0F degrees and $0.0% humidity Forecast: %ore o the same Current conditions: $8.0F degrees and &0.0% humidity

You might also like