观察者模式和中介者模式的异同

161 views
Skip to first unread message

star

unread,
Oct 21, 2014, 3:08:06 AM10/21/14
to f2...@googlegroups.com

观察者模式实现:

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

中介者模式实现:

var mediator = (function(){

    // Storage for topics that can be broadcast or listened to
    var topics = {};

    // Subscribe to a topic, supply a callback to be executed
    // when that topic is broadcast to
    var subscribe = function( topic, fn ){

        if ( !topics[topic] ){ 
          topics[topic] = [];
        }

        topics[topic].push( { context: this, callback: fn } );

        return this;
    };

    // Publish/broadcast an event to the rest of the application
    var publish = function( topic ){

        var args;

        if ( !topics[topic] ){
          return false;
        } 

        args = Array.prototype.slice.call( arguments, 1 );
        for ( var i = 0, l = topics[topic].length; i < l; i++ ) {

            var subscription = topics[topic][i];
            subscription.callback.apply( subscription.context, args );
        }
        return this;
    };

    return {
        Publish: publish,
        Subscribe: subscribe,
        installTo: function( obj ){
            obj.subscribe = subscribe;
            obj.publish = publish;
        }
    };

}());

从代码上看起来两者是相同的,不同就在于这两种模式的着眼点和使用方法:

观察者模式

观察者模式的核心思想是通过一个对象建立多个事件源之间的传播通道,事件源需要订阅该对象的事件而不需要知道其它的事件源。

在使用上, 观察者模式被用来对多个事件的关系进行解耦合。

中介者模式

中介者模式则是用来协调多个事件之间的关系,做法是把对象之间的关系抽取出来统一放到一个更高层次的对象中进行决策处理。让我们可以更快的浏览整个工程流,而不用通过深层次的阅读代码。

中介者

两者的区别

观察者模式的决策者是订阅者而中介者模式的决策者是中介者,这个本质的区别决定了两个模式在使用上的不同。

具体不同的地方包括两点,事件和管理器的使用。事件对于观察者模式来说用来进行事件的传播,是非常重要的一个实现手段,而对于中介者模式来说,重要的点在于告知管理器现在完成的事情以及将要做的事情,所以不必要使用事件来实现通信。而在管理器的使用上,观察者模式的管理器是很弱的,他不需要知道订阅者之间的关系,它的作用仅仅是传播事件。而中介者模式的管理器需要知道订阅者之间的关系,并能够在发生某个事件的时候通知订阅者完成下一项事情。

参考:backbone-fundamentals/#event-aggregators-and-mediators

Reply all
Reply to author
Forward
0 new messages