-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
33 lines (23 loc) · 823 Bytes
/
code.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
class Subject:
def __init__(self):
self.__observers = []
def register(self, observer):
self.__observers.append(observer)
def notifyAll(self, *args, **kwargs):
for observer in self.__observers:
observer.listen(self, *args, **kwargs)
class Observer1:
def __init__(self, subject):
subject.register(self)
def listen(self, subject, *args):
print(type(self).__name__, ':: Got', args, 'From', subject)
class Observer2:
def __init__(self, subject):
subject.register(self)
def listen(self, subject, *args):
print(type(self).__name__, ':: Got', args, 'From', subject)
if __name__ == "__main__":
subject = Subject()
observer1 = Observer1(subject)
observer2 = Observer2(subject)
subject.notifyAll('notification')