JavaScript編程設(shè)計(jì)模式之觀察者模式(Observer Pattern)實(shí)例詳解
本文實(shí)例講述了JavaScript編程設(shè)計(jì)模式之觀察者模式。分享給大家供大家參考,具體如下:
簡(jiǎn)介
簡(jiǎn)單的解釋觀察者模式,就是一個(gè)對(duì)象(subject)維護(hù)一個(gè)依賴他的對(duì)象(observers)列表,當(dāng)自身狀態(tài)發(fā)生變化時(shí),自動(dòng)通知所有觀察者對(duì)象。當(dāng)某個(gè)對(duì)象不需要獲得通知時(shí),可以從對(duì)象列表中刪除掉。
從上面的解釋中我們可以提煉出三個(gè)componet: Subject, ObserverList和Observer,用JS實(shí)現(xiàn)很簡(jiǎn)單:
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 extension
function 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 );
}
};
最后定義一個(gè)觀察者對(duì)象,實(shí)現(xiàn)update方法
// The Observer
function Observer(){
this.Update = function(){
// ...
};
}
當(dāng)有多個(gè)觀察者,只需擴(kuò)展上面的基本對(duì)象,并重寫(xiě)Update方法。
盡管觀察則模式被廣泛使用,但在JS中經(jīng)常使用它的變體: 發(fā)布訂閱模式
發(fā)布訂閱模式通過(guò)一個(gè)topic/event通道,解耦了觀察者模式中Subject(發(fā)布者)和Observer(訂閱者)之間耦合的問(wèn)題,在JS中被廣泛使用。
下面簡(jiǎn)單的例子說(shuō)明了使用發(fā)布訂閱模式的基本結(jié)構(gòu)
// A very simple new mail handler
// A count of the number of messages received
var mailCounter = 0;
// Initialize subscribers that will listen out for a topic
// with the name "inbox/newMessage".
// Render a preview of new messages
var 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 publisher
var 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 );
發(fā)布訂閱模式的實(shí)現(xiàn)
許多Js庫(kù)都很好的實(shí)現(xiàn)了發(fā)布訂閱模式,例如Jquery的自定義事件功能。
// Publish
// jQuery: $(obj).trigger("channel", [arg1, arg2, arg3]);
$( el ).trigger( "/login", [{username:"test", userData:"test"}] );
// Dojo: dojo.publish("channel", [arg1, arg2, arg3] );
dojo.publish( "/login", [{username:"test", userData:"test"}] );
// YUI: el.publish("channel", [arg1, arg2, arg3]);
el.publish( "/login", {username:"test", userData:"test"} );
// Subscribe
// jQuery: $(obj).on( "channel", [data], fn );
$( el ).on( "/login", function( event ){...} );
// Dojo: dojo.subscribe( "channel", fn);
var handle = dojo.subscribe( "/login", function(data){..} );
// YUI: el.on("channel", handler);
el.on( "/login", function( data ){...} );
// Unsubscribe
// jQuery: $(obj).off( "channel" );
$( el ).off( "/login" );
// Dojo: dojo.unsubscribe( handle );
dojo.unsubscribe( handle );
// YUI: el.detach("channel");
el.detach( "/login" );
簡(jiǎn)單實(shí)現(xiàn)
var pubsub = {};
(function(q) {
var topics = {},
subUid = -1;
// Publish or broadcast events of interest
// with a specific topic name and arguments
// such as the data to pass along
q.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 observed
q.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 subscription
q.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 ));
使用方法
// Another simple message handler
// A simple message logger that logs any topics and data received through our
// subscriber
var messageLogger = function ( topics, data ) {
console.log( "Logging: " + topics + ": " + data );
};
// Subscribers listen for topics they have subscribed to and
// invoke a callback function (e.g messageLogger) once a new
// notification is broadcast on that topic
var subscription = pubsub.subscribe( "inbox/newMessage", messageLogger );
// Publishers are in charge of publishing topics or notifications of
// interest to the application. e.g:
pubsub.publish( "inbox/newMessage", "hello world!" );
// or
pubsub.publish( "inbox/newMessage", ["test", "a", "b", "c"] );
// or
pubsub.publish( "inbox/newMessage", {
sender: "hello@google.com",
body: "Hey again!"
});
// We cab also unsubscribe if we no longer wish for our subscribers
// to be notified
// pubsub.unsubscribe( subscription );
// Once unsubscribed, this for example won't result in our
// messageLogger being executed as the subscriber is
// no longer listening
pubsub.publish( "inbox/newMessage", "Hello! are you still there?" );
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
ES6標(biāo)準(zhǔn) Arrow Function(箭頭函數(shù)=>)
ES6標(biāo)準(zhǔn)新增了一種新的函數(shù):Arrow Function(箭頭函數(shù)),為什么叫Arrow Function?因?yàn)樗亩x用的就是一個(gè)箭頭2020-05-05
layui實(shí)現(xiàn)下拉框三級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了layui實(shí)現(xiàn)下拉框三級(jí)聯(lián)動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
IE autocomplete internet explorer''s autocomplete
IE autocomplete internet explorer''s autocomplete...2007-06-06
JS組件系列之MVVM組件 vue 30分鐘搞定前端增刪改查
這篇文章主要介紹了JS組件系列之MVVM組件 vue 30分鐘搞定前端增刪改查,需要的朋友可以參考下2017-04-04
跟我學(xué)習(xí)javascript的var預(yù)解析與函數(shù)聲明提升
跟我學(xué)習(xí)javascript的var預(yù)解析與函數(shù)聲明提升,小編對(duì)var預(yù)解析與函數(shù)聲明提升知識(shí)點(diǎn)也不甚了解,和大家一起學(xué)習(xí)本篇文章。2015-11-11
JS運(yùn)動(dòng)特效之同時(shí)運(yùn)動(dòng)實(shí)現(xiàn)方法分析
這篇文章主要介紹了JS運(yùn)動(dòng)特效之同時(shí)運(yùn)動(dòng)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了javascript同時(shí)運(yùn)動(dòng)的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01
js中document.getElementByid、document.all和document.layers區(qū)分介紹
document.getElementById 是公共標(biāo)準(zhǔn),被目前的所有主流瀏覽器支持,document.all只有IE支持,document.layers是Netscape 4.x專有的屬性2011-12-12

