今标 网站建设,国外服装设计网站,北京家居网站建设,wordpress防站观察者模式 
单个对此#xff08;发布者#xff0c;也称为主体或是可观察对象#xff09;和一个或是多个对象#xff08;订阅者#xff0c;也称为观察者#xff09;之间的发布-订阅关系。增加发布者和订阅这个之间解耦#xff0c;使得在运行时添加、删除订阅者变得容易。…观察者模式 
单个对此发布者也称为主体或是可观察对象和一个或是多个对象订阅者也称为观察者之间的发布-订阅关系。增加发布者和订阅这个之间解耦使得在运行时添加、删除订阅者变得容易。对于MVC, 发布者是模型订阅者是视图。 
拍卖场景 
每个竞买人都有一个号码牌当天安门想要出价时就举起号码牌。当竞买人举起牌子时拍卖人即为主体更新竞买价格并将新价格广播给所有竞买人竞买者。 
软件场景  
RabbitMQ  库可以用于应用程序添加异步消息支持支持多种消息协议http和amqp 。RbbitMQ 可以在Python 应用与发布订阅模式该模式就是观察者设计模式。 
新闻流  
使用RSS 、Atom 或其他相关格式。可以关注一个新闻流每次更新时候你都会收到一个关于更新的通知。 
事件驱动系统 
监听器监听特定的事件监听器在监听的事件被创建时触发。这个事件可以是按下一个特定的键移动鼠标事件扮演发布者的角色监听器扮演观察者的角色。可以为单个事件发布者添加多个监听器观察者。 
案例 
格式化程序有一个默认的格式化程序以十进制格式显示一个值可以添加、注册更多的格式化程序。每次更新默认的格式化程序时候都会通知已经注册的格式化程序采取行动。程序以相关格式显示新的值。 
(1) 定义 Publisher 类。 
(2) 定义 DefaultFormatter 类以及特殊的 __init__ 和 __str__ 方法。 
(3) 向 DefaultFormatter 类添加 data 属性的设置方法和获取方法。 
(4) 定义两个观察者类 
(5) 添加程序的主体部分。 main() 函数的第一部分如下。 class Publisher:def __init__(self):self.observers  []  # 保存观察者def add(self, observer):if observer not in self.observers:self.observers.append(observer)else:print(fFailed to add:{observer})def remove(self, observer):try:self.observers.remove(observer)except ValueError:print(fFailed to remove:{observer})def notify(self):print(所有观察者通知更新)[o.notify(self) for o in self.observers]class DefaultFormatter(Publisher):def __init__(self, name):Publisher.__init__(self)self.name  nameself._data  0def __str__(self):return f{type(self).__name__}:{self.name} has data {self._data}propertydef data(self):return self._datadata.setterdef data(self, new_value):try:self._data  int(new_value)except ValueError as e:print(fError:{e})else:self.notify()  # 执行通知定义两个观察者
class HexFormatterObs:def notify(self, publisher):value  hex(publisher.data)print(f{type(self).__name__}: {publisher.data} has now hex data {value})class BinaryFormatterObs:def notify(self, publisher):value  bin(publisher.data)print(f{type(self).__name__}:{publisher.data} has  now bin data {value})def main():dfDefaultFormatter(test1)print(df)print(---------------------)hfHexFormatterObs()df.add(hf)df.data3print(df)bfBinaryFormatterObs()df.add(bf)df.data21print(df)print(----------hello-----------)df.datahelloprint(df)print(----------15.8-----------)df.data15.8print(df)
if __name__  __main__:main()# try:#     print(33333)# except:#     print(wwww)# else:#     print(else) 
执行结果  
DefaultFormatter:test1 has data 0
---------------------
所有观察者通知更新
HexFormatterObs: 3 has now hex data 0x3
DefaultFormatter:test1 has data 3
所有观察者通知更新
HexFormatterObs: 21 has now hex data 0x15
BinaryFormatterObs:21 has  now bin data 0b10101
DefaultFormatter:test1 has data 21
----------hello-----------
Error:invalid literal for int() with base 10: hello
DefaultFormatter:test1 has data 21
----------15.8-----------
所有观察者通知更新
HexFormatterObs: 15 has now hex data 0xf
BinaryFormatterObs:15 has  now bin data 0b1111
DefaultFormatter:test1 has data 15 
 文章转载自: http://www.morning.hrdx.cn.gov.cn.hrdx.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.nqmkr.cn.gov.cn.nqmkr.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.ngjpt.cn.gov.cn.ngjpt.cn http://www.morning.joinyun.com.gov.cn.joinyun.com http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.qstjr.cn.gov.cn.qstjr.cn http://www.morning.cwtrl.cn.gov.cn.cwtrl.cn http://www.morning.ryxyz.cn.gov.cn.ryxyz.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.wzyfk.cn.gov.cn.wzyfk.cn http://www.morning.nrrzw.cn.gov.cn.nrrzw.cn http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.glwyn.cn.gov.cn.glwyn.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.ltffk.cn.gov.cn.ltffk.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.jpnw.cn.gov.cn.jpnw.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.ssjry.cn.gov.cn.ssjry.cn http://www.morning.pswqx.cn.gov.cn.pswqx.cn http://www.morning.trqzk.cn.gov.cn.trqzk.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.wmfr.cn.gov.cn.wmfr.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn http://www.morning.qxxj.cn.gov.cn.qxxj.cn http://www.morning.wqkzf.cn.gov.cn.wqkzf.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.jglqn.cn.gov.cn.jglqn.cn http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.cwlxs.cn.gov.cn.cwlxs.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.dtrz.cn.gov.cn.dtrz.cn http://www.morning.pqwrg.cn.gov.cn.pqwrg.cn http://www.morning.nmlpp.cn.gov.cn.nmlpp.cn http://www.morning.hcqpc.cn.gov.cn.hcqpc.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.stbfy.cn.gov.cn.stbfy.cn http://www.morning.dfojgo.cn.gov.cn.dfojgo.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.xjtnp.cn.gov.cn.xjtnp.cn http://www.morning.tturfsoc.com.gov.cn.tturfsoc.com http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.kmkpm.cn.gov.cn.kmkpm.cn http://www.morning.nwbnt.cn.gov.cn.nwbnt.cn http://www.morning.bbmx.cn.gov.cn.bbmx.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.znqfc.cn.gov.cn.znqfc.cn http://www.morning.smygl.cn.gov.cn.smygl.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.bdqpl.cn.gov.cn.bdqpl.cn http://www.morning.tktcr.cn.gov.cn.tktcr.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.httzf.cn.gov.cn.httzf.cn http://www.morning.tnfyj.cn.gov.cn.tnfyj.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.msgcj.cn.gov.cn.msgcj.cn http://www.morning.dxgt.cn.gov.cn.dxgt.cn