欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS设计模式 --- 观察者模式

程序员文章站 2022-08-07 23:35:59
JS设计模式 — 观察者模式又来更新了,昨天学了白贺翔老师的观察者模式,分享一下,直接看代码! Document</titl...</div> <div class="content"> <h2> <a id="JS____0" target="_blank" rel="nofollow" href="/default/index/url?u=aHR0cHM6Ly93d3cuMTBxaWFud2FuLmNvbQ=="></a>JS设计模式 — 观察者模式</h2> <p><mark>又来更新了,昨天学了白贺翔老师的观察者模式,分享一下,直接看代码!</mark></p> <pre><code class="prism language-csharp"><span class="token operator"><</span><span class="token operator">!</span><span class="token class-name">DOCTYPE</span> html<span class="token operator">></span> <span class="token operator"><</span>html lang<span class="token operator">=</span><span class="token string">"en"</span><span class="token operator">></span> <span class="token operator"><</span>head<span class="token operator">></span> <span class="token operator"><</span>meta charset<span class="token operator">=</span><span class="token string">"UTF-8"</span><span class="token operator">></span> <span class="token operator"><</span>meta name<span class="token operator">=</span><span class="token string">"viewport"</span> content<span class="token operator">=</span><span class="token string">"width=device-width, initial-scale=1.0"</span><span class="token operator">></span> <span class="token operator"><</span>title<span class="token operator">></span>Document<span class="token operator"><</span><span class="token operator">/</span>title<span class="token operator">></span> <span class="token operator"><</span>script src<span class="token operator">=</span><span class="token string">"./CommonUtil.js"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>head<span class="token operator">></span> <span class="token operator"><</span>body<span class="token operator">></span> <span class="token operator"><</span>script charset<span class="token operator">=</span><span class="token string">"utf-8"</span><span class="token operator">></span> <span class="token comment">// JS观察者模式:对程序中的某一个对象进行实时观察,当该对象发生变化,进行通知</span> <span class="token comment">// 有两个重要的角色:观察者 被观察者</span> <span class="token comment">// 经典案例:订报纸(报社(也叫发布者,被观察者),订阅者(观察者))</span> <span class="token comment">// 发布者 被观察者</span> <span class="token keyword">var</span> Publish<span class="token operator">=</span><span class="token function">function</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token operator">=</span>name<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>subscribers<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//接受所有的订阅者(每一个订阅者都是一个函数类型fn)</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// Publish类实例对象发布消息的方法</span> Publish<span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>deliver<span class="token operator">=</span><span class="token function">function</span><span class="token punctuation">(</span>news<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">var</span> publish<span class="token operator">=</span><span class="token keyword">this</span><span class="token punctuation">;</span><span class="token comment">//当前实例 当前发布消息的报社</span> <span class="token keyword">this</span><span class="token punctuation">.</span>subscribers<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token function">function</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//把消息发布给这家报社的所有的订阅者</span> <span class="token function">fn</span><span class="token punctuation">(</span>news<span class="token punctuation">,</span>publish<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">;</span><span class="token comment">//链式编程</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// 因为所有的订阅者都是函数类型fn,所以直接在Function的原型上添加方法</span> <span class="token comment">// 订阅者 观察者 具体的一个订阅者去订阅报纸的方法</span> Function<span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>subscriber<span class="token operator">=</span><span class="token function">function</span><span class="token punctuation">(</span>publish<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">var</span> sub<span class="token operator">=</span><span class="token keyword">this</span><span class="token punctuation">;</span><span class="token comment">//取得当前订阅的报纸的这个人</span> <span class="token comment">// 先判断这家报社是否已经存在当前订阅者</span> <span class="token comment">// some方法:循环遍历数组的每一个元素,每一个元素读取执行一个函数,如果其中有一个返回true,那整体就返回true</span> <span class="token keyword">var</span> alreadyExists<span class="token operator">=</span> publish<span class="token punctuation">.</span>subscribers<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span><span class="token function">function</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> item <span class="token operator">===</span> sub<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>alreadyExists<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//如果不为真,表明该订阅者没有在这家报社订阅过</span> publish<span class="token punctuation">.</span>subscribers<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//就添加进去</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// 订阅者 观察者 具体的一个订阅者去取消订阅报纸的方法</span> Function<span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>unsubscriber<span class="token operator">=</span><span class="token function">function</span><span class="token punctuation">(</span>publish<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">var</span> sub<span class="token operator">=</span><span class="token keyword">this</span><span class="token punctuation">;</span><span class="token comment">//取得当前取消订阅的报纸的这个人</span> <span class="token comment">// 先判断当前订阅者是否在这个报社里,如果在,就把他过滤掉</span> <span class="token comment">// filter过滤函数:循环遍历数组的每一个元素,每一个元素读取执行一个函数,如果不匹配就过滤(删除)该元素</span> publish<span class="token punctuation">.</span>subscribers<span class="token operator">=</span>publish<span class="token punctuation">.</span>subscribers<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token function">function</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> item <span class="token operator">!==</span> sub<span class="token punctuation">;</span><span class="token comment">//满足的就返回,不满足的就过滤</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">;</span><span class="token comment">//链式编程 </span> <span class="token punctuation">}</span><span class="token punctuation">;</span> window<span class="token punctuation">.</span>onload<span class="token operator">=</span><span class="token function">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">// 实例化发布者对象(报社 被观察者)</span> <span class="token keyword">var</span> pub1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Publish</span><span class="token punctuation">(</span><span class="token string">'第一家报社'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">var</span> pub2<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Publish</span><span class="token punctuation">(</span><span class="token string">'第二家报社'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">var</span> pub3<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Publish</span><span class="token punctuation">(</span><span class="token string">'第三家报社'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 观察者 订阅者接收消息</span> <span class="token keyword">var</span> sub1<span class="token operator">=</span><span class="token function">function</span><span class="token punctuation">(</span>news<span class="token punctuation">)</span><span class="token punctuation">{</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'sub1'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>innerHTML <span class="token operator">+</span><span class="token operator">=</span> arguments<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">+</span> <span class="token string">":"</span> <span class="token operator">+</span> news <span class="token operator">+</span> <span class="token string">"\n"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token keyword">var</span> sub2<span class="token operator">=</span><span class="token function">function</span><span class="token punctuation">(</span>news<span class="token punctuation">)</span><span class="token punctuation">{</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'sub2'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>innerHTML <span class="token operator">+</span><span class="token operator">=</span> arguments<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>name <span class="token operator">+</span> <span class="token string">":"</span> <span class="token operator">+</span> news <span class="token operator">+</span> <span class="token string">"\n"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// 订阅者订阅报纸 执行方法</span> sub1<span class="token punctuation">.</span><span class="token function">subscriber</span><span class="token punctuation">(</span>pub1<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">subscriber</span><span class="token punctuation">(</span>pub2<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">subscriber</span><span class="token punctuation">(</span>pub3<span class="token punctuation">)</span><span class="token punctuation">;</span> sub2<span class="token punctuation">.</span><span class="token function">subscriber</span><span class="token punctuation">(</span>pub1<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">subscriber</span><span class="token punctuation">(</span>pub2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 事件绑定 BH.EventUtil.addHandler(document.getElementById('pub1'),'click',function(){</span> pub1<span class="token punctuation">.</span><span class="token function">deliver</span><span class="token punctuation">(</span>document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'text1'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token keyword">value</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> BH<span class="token punctuation">.</span>EventUtil<span class="token punctuation">.</span><span class="token function">addHandler</span><span class="token punctuation">(</span>document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'pub2'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">'click'</span><span class="token punctuation">,</span><span class="token function">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> pub2<span class="token punctuation">.</span><span class="token function">deliver</span><span class="token punctuation">(</span>document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'text2'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token keyword">value</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> BH<span class="token punctuation">.</span>EventUtil<span class="token punctuation">.</span><span class="token function">addHandler</span><span class="token punctuation">(</span>document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'pub3'</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">'click'</span><span class="token punctuation">,</span><span class="token function">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> pub3<span class="token punctuation">.</span><span class="token function">deliver</span><span class="token punctuation">(</span>document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'text3'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token keyword">value</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 取消订阅</span> sub2<span class="token punctuation">.</span><span class="token function">unsubscriber</span><span class="token punctuation">(</span>pub2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span> <span class="token operator"><</span>input type<span class="token operator">=</span><span class="token string">"button"</span> id<span class="token operator">=</span><span class="token string">"pub1"</span> <span class="token keyword">value</span><span class="token operator">=</span><span class="token string">"第一家报社"</span> <span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>input type<span class="token operator">=</span><span class="token string">"text"</span> id<span class="token operator">=</span><span class="token string">"text1"</span> <span class="token operator">/</span><span class="token operator">></span><span class="token operator"><</span>br<span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>input type<span class="token operator">=</span><span class="token string">"button"</span> id<span class="token operator">=</span><span class="token string">"pub2"</span> <span class="token keyword">value</span><span class="token operator">=</span><span class="token string">"第二家报社"</span> <span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>input type<span class="token operator">=</span><span class="token string">"text"</span> id<span class="token operator">=</span><span class="token string">"text2"</span> <span class="token operator">/</span><span class="token operator">></span><span class="token operator"><</span>br<span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>input type<span class="token operator">=</span><span class="token string">"button"</span> id<span class="token operator">=</span><span class="token string">"pub3"</span> <span class="token keyword">value</span><span class="token operator">=</span><span class="token string">"第三家报社"</span> <span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>input type<span class="token operator">=</span><span class="token string">"text"</span> id<span class="token operator">=</span><span class="token string">"text3"</span> <span class="token operator">/</span><span class="token operator">></span><span class="token operator"><</span>br<span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>textarea id<span class="token operator">=</span><span class="token string">"sub1"</span> cols<span class="token operator">=</span><span class="token string">"30"</span> rows<span class="token operator">=</span><span class="token string">"5"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>textarea<span class="token operator">></span> <span class="token operator"><</span>textarea id<span class="token operator">=</span><span class="token string">"sub2"</span> cols<span class="token operator">=</span><span class="token string">"30"</span> rows<span class="token operator">=</span><span class="token string">"5"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>textarea<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>body<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>html<span class="token operator">></span> </code></pre> <p>本文地址:https://blog.csdn.net/zz_xiaojun/article/details/109381320</p> </div> <div class="tags"> 相关标签: <a href="/t/JavaScript/" target="_blank"> JavaScript </a> </div> <div class="info-pre-next"> <p> 上一篇: <a href="/article/1794961.html"> vue 公共列表选择组件,引用Vant-UI的样式方式 </a> </p> <p> 下一篇: <a href="/article/1794963.html"> 微信支付宝版电子营业执照是怎么回事?微信支付宝版电子营业执照怎么申请? </a> </p> </div> <div class="wz_tuijian"> <p> 推荐阅读 </p> <ul> <li> <a href="/article/2185899.html" target="_blank" title="JavaScript设计模式之命令模式实例分析"> <h2> JavaScript设计模式之命令模式实例分析 </h2> </a> </li> <li> <a href="/article/2185898.html" target="_blank" title="JavaScript设计模式之享元模式实例详解"> <h2> JavaScript设计模式之享元模式实例详解 </h2> </a> </li> <li> <a href="/article/2185801.html" target="_blank" title="JavaScript设计模式之装饰者模式实例详解"> <h2> JavaScript设计模式之装饰者模式实例详解 </h2> </a> </li> <li> <a href="/article/2184562.html" target="_blank" title="大话设计模式笔记(十六)の组合模式"> <h2> 大话设计模式笔记(十六)の组合模式 </h2> </a> </li> <li> <a href="/article/2184392.html" target="_blank" title="大话设计模式笔记(十五)の备忘录模式"> <h2> 大话设计模式笔记(十五)の备忘录模式 </h2> </a> </li> <li> <a href="/article/2184249.html" target="_blank" title="总结JavaScript设计模式编程中的享元模式使用"> <h2> 总结JavaScript设计模式编程中的享元模式使用 </h2> </a> </li> <li> <a href="/article/2184278.html" target="_blank" title="常用的javascript设计模式"> <h2> 常用的javascript设计模式 </h2> </a> </li> <li> <a href="/article/2183223.html" target="_blank" title="【朝花夕拾】设计模式之单例模式"> <h2> 【朝花夕拾】设计模式之单例模式 </h2> </a> </li> <li> <a href="/article/2183207.html" target="_blank" title="【朝花夕拾】设计模式之中介者模式"> <h2> 【朝花夕拾】设计模式之中介者模式 </h2> </a> </li> <li> <a href="/article/2183031.html" target="_blank" title="设计模式之装饰者模式"> <h2> 设计模式之装饰者模式 </h2> </a> </li> </ul> </div> </article> </div> </main> <footer><div class="box"><div class="ft_nav"><div class="ft_about"><p>关于网站</p><ul><li><a href="/sitemap.xml" target="_blank" title="网站地图">网站地图</a></li><li><a href="/list/2/" title="最新程序员文章站">最新程序员文章站</a></li></ul></div><div class="ft_contact"><ul><li>本站所有数据收集于网络如有侵犯到您的权益,请联系我们进行下架处理。</li><li class="email_show"></li></ul></div></div><div class="copyright"><div class="cr_left"><p> 备案号:<a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">粤ICP备20058927号</a></p><p>© Copyright © 2020-2022 www.superweb999.com 程序员文章站. </p></div></div></div></footer> </body> </html>