]> git.datanom.net - pwp.git/blame - app/DB/observer.py
Half way through migration away from sqlalchemy
[pwp.git] / app / DB / observer.py
CommitLineData
e5424f29
MR
1import abc
2
3class WeatherSubject(metaclass=abc.ABCMeta):
4 @abc.abstractmethod
5 def add_observer(self, weather_observer):
6 pass
7
8 @abc.abstractmethod
9 def remove_observer(self, weather_observer):
10 pass
11
12 @abc.abstractmethod
13 def do_notify(self):
14 pass
15
16
17class WeatherObserver(metaclass=abc.ABCMeta):
18 @abc.abstractmethod
19 def do_update(self, temperature):
20 pass
21
22
23class WeatherStation(WeatherSubject):
24 def __init__(self, temperature):
25 self.observers = []
26 self.temperature = temperature
27
28 def add_observer(self, weather):
29 self.observers.append(weather)
30
31 def remove_observer(self, weather):
32 self.observers.remove(weather)
33
34 def do_notify(self):
35 for observer in self.observers:
36 observer.do_update(self.temperature)
37
38 def set_temperature(self, temperature):
39 print("Weather station setting temperature to %s" % temperature)
40 self.temperature = temperature
41 self.do_notify()
42
43
44class WeatherCustomer1(WeatherObserver):
45 def do_update(self, temperature):
46 print("Weather customer 1 just found out the temperature is: %s" % temperature)
47
48
49class WeatherCustomer2(WeatherObserver):
50 def do_update(self, temperature):
51 print("Weather customer 2 just found out the temperature is: %s" % temperature)
52
53
54if __name__ == '__main__':
55 station = WeatherStation(33)
56
57 wc1 = WeatherCustomer1()
58 wc2 = WeatherCustomer2()
59 station.add_observer(wc1)
60 station.add_observer(wc2)
61
62 station.set_temperature(34)
63 station.remove_observer(wc1)
64 station.set_temperature(35)
This page took 0.038038 seconds and 6 git commands to generate.