日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

设计模式(三):观察者模式与发布/订阅模式区别

發(fā)布時間:2024/4/17 asp.net 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式(三):观察者模式与发布/订阅模式区别 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在翻閱資料的時候,有人把觀察者(Observer)模式等同于發(fā)布(Publish)/訂閱(Subscribe)模式,也有人認為這兩種模式還是存在差異,而我認為確實是存在差異的,本質(zhì)上的區(qū)別是調(diào)度的地方不同。

觀察者模式

比較概念的解釋是,目標和觀察者是基類,目標提供維護觀察者的一系列方法,觀察者提供更新接口。具體觀察者和具體目標繼承各自的基類,然后具體觀察者把自己注冊到具體目標里,在具體目標發(fā)生變化時候,調(diào)度觀察者的更新方法。

比如有個“天氣中心”的具體目標A,專門監(jiān)聽天氣變化,而有個顯示天氣的界面的觀察者B,B就把自己注冊到A里,當A觸發(fā)天氣變化,就調(diào)度B的更新方法,并帶上自己的上下文。

發(fā)布/訂閱模式

比較概念的解釋是,訂閱者把自己想訂閱的事件注冊到調(diào)度中心,當該事件觸發(fā)時候,發(fā)布者發(fā)布該事件到調(diào)度中心(順帶上下文),由調(diào)度中心統(tǒng)一調(diào)度訂閱者注冊到調(diào)度中心的處理代碼。

比如有個界面是實時顯示天氣,它就訂閱天氣事件(注冊到調(diào)度中心,包括處理程序),當天氣變化時(定時獲取數(shù)據(jù)),就作為發(fā)布者發(fā)布天氣信息到調(diào)度中心,調(diào)度中心就調(diào)度訂閱者的天氣處理程序。

總結

1. 從兩張圖片可以看到,最大的區(qū)別是調(diào)度的地方。

雖然兩種模式都存在訂閱者和發(fā)布者(具體觀察者可認為是訂閱者、具體目標可認為是發(fā)布者),但是觀察者模式是由具體目標調(diào)度的,而發(fā)布/訂閱模式是統(tǒng)一由調(diào)度中心調(diào)的,所以觀察者模式的訂閱者與發(fā)布者之間是存在依賴的,而發(fā)布/訂閱模式則不會。

2. 兩種模式都可以用于松散耦合,改進代碼管理和潛在的復用。

附錄

觀察者模式實現(xiàn)代碼(JavaScript版):

//觀察者列表 function ObserverList(){this.observerList = []; } ObserverList.prototype.add = function( obj ){return this.observerList.push( obj ); }; ObserverList.prototype.count = function(){return this.observerList.length; }; ObserverList.prototype.get = function( index ){if( index > -1 && index < this.observerList.length ){return this.observerList[ index ];} }; ObserverList.prototype.indexOf = function( obj, startIndex ){var i = startIndex;while( i < this.observerList.length ){if( this.observerList[i] === obj ){return i;}i++;}return -1; }; ObserverList.prototype.removeAt = function( index ){this.observerList.splice( index, 1 ); };//目標 function Subject(){this.observers = new ObserverList(); } Subject.prototype.addObserver = function( observer ){this.observers.add( observer ); }; Subject.prototype.removeObserver = function( observer ){this.observers.removeAt( this.observers.indexOf( observer, 0 ) ); }; Subject.prototype.notify = function( context ){var observerCount = this.observers.count();for(var i=0; i < observerCount; i++){this.observers.get(i).update( context );} };//觀察者 function Observer(){this.update = function(){// ... }; }

發(fā)布/訂閱模式實現(xiàn)代碼(JavaScript經(jīng)典版):

var pubsub = {}; (function(myObject) {// Storage for topics that can be broadcast// or listened tovar topics = {};// An topic identifiervar subUid = -1;// Publish or broadcast events of interest// with a specific topic name and arguments// such as the data to pass alongmyObject.publish = function( topic, args ) {if ( !topics[topic] ) {return false;}var subscribers = topics[topic],len = subscribers ? subscribers.length : 0;while (len--) {subscribers[len].func( topic, args );}return this;};// Subscribe to events of interest// with a specific topic name and a// callback function, to be executed// when the topic/event is observedmyObject.subscribe = function( topic, func ) {if (!topics[topic]) {topics[topic] = [];}var token = ( ++subUid ).toString();topics[topic].push({token: token,func: func});return token;};// Unsubscribe from a specific// topic, based on a tokenized reference// to the subscriptionmyObject.unsubscribe = function( token ) {for ( var m in topics ) {if ( topics[m] ) {for ( var i = 0, j = topics[m].length; i < j; i++ ) {if ( topics[m][i].token === token ) {topics[m].splice( i, 1 );return token;}}}}return this;}; }( pubsub ));

參考文獻

1. 《Learning JavaScript Design Patterns》?by?Addy Osmani

?

本文為原創(chuàng)文章,轉(zhuǎn)載請保留原出處,方便溯源,如有錯誤地方,謝謝指正。

本文地址 :http://www.cnblogs.com/lovesong/p/5272752.html

轉(zhuǎn)載于:https://www.cnblogs.com/lovesong/p/5272752.html

總結

以上是生活随笔為你收集整理的设计模式(三):观察者模式与发布/订阅模式区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。