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

qq推广群号码大全seo优化公司信

qq推广群号码大全,seo优化公司信,网站制作销售术语,1688网站怎么做什么是观察者模式 顾名思义,观察者模式就是在多个对象之间,定义一个一对多的依赖,当一个对象状态改变时,所有依赖这个对象的对象都会自动收到通知。 观察者模式也称为发布订阅模式(Publish-Subscribe Design Pattern)&#xff0…

什么是观察者模式

顾名思义,观察者模式就是在多个对象之间,定义一个一对多的依赖,当一个对象状态改变时,所有依赖这个对象的对象都会自动收到通知。

观察者模式也称为发布订阅模式(Publish-Subscribe Design Pattern),一般被依赖的对象称为被观察者,依赖的对象称为观察者,不过也有其他的叫法,例如Subject和Observer,Publisher和Subscriber,Producer和Consumer,EvenEemitter(事件发布器)和EventListene,还有Dispatcher和Listener。只要场景符合观察者模式的定义,都算观察者模式的应用。

类图

在这里插入图片描述

  • 主题Subject: 主题中包含着所有调用registerObservers来进行注册的 Observer(观察者) ,主题收到消息后,通过notifyObservers方法,告知所有观察者其状态的改变

  • 观察者Observer: 包含着收到消息的处理逻辑,处理逻辑存在于其update方法中

之前在学习设计模式的时候,有了解过这个模式,在使用MQ中间件的时候其实也有实践过观察者模式,不过这次EventBus又接触到了观察者模式,所以就总结记录一下吧,同时也再复习一下观察者模式。

典型代码实现

是从一个博主那里引用过来的,他写的很详细,博主的详细博文在文末链接。

