-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpublish_subscribe_pattern.py
74 lines (52 loc) · 1.67 KB
/
publish_subscribe_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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Andy'
"""
大话设计模式
设计模式——观察者模式
观察者模式又叫做发布-订阅模式 (Publish Subscribe Pattern):定义了一种一对多的关系,让多个观察对象同时监听一个主题对象,当主题对象状态发生变化时会通知所有观察者,是它们能够自动更新自己
使用场景:当一个对象的改变需要同时改变其他对象的时候,而且它不知道具体有多少对象待改变
"""
#抽象通知者类
class Subject(object):
def attach(self, observer):
pass
def detach(self,observer):
pass
def notify(self):
pass
#具体通知者类
class Boss(Subject):
def __init__(self):
self.observer_list = []
self.subject_status = ''
def attach(self, observer):
self.observer_list.append(observer)
def detach(self,observer):
self.observer_list.remove(observer)
def notify(self):
for item in self.observer_list:
item.update()
#抽象观察者类
class Observer(object):
def __init__(self, name, publish):
self.name = name
self.publish = publish
def update(self):
pass
#具体观察者类-看股票的人
class StockObserver(Observer):
def update(self):
print self.publish.subject_status,self.name,'关闭股票行情,继续工作'
#具体观察者类-看NBA的人
class NbaObserver(Observer):
def update(self):
print self.publish.subject_status,self.name,'关闭NBA,继续工作'
if __name__ == "__main__":
publisher = Boss()
stocker = StockObserver('Andy',publisher)
nbaer = NbaObserver('Tracy',publisher)
publisher.attach(stocker)
publisher.attach(nbaer)
publisher.subject_status = '本老板回来了'
publisher.notify()