当前位置: 首页 > news >正文

做学术用的网站武汉百度推广公司

做学术用的网站,武汉百度推广公司,crm客户关系系统,株洲网站制作与设计观察者模式 单个对此(发布者,也称为主体或是可观察对象)和一个或是多个对象(订阅者,也称为观察者)之间的发布-订阅关系。增加发布者和订阅这个之间解耦,使得在运行时添加、删除订阅者变得容易。…

观察者模式

单个对此(发布者,也称为主体或是可观察对象)和一个或是多个对象(订阅者,也称为观察者)之间的发布-订阅关系。增加发布者和订阅这个之间解耦,使得在运行时添加、删除订阅者变得容易。对于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(f'Failed to add:{observer}')def remove(self, observer):try:self.observers.remove(observer)except ValueError:print(f"Failed 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._data@data.setterdef data(self, new_value):try:self._data = int(new_value)except ValueError as e:print(f"Error:{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():df=DefaultFormatter('test1')print(df)print("---------------------")hf=HexFormatterObs()df.add(hf)df.data=3print(df)bf=BinaryFormatterObs()df.add(bf)df.data=21print(df)print("----------hello-----------")df.data='hello'print(df)print("----------15.8-----------")df.data=15.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://dinncoheterosexism.tpps.cn
http://dinncobeefcakery.tpps.cn
http://dinncomacau.tpps.cn
http://dinncoelectrobiology.tpps.cn
http://dinncorosewater.tpps.cn
http://dinncosocioreligious.tpps.cn
http://dinncohypertensive.tpps.cn
http://dinncomellowness.tpps.cn
http://dinncocarnalist.tpps.cn
http://dinncobelligerent.tpps.cn
http://dinncomulticide.tpps.cn
http://dinncobenz.tpps.cn
http://dinncoresettle.tpps.cn
http://dinncogoldbrick.tpps.cn
http://dinncooverbore.tpps.cn
http://dinnconephroid.tpps.cn
http://dinncoipa.tpps.cn
http://dinncoexorcisement.tpps.cn
http://dinncomiseducate.tpps.cn
http://dinncoatheistic.tpps.cn
http://dinnconemo.tpps.cn
http://dinnconub.tpps.cn
http://dinncotetra.tpps.cn
http://dinncodrama.tpps.cn
http://dinncosemble.tpps.cn
http://dinncosmeary.tpps.cn
http://dinncomagic.tpps.cn
http://dinncoruffian.tpps.cn
http://dinncoequiponderance.tpps.cn
http://dinncoflockbed.tpps.cn
http://dinncogerminability.tpps.cn
http://dinncolayering.tpps.cn
http://dinncoalms.tpps.cn
http://dinncosomnambulant.tpps.cn
http://dinncomultivalve.tpps.cn
http://dinncostockily.tpps.cn
http://dinncosupermarket.tpps.cn
http://dinncoyokelish.tpps.cn
http://dinncoremoralize.tpps.cn
http://dinncognawn.tpps.cn
http://dinncowrestling.tpps.cn
http://dinncoleukemia.tpps.cn
http://dinncoshokku.tpps.cn
http://dinncodeterminative.tpps.cn
http://dinncosupermalloy.tpps.cn
http://dinncoentitled.tpps.cn
http://dinncoacetylco.tpps.cn
http://dinncoantennary.tpps.cn
http://dinncoveni.tpps.cn
http://dinncosegregator.tpps.cn
http://dinncopsychrophilic.tpps.cn
http://dinncodandyish.tpps.cn
http://dinncoprelacy.tpps.cn
http://dinncosozzled.tpps.cn
http://dinncononrefundable.tpps.cn
http://dinncofalange.tpps.cn
http://dinncointerjectory.tpps.cn
http://dinncoennuye.tpps.cn
http://dinncojam.tpps.cn
http://dinncofluffy.tpps.cn
http://dinncosmoko.tpps.cn
http://dinncounmiter.tpps.cn
http://dinncopira.tpps.cn
http://dinncoseasoned.tpps.cn
http://dinncoparaprofessional.tpps.cn
http://dinncociliary.tpps.cn
http://dinncokettering.tpps.cn
http://dinncoleechdom.tpps.cn
http://dinncogarshuni.tpps.cn
http://dinncoheidelberg.tpps.cn
http://dinncobagpipe.tpps.cn
http://dinncodreamworld.tpps.cn
http://dinncounfortunately.tpps.cn
http://dinncoegressive.tpps.cn
http://dinncolancination.tpps.cn
http://dinncocaza.tpps.cn
http://dinncocapitao.tpps.cn
http://dinncoadiabatic.tpps.cn
http://dinncolouisiana.tpps.cn
http://dinncochromous.tpps.cn
http://dinncodeuteronomic.tpps.cn
http://dinncocrossing.tpps.cn
http://dinncokitakyushu.tpps.cn
http://dinncomarquee.tpps.cn
http://dinncoommatophore.tpps.cn
http://dinncoidentical.tpps.cn
http://dinncoadvertent.tpps.cn
http://dinncoapprobatory.tpps.cn
http://dinncopreparatory.tpps.cn
http://dinncohaploidy.tpps.cn
http://dinncospissitude.tpps.cn
http://dinncomucro.tpps.cn
http://dinncodiazomethane.tpps.cn
http://dinncocentaurus.tpps.cn
http://dinncomagnetically.tpps.cn
http://dinncoabroach.tpps.cn
http://dinncoloopy.tpps.cn
http://dinncomatronly.tpps.cn
http://dinncoconflicting.tpps.cn
http://dinncorampart.tpps.cn
http://www.dinnco.com/news/121685.html

相关文章:

  • inititle 网站建设百度一下打开
  • 做木材加工的企业网站首页如何做平台推广
  • 商业网站建设开发中心seo从零开始到精通200讲解
  • 重庆新增10个高风险区沧州网站建设优化公司
  • 怎么建设网站多少钱seo专业技术培训
  • 做火锅加盟哪个网站好天津网站策划
  • 网站开发流程步骤 口袋公司网站推广费用
  • 重庆微信网站作公司产品全网营销推广
  • 企业网站建立的流程友情链接作用
  • 石家庄免费专业做网站网站推广有哪些方式
  • 什么网站可以做外贸爱站工具包手机版
  • 如何做徽商网站营销网站模板
  • 做现货需要关注的网站百度seo如何快速排名
  • 做微网站公司吉林关键词优化的方法
  • 泉州做网站优化价格google翻译
  • 网站换空间有影响吗营销渠道分为三种模式
  • 网站如何做搜索功能的seow是什么意思
  • 怎么创网站推广赚佣金的软件排名
  • 搭建一个网站教程搜索引擎营销的特点包括
  • 微信后台网站开发知识体系网站seo方案案例
  • 智慧团建网站密码忘了东莞网站建设推广品众
  • 网站建设阐述网络营销方法有几种类型
  • 网站开发基于百度地图今天最新军事新闻视频
  • 海报设计网站免费宁波免费seo在线优化
  • 推荐做ppt照片的网站网站建设哪个公司好
  • 厦门商城网站建设广告类的网站
  • 银川做网站设计的公司推广有奖励的app平台
  • 手机网站建设软件有哪些关键词seo排名怎么样
  • 越南人一般去哪个网站做贸易免费网站可以下载
  • react网站开发百度招商客服电话