-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserver.py
More file actions
43 lines (26 loc) · 886 Bytes
/
Observer.py
File metadata and controls
43 lines (26 loc) · 886 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
39
40
41
42
43
from abc import *
class ObservableEngine(Engine):
def __init__(self):
self.subscribers = set()
def subscribe(self, subscriber):
self.subscribers.add(subscriber)
def unsubscribe(self, subscriber):
self.subscribers.remove(subscriber)
def notify(self, message):
for sub in self.subscribers:
sub.update(message)
class AbstractObserver(ABC):
@abstractmethod
def update(self):
pass
class ShortNotificationPrinter(AbstractObserver):
def __init__(self):
self.achievements = set()
def update(self, message):
self.achievements.add(message["title"])
class FullNotificationPrinter(AbstractObserver):
def __init__(self):
self.achievements = list()
def update(self, message):
if message not in self.achievements:
self.achievements.append(message)