国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript編程設計模式之觀察者模式(Observer Pattern)實例詳解

2024-05-06 15:25:15
字體:
來源:轉載
供稿:網友

本文實例講述了JavaScript編程設計模式之觀察者模式。分享給大家供大家參考,具體如下:

簡介

簡單的解釋觀察者模式,就是一個對象(subject)維護一個依賴他的對象(observers)列表,當自身狀態發生變化時,自動通知所有觀察者對象。當某個對象不需要獲得通知時,可以從對象列表中刪除掉。

從上面的解釋中我們可以提煉出三個componet: Subject, ObserverList和Observer,用JS實現很簡單:

function ObserverList(){ this.observerList = [];}ObserverList.prototype.Add = function( obj ){ return this.observerList.push( obj );};ObserverList.prototype.Empty = function(){ this.observerList = [];};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.Insert = function( obj, index ){ var pointer = -1; if( index === 0 ){  this.observerList.unshift( obj );  pointer = index; }else if( index === this.observerList.length ){  this.observerList.push( obj );  pointer = index; } return pointer;};ObserverList.prototype.IndexOf = function( obj, startIndex ){ var i = startIndex, pointer = -1; while( i < this.observerList.length ){  if( this.observerList[i] === obj ){   pointer = i;  }  i++; } return pointer;};ObserverList.prototype.RemoveAt = function( index ){ if( index === 0 ){  this.observerList.shift(); }else if( index === this.observerList.length -1 ){  this.observerList.pop(); }};// Extend an object with an extensionfunction extend( extension, obj ){ for ( var key in extension ){  obj[key] = extension[key]; }}

Subject擁有增加和刪除Observer的能力

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 ); }};

最后定義一個觀察者對象,實現update方法

// The Observerfunction Observer(){ this.Update = function(){  // ... };}

當有多個觀察者,只需擴展上面的基本對象,并重寫Update方法。

盡管觀察則模式被廣泛使用,但在JS中經常使用它的變體: 發布訂閱模式

發布訂閱模式通過一個topic/event通道,解耦了觀察者模式中Subject(發布者)和Observer(訂閱者)之間耦合的問題,在JS中被廣泛使用。

下面簡單的例子說明了使用發布訂閱模式的基本結構

// A very simple new mail handler// A count of the number of messages receivedvar mailCounter = 0;// Initialize subscribers that will listen out for a topic// with the name "inbox/newMessage".// Render a preview of new messagesvar subscriber1 = subscribe( "inbox/newMessage", function( topic, data ) { // Log the topic for debugging purposes console.log( "A new message was received: ", topic ); // Use the data that was passed from our subject // to display a message preview to the user $( ".messageSender" ).html( data.sender ); $( ".messagePreview" ).html( data.body );});// Here's another subscriber using the same data to perform// a different task.// Update the counter displaying the number of new// messages received via the publishervar subscriber2 = subscribe( "inbox/newMessage", function( topic, data ) { $('.newMessageCounter').html( mailCounter++ );});publish( "inbox/newMessage", [{ sender:"hello@google.com", body: "Hey there! How are you doing today?"}]);// We could then at a later point unsubscribe our subscribers// from receiving any new topic notifications as follows:// unsubscribe( subscriber1, );// unsubscribe( subscriber2 );            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 石屏县| 平湖市| 喀什市| 城市| 沽源县| 顺义区| 息烽县| 时尚| 佳木斯市| 海南省| 罗山县| 汉寿县| 宁海县| 刚察县| 安国市| 贵州省| 汽车| 临汾市| 夏津县| 牙克石市| 哈尔滨市| 尼玛县| 年辖:市辖区| 宾川县| 四子王旗| 武穴市| 韶关市| 澄江县| 松桃| 玉门市| 渭源县| 英德市| 江门市| 依兰县| 西丰县| 尼木县| 金沙县| 麦盖提县| 德安县| 济源市| 天全县|