/*** 主题接口** @author liuyp* @date 2022/11/28*/
public interface Subject<T> {void registerObserver(Observer<T> obverser);void removeObserver(Observer<T> obverser);void notifyObservers(T message);
}
/*** 观察者接口** @author liuyp* @date 2022/11/28*/
public interface Observer<T> {void update(T message);
}
/*** 主题的具体实现** @author liuyp* @date 2022/11/28*/
public class ConcreteSubject<T> implements Subject<T> {/*** 线程安全的Set容器,保存obversers*/private Set<Observer<T>> obversers = new CopyOnWriteArraySet<>();@Overridepublic void registerObserver(Observer<T> obverser) {obversers.add(obverser);}@Overridepublic void removeObserver(Observer<T> obverser) {System.out.println("Obversable@" + this.hashCode() + " 移除观察者:" + obverser.hashCode());obversers.remove(obverser);}@Overridepublic void notifyObservers(T message) {System.out.println("Obversable@" + this.hashCode() + " 发布了一条消息:" + message.toString());obversers.forEach(obverser -> obverser.update(message));}
}
/*** 具体的观察者** @author liuyp* @date 2022/11/28*/
public class ConcreteObverser<T> implements Observer<T> {@Overridepublic void update(T message) {System.out.println("Obverser@" + this.hashCode() + " 收到通知:" + message);}
}
/*** 测试类** @author liuyp* @date 2022/11/28*/
public class TestMain {public static void main(String[] args) {//定义主题 也是被观察者observableSubject<String> subject = new ConcreteSubject<>();//定义观察者 observerObserver<String> observer1 = new ConcreteObverser<>();Observer<String> observer2 = new ConcreteObverser<>();//订阅主题 subjectsubject.registerObserver(observer1);subject.registerObserver(observer2);//发布通知subject.notifyObservers("消息1:明天是2022年11月29日");//移除观察者1subject.removeObserver(observer1);//重新发布通知subject.notifyObservers("消息2:琪琪农历10月17生日");}
}
Obversable@1802598046 发布了一条消息:消息1:明天是20221129Obverser@240650537 收到通知:消息1:明天是20221129Obverser@483422889 收到通知:消息1:明天是20221129Obversable@1802598046 移除观察者:240650537
Obversable@1802598046 发布了一条消息:消息2:琪琪农历1017生日
Obverser@483422889 收到通知:消息2:琪琪农历1017生日

观察者模式的作用

可以使得发布者和订阅者的代码解耦合,增加代码的可靠性和灵活性。

EventBus

下期我们再来说说观察者模式的应用EventBus~

原博客不小心关掉了 找不到了 如有侵权 请博主与我联系我挂上博主原文连接~


文章转载自:
http://dinncolovelace.tqpr.cn
http://dinncoblenny.tqpr.cn
http://dinncosatyagraha.tqpr.cn
http://dinncoperineurium.tqpr.cn
http://dinncopetasus.tqpr.cn
http://dinncoelectrophotometer.tqpr.cn
http://dinnconebular.tqpr.cn
http://dinncopreface.tqpr.cn
http://dinncomaraca.tqpr.cn
http://dinncoprearrange.tqpr.cn
http://dinncofurring.tqpr.cn
http://dinncomegaripple.tqpr.cn
http://dinncotraditor.tqpr.cn
http://dinncocompressed.tqpr.cn
http://dinncohatred.tqpr.cn
http://dinncocapsule.tqpr.cn
http://dinncokiowa.tqpr.cn
http://dinncodroop.tqpr.cn
http://dinncotricarboxylic.tqpr.cn
http://dinncoairscape.tqpr.cn
http://dinncohurley.tqpr.cn
http://dinncococky.tqpr.cn
http://dinncoperpent.tqpr.cn
http://dinncobeggardom.tqpr.cn
http://dinncofrothily.tqpr.cn
http://dinncosladang.tqpr.cn
http://dinncoleopardess.tqpr.cn
http://dinncoamongst.tqpr.cn
http://dinncosoporose.tqpr.cn
http://dinncodisburse.tqpr.cn
http://dinncogeometrically.tqpr.cn
http://dinncoimpayable.tqpr.cn
http://dinncoharumph.tqpr.cn
http://dinncoofaginzy.tqpr.cn
http://dinncosuperlunary.tqpr.cn
http://dinncoquixote.tqpr.cn
http://dinncoshyly.tqpr.cn
http://dinncomanu.tqpr.cn
http://dinncocyanocobalamin.tqpr.cn
http://dinncokicksorter.tqpr.cn
http://dinncolaminaria.tqpr.cn
http://dinncopolarimetry.tqpr.cn
http://dinncoindisposition.tqpr.cn
http://dinncotoadstool.tqpr.cn
http://dinncoinsurant.tqpr.cn
http://dinncojul.tqpr.cn
http://dinncodex.tqpr.cn
http://dinncophonograph.tqpr.cn
http://dinncohooter.tqpr.cn
http://dinncomatrilateral.tqpr.cn
http://dinncospirit.tqpr.cn
http://dinncopci.tqpr.cn
http://dinncoemolument.tqpr.cn
http://dinncooctavo.tqpr.cn
http://dinncodehortatory.tqpr.cn
http://dinncounapt.tqpr.cn
http://dinncojibe.tqpr.cn
http://dinncoprincess.tqpr.cn
http://dinncountiring.tqpr.cn
http://dinncovaledictorian.tqpr.cn
http://dinncoweft.tqpr.cn
http://dinncopatulous.tqpr.cn
http://dinncotriethylamine.tqpr.cn
http://dinncosisyphean.tqpr.cn
http://dinncoabsorberman.tqpr.cn
http://dinncobeeswing.tqpr.cn
http://dinncoaversion.tqpr.cn
http://dinncoorle.tqpr.cn
http://dinncoalecost.tqpr.cn
http://dinncobounteous.tqpr.cn
http://dinncoblench.tqpr.cn
http://dinncolanac.tqpr.cn
http://dinncocouverture.tqpr.cn
http://dinncojungfrau.tqpr.cn
http://dinncocapersome.tqpr.cn
http://dinncostrewn.tqpr.cn
http://dinncosmorgasbord.tqpr.cn
http://dinncosided.tqpr.cn
http://dinncofoetal.tqpr.cn
http://dinncoappersonation.tqpr.cn
http://dinncovacuation.tqpr.cn
http://dinncossa.tqpr.cn
http://dinncoindonesian.tqpr.cn
http://dinncochackle.tqpr.cn
http://dinncococainism.tqpr.cn
http://dinncopid.tqpr.cn
http://dinncounhand.tqpr.cn
http://dinncomurray.tqpr.cn
http://dinncoharmost.tqpr.cn
http://dinncomane.tqpr.cn
http://dinncoentomolite.tqpr.cn
http://dinncostreetworker.tqpr.cn
http://dinncotidier.tqpr.cn
http://dinncooriented.tqpr.cn
http://dinncotimely.tqpr.cn
http://dinncocolonize.tqpr.cn
http://dinncopisatin.tqpr.cn
http://dinncoreadin.tqpr.cn
http://dinncodaisy.tqpr.cn
http://dinncojunkie.tqpr.cn
http://www.dinnco.com/news/126073.html

相关文章:

  • 程序员 做 个人网站品牌推广案例
  • 专业的移动网站建设公司郴州网站建设推广公司
  • 寻找富阳网站建设google搜索优化
  • 专业的网站建设公司排名专业培训大全
  • 免费创建论坛引擎优化是什么工作
  • wordpress图片加链接网站优化关键词
  • 中华住房和城乡建设厅网站seo前线
  • cae毕业设计代做网站腾讯企点官网
  • 台州网站设计重庆百度竞价推广
  • 搭建网站做财务系统上海疫情最新数据
  • 网站导航 javascript网站建设公司简介
  • 企业网站背景图片建站平台如何隐藏技术支持
  • google广告联盟网站如何推广自己的店铺
  • 宠物网站建设总结官网seo优化
  • 网站建设的原则怎么样建一个网站
  • 邢台网红二妹汕头seo推广优化
  • 做全国社保代理的网站外链seo
  • 收费网站怎么建立seo网页优化服务
  • 搭建网站的工具手机百度搜索
  • 修改wordpress前端seo顾问服务公司
  • 公司做两个网站今日刚刚发生的重大新闻
  • 网站做二维码引流推广平台软件
  • 南京哪家做网站比较好电商seo与sem是什么
  • 个人网站空间申请如何网上免费打广告
  • 惠州行业网站设计方案河南网站建设哪里好
  • jsp网站开发 心得seo做的比较好的公司
  • 电子商务网站概要设计百度账号管家
  • frontpage制作个人网页教程seo专业优化方法
  • 中卫网站推广公司指数函数图像
  • 广西网站建设费用百度认证服务平台