-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver_pattern.py
83 lines (60 loc) · 2.39 KB
/
observer_pattern.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
The intent of this yet another behavior design pattern is to automatically notify all dependents when one of em updated
or changes state.
..date.. March 22 2020.
..real-time eg.. A class has been acting as publisher and there are many subscriber classes available and waiting for
information. we dont want publisher to get to know about all those subscribers, & not want to create
a dependency with all of them. so we create an interface where publisher will communicate only through
that interface. all subscribers can connect to that interface.This interface should declare the
notification method along with a set of parameters that the publisher can use to pass some
contextual data along with the notification.
https://refactoring.guru/design-patterns/observer
"""
from abc import ABC, abstractmethod
class Observer(ABC):
@abstractmethod
def update(self, observable, *args):
pass
class Observable:
def __init__(self):
self.__observers = list()
def add_observer(self, x):
self.__observers.append(x)
def delete_observer(self, x):
self.__observers.remove(x)
def notify_observers(self, *args):
for _ in self.__observers:
_.update(self, *args)
class Twitter(Observable, Observer):
def __init__(self, x):
super().__init__()
self._name = x
@property
def name(self):
return self._name
def follow(self, new_follower):
new_follower.add_observer(self)
return self
def tweet(self, content):
self.notify_observers(content)
def update(self, observable, tweet):
print(f"{self.name} received a tweet from {observable.name}: {tweet}")
a = Twitter('Alice')
k = Twitter('King')
q = Twitter('Queen')
h = Twitter('Mad Hatter')
c = Twitter('Cheshire Cat')
a.follow(c).follow(h).follow(q)
k.follow(q)
q.follow(q).follow(h)
h.follow(a).follow(q).follow(c)
print(f'==== {q.name} tweets ====')
q.tweet('Off with their heads!')
print(f'\n==== {a.name} tweets ====')
a.tweet('What a strange world we live in.')
print(f'\n==== {k.name} tweets ====')
k.tweet('Begin at the beginning, and go on till you come to the end: then stop.')
print(f'\n==== {c.name} tweets ====')
c.tweet("We're all mad here.")
print(f'\n==== {h.name} tweets ====')
h.tweet('Why is a raven like a writing-desk?')