-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobserver pattern
175 lines (143 loc) · 4.37 KB
/
observer pattern
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# class Subscriber:
# def __init__(self,name):
# self.name = name
# def update(self,message):
# print('{} got message: {}'.format(self.name,message))
#
# class Publisher:
# def __init__(self):
# self.subscribers=set()
# def register(self,who):
# self.subscribers.add(who)
# def unregister(self,who):
# self.subscribers.discard(who)
# def dispatch(self,message):
# for subscriber in self.subscribers:
# # update is a def in subscriber
# subscriber.update(message)
#
# pub=Publisher()
#
# bob = Subscriber('Bob')
# alice = Subscriber('Alice')
# john = Subscriber('John')
#
# pub.register(bob)
# pub.register(alice)
# pub.register(john)
#
# pub.dispatch('Its lunchtime')
# pub.unregister(john)
# pub.dispatch('Its dinner time')
# class SubscriberOne:
# def __init__(self,name):
# self.name = name
# def update(self,message):
# print('{} got message: {}'.format(self.name,message))
#
#
# class SubscriberTwo:
# def __init__(self,name):
# self.name = name
# def receive(self,message):
# print('{} receive message: {}'.format(self.name, message))
#
# class Publisher:
# def __init__(self):
# self.subscribers = dict()
# def register(self,who,callback=None):
# if callback == None:
# callback = getattr(who,'update')
# self.subscribers[who]=callback
# # one two receive/// update
#
# def unregister(self,who):
# del self.subscribers[who]
#
# def dispatch(self,message):
# for subscriber, callback in self.subscribers.items():
# callback(message)
#
# pub = Publisher()
# bob = SubscriberOne('Bob')
# alice = SubscriberTwo('Alice')
# john = SubscriberOne('John')
#
#
# pub.register(bob,bob.update)
# pub.register(alice,alice.receive)
# pub.register(john)
#
#
#
# pub.dispatch("It's lunchtime!")
# pub.unregister(john)
# pub.dispatch("Time for dinner")
a=['lunch', 'dinner']
b={event:dict() for event in a}
print(b)
class Subscriber:
def __init__(self,name):
self.name = name
def update(self,message):
print('{} got message {}'.format(self.name,message))
class Publisher:
def __init__(self,events):
self.events = {event: dict() for event in events}
def get_subscribers(self,event):
return self.events[event]
def register(self,event,who,callback=None):
if callback == None:
callback = getattr(who, 'update')
self.get_subscribers(event)[who]=callback
def unregister(self,event,who):
del self.get_subscribers(event)[who]
def dispatch(self,event,message):
for subscriber, callback in self.get_subscribers(event).items():
callback(message)
pub = Publisher(['lunch','dinner'])
bob = Subscriber('bob')
alice = Subscriber('Alice')
john = Subscriber('John')
### can distribute the message
pub.register('lunch',bob)
pub.register("lunch", john)
pub.register("dinner", john)
pub.dispatch("lunch", "It's lunchtime!")
pub.dispatch("dinner", "Dinner is served")
class Publisher:
def __init__(self,customer_class):
self.customer_class = {status: dict() for status in customer_class}
def get_subscribers(self,status):
return self.customer_class[status]
def register(self,who,callback=None):
if callback == None:
callback = getattr(who, 'update')
self.get_subscribers('promotion')[who] = callback
if who.status=='VIP':
self.get_subscribers('VIP promotion')[who]=callback
def unregister(self,event,who):
del self.get_subscribers(event)[who]
def dispatch(self,event,message):
for subscriber, callback in self.get_subscribers(event).items():
callback(message)
class Subscriber:
def __init__(self,name,status=None):
self.name = name
if status == None:
self.status = 'Normal'
else:
self.status = status
def update(self,message):
print('{} customer {} got message {}'.format(self.status,self.name,message))
pub = Publisher(['VIP promotion','promotion'])
bob = Subscriber('bob','VIP')
alice = Subscriber('Alice')
john = Subscriber('John')
print(pub.customer_class)
### can distribute the message
pub.register(bob)
pub.register(john)
pub.register(john)
pub.dispatch("VIP promotion", "It's VIP promotion!")
pub.dispatch("promotion", "it's promotion")