-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver.py
53 lines (37 loc) · 1021 Bytes
/
observer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typing import List
class Observer:
def __init__(self):
self._observers: List[Signal] = []
def attach(self, observer):
self._observers.append(observer)
def notify(self):
for observer in self._observers:
observer.update(self)
class Data(Observer):
def __init__(self, name):
super().__init__()
self.name = name
self._data = 0
@property
def data(self):
return self._data
@data.setter
def data(self, value):
self._data = value
self.notify()
class Signal:
def update(self, subject: Observer):
pass
class One(Signal):
def update(self, subject: Data):
print(f'new "{subject.name}" data: {subject.data}')
class Two(Signal):
def update(self, subject: Data):
print(f'new "{subject.name}" data: {subject.data}')
if __name__ == '__main__':
d1 = Data('first')
d1.attach(One())
d2 = Data('second')
d2.attach(Two())
d1.data = 5
d2.data = 3