【spring】spring自带的观察者模式【转载】 ゞ 浴缸里的玫瑰 2023-01-13 05:47 59阅读 0赞 > `前言` 转载自:[https://mp.weixin.qq.com/s?srcid=0415igWGyJhQOhntXUaSd56G][https_mp.weixin.qq.com_s_srcid_0415igWGyJhQOhntXUaSd56G] ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3MxNDQxMTAxMjY1_size_16_color_FFFFFF_t_70] > `代码示例` * 首先,观察目标类 import lombok.Getter; import org.springframework.context.ApplicationEvent; /** * @author qilong.sun * @version v1.0 * @time 2021/4/16 9:33 * @title 观察者目标类 * @description */ @Getter public class JavaStackEvent extends ApplicationEvent { /** * Create a new {@code ApplicationEvent}. * * @param source the object on which the event initially occurred or with * which the event is associated (never {@code null}) */ public JavaStackEvent(Object source) { super(source); } } * 然后,观察者类 import lombok.NonNull; import lombok.RequiredArgsConstructor; import org.springframework.context.ApplicationListener; import org.springframework.scheduling.annotation.Async; /** * @author qilong.sun * @version v1.0 * @time 2021/4/16 9:42 * @title 观察者类 * @description */ @RequiredArgsConstructor public class ReaderListener implements ApplicationListener<JavaStackEvent> { @NonNull private String name; private String article; @Async @Override public void onApplicationEvent(JavaStackEvent event) { // 更新文章 updateArticle(event); } private void updateArticle(JavaStackEvent event) { this.article = (String) event.getSource(); System.out.printf("我是读者:%s,文章已更新为:%s\n", this.name, this.article); } } * 最后,测试配置类;也就是用户实例 import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author qilong.sun * @version v1.0 * @time 2021/4/16 9:53 * @title 测试配置类 * @description */ @Slf4j @Configuration public class ObserverConfiguration { @Bean public CommandLineRunner commandLineRunner(ApplicationContext context) { return (args) -> { log.info("发布事件:什么是观察者模式?"); context.publishEvent(new JavaStackEvent("什么是观察者模式?")); }; } @Bean public ReaderListener readerListener1(){ return new ReaderListener("小明"); } @Bean public ReaderListener readerListener2(){ return new ReaderListener("小张"); } @Bean public ReaderListener readerListener3(){ return new ReaderListener("小爱"); } } > `测试` 启动项目,控制台打印信息: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3MxNDQxMTAxMjY1_size_16_color_FFFFFF_t_70 1] > `理解` [https_mp.weixin.qq.com_s_srcid_0415igWGyJhQOhntXUaSd56G]: https://mp.weixin.qq.com/s?srcid=0415igWGyJhQOhntXUaSd56G&scene=22&sharer_sharetime=1618453332302&mid=2247495875&sharer_shareid=304c34be1ddc2c7df7787d080e240ce2&sn=3a1e1143c5a9a08abee885e632b1dc15&idx=1&__biz=MzIyNzc1ODQ0MQ%3D%3D&chksm=e85ee8c5df2961d3ab215fd68c2c29f9523c6c00c13b500ef014b2449d12835130a25c5c69ad&mpshare=1#rd [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3MxNDQxMTAxMjY1_size_16_color_FFFFFF_t_70]: /images/20221022/9fc6374a0b1c413d961806613602d3fd.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3MxNDQxMTAxMjY1_size_16_color_FFFFFF_t_70 1]: /images/20221022/e99c7e231ac544959a5a68cfeaac8dd3.png
相关 设计模式 - JDK 自带的观察者模式 文章目录 * 设计模式 - JDK 自带的观察者模式 * * * * 1、被观察者 * 2、观察者接口 ... 桃扇骨/ 2024年04月17日 22:27/ 0 赞/ 114 阅读
相关 【spring】spring自带的观察者模式【转载】 > `前言` 转载自:[https://mp.weixin.qq.com/s?srcid=0415igWGyJhQOhntXUaSd56G][https_mp.weixin. ゞ 浴缸里的玫瑰/ 2023年01月13日 05:47/ 0 赞/ 60 阅读
相关 观察者模式 观察者模式 Observer 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。 这个主题对象在状态上发生变化时,会通知所有观察者对 梦里梦外;/ 2022年08月03日 08:20/ 0 赞/ 140 阅读
相关 观察者模式 什么是观察者模式 有人这么说 > 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。 > > 这个主题对象在状态上发生变化时,会通知所有观 梦里梦外;/ 2022年07月20日 12:05/ 0 赞/ 390 阅读
相关 观察者模式 场景描述: 一个气象站应用,可以实时获取温度、湿度和气压信息,气象站提供一个封装好的类WeatherData,该类有最新的气象信息,当这些信息发生变动的时候,类中的meas 叁歲伎倆/ 2022年06月14日 10:24/ 0 赞/ 285 阅读
相关 观察者模式 观察者模式:定义了对象之间的一对多的依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。 观察者模式图: ![输入图片说明][13105107_Mf 旧城等待,/ 2022年06月03日 02:41/ 0 赞/ 383 阅读
相关 观察者模式 什么是观察者模式? 简单的来说,观察者模式=出版者+订阅者。用比较书面的话来说的话是:定义了对象之间的一对多依赖,当一所对应的对象状态改变时,它的所有依赖者都会收到通知并 你的名字/ 2022年02月01日 13:53/ 0 赞/ 519 阅读
相关 观察者模式 当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知它的依赖对象。观察者模式属于行为型模式。 意图:定义对象 系统管理员/ 2021年09月17日 01:36/ 0 赞/ 619 阅读
相关 观察者模式 对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知它的依赖对象。观察者模式属于行为型模式。 介绍 ... 小灰灰/ 2020年06月13日 05:42/ 0 赞/ 701 阅读
还没有评论,来说两句吧...