Skip to content

Commit 0ef758f

Browse files
authored
Merge pull request #9 from dancergraham/feature/observer
Feature/observer
2 parents d8f240c + a1dd6a0 commit 0ef758f

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

chapter02_observer/readme.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Chapter 2: Observer design pattern
22

3-
> **Observer**: defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
3+
> **Observer**: defines a one-to-many dependency between objects so that when one object changes state, all of its
4+
> dependents are notified and updated automatically.
45
5-
Very useful design pattern, often used in user interfaces and as part of the popular Model-View-Controller design pattern used, for instance, in Django.
6-
7-
I find the name contradictory: shouldn't it be called the 'Subject' design pattern as the subject is the key actor ?
6+
A very useful design pattern, often used in user interfaces
7+
and as part of the popular Model-View-Controller (MVC) pattern used,
8+
for instance, in Django.
9+
As I wrote out the code I found it very appealing that I did not need
10+
to change the subject at all to add new observers.

chapter02_observer/weather.py

+27
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,39 @@ def display(self):
112112
print("Watch out for cooler, rainy weather")
113113

114114

115+
class HeatIndexDisplay(DisplayElement, Observer):
116+
def __init__(self, weather_data):
117+
self.heat_index = 0.
118+
self._weather_data = weather_data
119+
weather_data.register_observer(self)
120+
121+
def update(self, temp, humidity, pressure):
122+
self.heat_index = self._compute_heat_index(temp, humidity)
123+
self.display()
124+
125+
def display(self):
126+
print(f"Heat index is {self.heat_index:.5f}")
127+
128+
@classmethod
129+
def _compute_heat_index(cls, t, rh):
130+
index = ((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh)
131+
+ (0.00941695 * (t * t)) + (0.00728898 * (rh * rh))
132+
+ (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) +
133+
(0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) +
134+
(0.0000291583 * (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) +
135+
(0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) +
136+
0.000000000843296 * (t * t * rh * rh * rh)) -
137+
(0.0000000000481975 * (t * t * t * rh * rh * rh)))
138+
return index
139+
140+
115141
def weather_station():
116142
weather_data = WeatherData()
117143

118144
current_display = CurrentConditionsDisplay(weather_data)
119145
statistics_display = StatisticsDisplay(weather_data)
120146
forecast_display = ForecastDisplay(weather_data)
147+
heat_index_display = HeatIndexDisplay(weather_data)
121148

122149
weather_data.set_measurements(80, 65, 30.4)
123150
weather_data.set_measurements(82, 70, 29.2)

0 commit comments

Comments
 (0)