# Observer Design Pattern Implementation in Ring
# Example: Weather Station with multiple observers
# Main program
see "=== Observer Design Pattern Demo ===" + nl
see "Weather Station with Multiple Display Observers" + nl + nl
# Create subject
weatherStation = new WeatherStation
# Create observers
currentDisplay = new CurrentConditionsDisplay
statsDisplay = new StatisticsDisplay
forecastDisplay = new ForecastDisplay
# Attach observers to subject
weatherStation.attach(currentDisplay)
weatherStation.attach(statsDisplay)
weatherStation.attach(forecastDisplay)
# Simulate weather changes
weatherStation.setMeasurements(25, 65, 1013)
weatherStation.setMeasurements(28, 70, 1015)
weatherStation.setMeasurements(22, 90, 1010)
# Detach one observer
see nl
weatherStation.detach(statsDisplay)
# More weather changes
weatherStation.setMeasurements(26, 75, 1014)
see nl + "=== Demo Complete ===" + nl
# Subject interface (Weather Station)
Class WeatherStation
observers = []
temperature = 0
humidity = 0
pressure = 0
Func attach(observer)
add(observers, observer)
see "Observer attached: " + observer.name + nl
Func detach(observer)
index = find(observers, observer)
if index > 0
del(observers, index)
see "Observer detached: " + observer.name + nl
ok
Func notify()
see "Notifying all observers..." + nl
for observer in observers
observer.update(temperature, humidity, pressure)
next
Func setMeasurements(temp, humid, press)
see nl + "Weather Station: New measurements received" + nl
temperature = temp
humidity = humid
pressure = press
notify()
Func getTemperature()
return temperature
Func getHumidity()
return humidity
Func getPressure()
return pressure
# Observer 1: Current Conditions Display
Class CurrentConditionsDisplay
name = "Current Conditions Display"
temperature = 0
humidity = 0
Func update(temp, humid, press)
temperature = temp
humidity = humid
display()
Func display()
see " [" + name + "]" + nl
see " Temperature: " + temperature + "°C" + nl
see " Humidity: " + humidity + "%" + nl
# Observer 2: Statistics Display
Class StatisticsDisplay
name = "Statistics Display"
tempReadings = []
Func update(temp, humid, press)
add(tempReadings, temp)
display()
Func display()
if len(tempReadings) = 0
return
ok
sum = 0
maxTemp = tempReadings[1]
minTemp = tempReadings[1]
for temp in tempReadings
sum += temp
if temp > maxTemp
maxTemp = temp
ok
if temp < minTemp
minTemp = temp
ok
next
avg = sum / len(tempReadings)
see " [" + name + "]" + nl
see " Avg Temperature: " + avg + "°C" + nl
see " Max Temperature: " + maxTemp + "°C" + nl
see " Min Temperature: " + minTemp + "°C" + nl
# Observer 3: Forecast Display
Class ForecastDisplay
name = "Forecast Display"
lastPressure = 0
currentPressure = 0
Func update(temp, humid, press)
lastPressure = currentPressure
currentPressure = press
display()
Func display()
see " [" + name + "]" + nl
if currentPressure > lastPressure
see " Forecast: Improving weather!" + nl
but currentPressure = lastPressure
see " Forecast: Same as before" + nl
else
see " Forecast: Watch out for cooler, rainy weather" + nl
ok