-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver.py
More file actions
38 lines (27 loc) · 836 Bytes
/
observer.py
File metadata and controls
38 lines (27 loc) · 836 Bytes
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
class Observer:
'''Observer object
it receive notifications of the observable object (Bird)
'''
def __init__(self, name) -> None:
self.name = name
def update(self, message):
print(f'|{self.name} \treceived | {message}')
class Bird:
'''Observable object
it has the responsibility to notify the own observers
'''
def __init__(self):
self.observers = []
def add_observer(self, observer):
self.observers.append(observer)
def notify_observers(self, message):
for observer in self.observers:
observer.update(message)
mateus = Observer('Mateus')
william = Observer('William')
patrick = Observer('Patrick')
bird = Bird()
bird.add_observer(mateus)
bird.add_observer(william)
bird.add_observer(patrick)
bird.notify_observers("I'm flying")