]>
git.datanom.net - pwp.git/blob - app/DB/observer.py
3 class WeatherSubject(metaclass
=abc
.ABCMeta
):
5 def add_observer(self
, weather_observer
):
9 def remove_observer(self
, weather_observer
):
17 class WeatherObserver(metaclass
=abc
.ABCMeta
):
19 def do_update(self
, temperature
):
23 class WeatherStation(WeatherSubject
):
24 def __init__(self
, temperature
):
26 self
.temperature
= temperature
28 def add_observer(self
, weather
):
29 self
.observers
.append(weather
)
31 def remove_observer(self
, weather
):
32 self
.observers
.remove(weather
)
35 for observer
in self
.observers
:
36 observer
.do_update(self
.temperature
)
38 def set_temperature(self
, temperature
):
39 print("Weather station setting temperature to %s" % temperature
)
40 self
.temperature
= temperature
44 class WeatherCustomer1(WeatherObserver
):
45 def do_update(self
, temperature
):
46 print("Weather customer 1 just found out the temperature is: %s" % temperature
)
49 class WeatherCustomer2(WeatherObserver
):
50 def do_update(self
, temperature
):
51 print("Weather customer 2 just found out the temperature is: %s" % temperature
)
54 if __name__
== '__main__':
55 station
= WeatherStation(33)
57 wc1
= WeatherCustomer1()
58 wc2
= WeatherCustomer2()
59 station
.add_observer(wc1
)
60 station
.add_observer(wc2
)
62 station
.set_temperature(34)
63 station
.remove_observer(wc1
)
64 station
.set_temperature(35)
This page took 0.195939 seconds and 6 git commands to generate.