小时候最开心的事莫过于躺在沙发上看《西游记》了。大闹天宫、三打白骨精、真假美猴王......一幕幕精彩的故事萦绕脑海,现在想来,回味无穷。
不知道你有没有注意到这个细节:每当孙悟空到了一个新的环境需要了解本地的“风土人情”时,都会挥舞一下金箍棒,将土地召唤出来。那么你可知道,土地公公接收孙悟空召唤的原理是什么吗?
事件通知机制
我们可以先将其理解为“事件通知机制”,即每当孙悟空将金箍棒敲在地上时,就相当于给土地发了一封 email 的通知,告诉他俺老孙来了,赶快出来接驾。当土地收到通知之后就会立即现身了。
大家都知道 Spring 已经为我们提供好了事件监听、订阅的实现,接下来我们用代码来实现一下这个场景。
首先我们要定义一个事件,来记录下孙悟空敲地的动作。
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;">@Getter</span><br/><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">class</span> <span style="font-weight: bold;color: white;line-height: 26px;">MonkeyKingEvent</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">extends</span> <span style="font-weight: bold;color: white;line-height: 26px;">ApplicationEvent</span> </span>{<br/><br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">private</span> MonkeyKing monkeyKing;<br/><br/> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="color: #a6e22e;font-weight: bold;line-height: 26px;">MonkeyKingEvent</span><span style="line-height: 26px;">(MonkeyKing monkeyKing)</span> </span>{<br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">super</span>(<span style="color: #a6e22e;line-height: 26px;">"monkeyKing"</span>);<br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">this</span>.monkeyKing = monkeyKing;<br/> }<br/><br/>}<br/> |
其中 MonkeyKing
是我们定义好的孙悟空的实体类
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;">@Data</span><br/><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">class</span> <span style="font-weight: bold;color: white;line-height: 26px;">MonkeyKing</span> </span>{<br/><br/> <span style="color: #75715e;line-height: 26px;">/**<br/> * 是否敲地,默认为否<br/> **/</span><br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">private</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">boolean</span> knockGround = <span style="color: #f92672;font-weight: bold;line-height: 26px;">false</span>;<br/><br/>}<br/> |
然后我们需要实现 ApplicationListener
来监听孙悟空敲地的动作。
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;">@Component</span><br/><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">class</span> <span style="font-weight: bold;color: white;line-height: 26px;">MyGuardianListener</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">implements</span> <span style="font-weight: bold;color: white;line-height: 26px;">ApplicationListener</span><<span style="font-weight: bold;color: white;line-height: 26px;">MonkeyKingEvent</span>> </span>{<br/><br/> <span style="color: #75715e;line-height: 26px;">@Override</span><br/> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">void</span> <span style="color: #a6e22e;font-weight: bold;line-height: 26px;">onApplicationEvent</span><span style="line-height: 26px;">(MonkeyKingEvent event)</span> </span>{<br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">boolean</span> knockGround = event.getMonkeyKing().isKnockGround();<br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">if</span>(knockGround){<br/> MyGuardian.appear();<br/> }<span style="color: #f92672;font-weight: bold;line-height: 26px;">else</span>{<br/> MyGuardian.seclusion();<br/> }<br/> }<br/>}<br/> |
最后我们来验证下整个流程。
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;">@PostMapping</span><br/><span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">void</span> <span style="color: #a6e22e;font-weight: bold;line-height: 26px;">testEvent</span><span style="line-height: 26px;">(@RequestParam <span style="color: #f92672;font-weight: bold;line-height: 26px;">boolean</span> knockGround)</span> </span>{<br/> MonkeyKing monkeyKing = <span style="color: #f92672;font-weight: bold;line-height: 26px;">new</span> MonkeyKing();<br/> monkeyKing.setKnockGround(knockGround);<br/> MonkeyKingEvent monkeyKingEvent = <span style="color: #f92672;font-weight: bold;line-height: 26px;">new</span> MonkeyKingEvent(monkeyKing);<br/> <span style="color: #75715e;line-height: 26px;">//发布孙悟空敲地的动作事件</span><br/> applicationEventPublisher.publishEvent(monkeyKingEvent);<br/>}<br/><br/> |
当我们调用testEvent()
方法传入knockGround
为 true
时,打印
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;">土地公公出现了<br/> |
传入为false
时,打印
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;">土地公公遁地了<br/> |
这样我们就简单实现了“孙悟空召唤土地”的功能。你以为这样就结束了?从小老师就教导我们要“知其然,更要知其所以然”。
大家都说读源码更像是在喝咖啡,读不懂又苦又涩,读懂了浓郁醇香。为了不影响大家的好心情,这里我们就不研究它的源码了,我们直捣黄龙。
观察者模式
说是事件通知机制也好,事件监听-订阅的实现也罢,其实它内部的最终实现原理依赖的是观察者模式。看到这,先不要胆怯,不要觉得设计模式晦涩难懂、久攻不下。今天我就用通俗易懂的小故事来带你重新认识一下观察者模式。
故事是这样的,上边我们只说了孙悟空敲地的动作,但是你是否还记得孙悟空将金箍棒往天上一指,便换来雷公电母、龙王等为其施法布雨?闭上双眼,与虎力大仙比试的场景仍历历在目。
由此可见,不光土地能收到孙悟空的通知,连雷公电母和龙王也是可以接收到的。在这里,我们把孙悟空比作主题,也就是大家说的被观察者和 Subject
的概念,把雷公电母和龙王以及土地比作观察者。
以下是我们的代码逻辑:
首先,我们定义一个主题的基础类,里边会记录所有订阅该主题的观察者列表,还包含了增加、删除以及通知观察者的方法。
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">class</span> <span style="font-weight: bold;color: white;line-height: 26px;">Subject</span> </span>{<br/><br/> <span style="color: #75715e;line-height: 26px;">//观察者列表</span><br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">private</span> Vector<Observer> vector = <span style="color: #f92672;font-weight: bold;line-height: 26px;">new</span> Vector();<br/><br/> <span style="color: #75715e;line-height: 26px;">/**<br/> * 增加观察者<br/> **/</span><br/> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">void</span> <span style="color: #a6e22e;font-weight: bold;line-height: 26px;">addObserver</span><span style="line-height: 26px;">(Observer observer)</span></span>{<br/> vector.add(observer);<br/> }<br/><br/> <span style="color: #75715e;line-height: 26px;">/**<br/> * 删除观察者<br/> **/</span><br/> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">void</span> <span style="color: #a6e22e;font-weight: bold;line-height: 26px;">deleteObserver</span><span style="line-height: 26px;">(Observer observer)</span></span>{<br/> vector.remove(observer);<br/> }<br/><br/> <span style="color: #75715e;line-height: 26px;">/**<br/> * 通知所有观察者<br/> **/</span><br/> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">void</span> <span style="color: #a6e22e;font-weight: bold;line-height: 26px;">notifyObserver</span><span style="line-height: 26px;">(String goldenCudgel)</span> </span>{<br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">for</span>(Observer observer : vector) {<br/> observer.update(goldenCudgel);<br/> }<br/> }<br/><br/>}<br/> |
然后,我们定义一个观察者的接口,包含观察者收到通知之后的“动作”。
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">interface</span> <span style="font-weight: bold;color: white;line-height: 26px;">Observer</span> </span>{<br/> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">void</span> <span style="color: #a6e22e;font-weight: bold;line-height: 26px;">update</span><span style="line-height: 26px;">(String goldenCudgel)</span></span>;<br/>}<br/> |
这时候我们再分别定义出“土地”、“雷公电母”、“龙王”的观察者实体类,实现具体的打雷下雨等动作。
“雷公电母”、“龙王”等实现与“土地”类似,故此处仅展示观察者“土地”。
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;">@Component</span><br/><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">class</span> <span style="font-weight: bold;color: white;line-height: 26px;">MyGuardianObserver</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">implements</span> <span style="font-weight: bold;color: white;line-height: 26px;">Observer</span> </span>{<br/><br/> <span style="color: #75715e;line-height: 26px;">@Override</span><br/> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">void</span> <span style="color: #a6e22e;font-weight: bold;line-height: 26px;">update</span><span style="line-height: 26px;">(String goldenCudgel)</span> </span>{<br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">if</span>(upGoldenCudgel(goldenCudgel)) {<br/> System.out.println(<span style="color: #a6e22e;line-height: 26px;">"土地公公出现了"</span>);<br/> }<br/> }<br/><br/> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">boolean</span> <span style="color: #a6e22e;font-weight: bold;line-height: 26px;">upGoldenCudgel</span><span style="line-height: 26px;">(String goldenCudgel)</span></span>{<br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">if</span>(Objects.equals(goldenCudgel,<span style="color: #a6e22e;line-height: 26px;">"down"</span>)){<br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">return</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">true</span>;<br/> }<br/> <span style="color: #f92672;font-weight: bold;line-height: 26px;">return</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">false</span>;<br/> }<br/><br/>}<br/> |
接着,我们就可以定义被观察者的具体实现类“孙悟空”了
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">class</span> <span style="font-weight: bold;color: white;line-height: 26px;">MonkeyKingSubject</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">extends</span> <span style="font-weight: bold;color: white;line-height: 26px;">Subject</span></span>{<br/> <br/> <span style="color: #75715e;line-height: 26px;">/**<br/> * 金箍棒是举起来还是放下呢?哈哈,你猜猜。。。<br/> **/</span><br/> <span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">void</span> <span style="color: #a6e22e;font-weight: bold;line-height: 26px;">doGoldenCudgel</span><span style="line-height: 26px;">(String goldenCudgel)</span></span>{<br/> notifyObserver(goldenCudgel);<br/> }<br/><br/>}<br/> |
最后我们来做个测试看看他们能不能响应孙悟空的通知。
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;"><span style="color: #75715e;line-height: 26px;">@PostMapping</span><br/><span style="line-height: 26px;"><span style="color: #f92672;font-weight: bold;line-height: 26px;">public</span> <span style="color: #f92672;font-weight: bold;line-height: 26px;">void</span> <span style="color: #a6e22e;font-weight: bold;line-height: 26px;">observerTest</span><span style="line-height: 26px;">()</span></span>{<br/> MonkeyKingSubject subject = <span style="color: #f92672;font-weight: bold;line-height: 26px;">new</span> MonkeyKingSubject();<br/> subject.addObserver(<span style="color: #f92672;font-weight: bold;line-height: 26px;">new</span> ThunderGodObserver());<br/> subject.addObserver(<span style="color: #f92672;font-weight: bold;line-height: 26px;">new</span> MyGuardianObserver());<br/> subject.addObserver(<span style="color: #f92672;font-weight: bold;line-height: 26px;">new</span> DragonKingObserver());<br/><br/> subject.doGoldenCudgel(<span style="color: #a6e22e;line-height: 26px;">"up"</span>);<br/> System.out.println(<span style="color: #a6e22e;line-height: 26px;">"我是分割线-----------------------------"</span>);<br/> subject.doGoldenCudgel(<span style="color: #a6e22e;line-height: 26px;">"down"</span>);<br/>}<br/> |
结果展示
1 | <span style="display: block;background: url("https://mmbiz.qpic.cn/mmbiz_svg/QnM5bMcic4Z1CVSiciauH2YVGuhw7Fh8rlD40ib3Jw2DUgWXib7gCuN5ZYWQZJyPRNVj3vjndpuiatDgLtWuAFQYjD5ibHia2PRrV63K/640?wx_fmt=svg") 10px 10px / 40px no-repeat rgb(39, 40, 34);height: 30px;width: 100%;margin-bottom: -7px;border-radius: 5px;"/><code style="overflow-x: auto;padding: 16px;color: #ddd;display: -webkit-box;font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;font-size: 12px;-webkit-overflow-scrolling: touch;padding-top: 15px;background: #272822;border-radius: 5px;">雷公电母发出电闪雷鸣<br/>龙王前来下雨<br/>我是分割线-----------------------------<br/>土地公公出现了<br/> |
总结
故事的最后怎么能少的了总结呢?观察者模式与事件通知机制都是在一对多的关系中,当一个对象被修改时,则会自动通知依赖它的对象,两者之间相互独立,互相解耦,这样既省去了反复检索状态的资源消耗,也能够得到最高的反馈速度。
当然它的缺点也不容忽视:
如果一个被观察者对象有很多的直接和间接的观察者的话,将所有的观察者都通知到会花费很多时间; 如果在观察者和观察目标之间有循环依赖的话,观察目标会触发它们之间进行循环调用,可能导致系统崩溃; 观察者模式没有相应的机制让观察者知道所观察的目标对象是怎么发生变化的,而仅仅只是知道观察目标发生了变化;
文章的最后,照例奉上源码,后台回复 event
即可获取。以上就是今天的全部内容了,如果你有不同的意见或者更好的idea
,欢迎联系阿Q,添加阿Q可以加入技术交流群参与讨论呦!