简体   繁体   English

中介模式与直接调用中间函数

[英]Mediator Pattern vs. Direct Calls to Intermediate Function

How is the Mediator design pattern, eg something like this: Mediator的设计模式如何,例如:

var mediator = (function(){

var _jobs = {};


function _extractedParams(arguments){
    var arr = [];
    if (arguments.length > 1){
        for (var i=1; i < arguments.length; i++) arr.push(arguments[i]);
    } 
    return arr;
}


function _defaultSubscribeCallback(){
    console.log("_defaultSubscribeCallback()");
}


return {

    subscribe: function(){
        var channel = arguments[0];

        if (!_jobs.hasOwnProperty(channel)) _jobs[channel] = [];

        _jobs[channel].push({
            originalParams: _extractedParams(arguments),
            callback: arguments[1] || _defaultSubscribeCallback
        });

    },

    publish: function(){
        var channel = arguments[0];

        if (!_jobs.hasOwnProperty(channel)) return 0;

        for (var i=0; i < _jobs[channel].length; i++){
            var subscription = _jobs[channel][i];

            subscription.callback.apply(this, subscription.originalParams);
        }

        return 1;  
    }

};

}());

var module1 = (function(){

    return {
        myFunction: function(){
           console.log(arguments[0]); //"Hello"
        }
    };

}());

mediator.subscribe("LOG_IN", module1.myFunction, "Hello");

var module2 = (function(){

    return {

        afterAuth: function(){
           mediator.publish("LOG_IN");
        }

    };

}());

//After user authentication
module2.afterAuth();

...better than simply calling an intermediate function when one module wants to communicate with another, eg something like this?: ...比当一个模块要与另一个模块进行通信(例如类似这样)简单地调用一个中间函数更好:

var mediator = (function(){

    return {
        runFunction: function(){
            var params;

            if (arguments.length > 1){
                params = [];
                for (var i=1; i < arguments.length; i++) params.push(arguments[i]);
            }

            if (arguments[0] && typeof arguments[0] === "function") arguments[0](params);
        }
    };

}());

var module1 = (function(){

    return {
        myFunction: function(){
           console.log(arguments[0]); //"Hello"
        }
    };

}());

var module2 = (function(){

    return {

        afterAuth: function(){
           mediator.runFunction(module1.myFunction, "Hello");
        }

    };

}());

//After user authentication
module2.afterAuth();

While both strategies seem to have the same benefits (decoupling, etc.), the first example produces overhead by holding onto the subscriptions in _jobs (though I didn't include it, I would supply an unsubscribe() method to help mitigate this, but it would remain an issue); 虽然两种策略似乎都具有相同的好处(解耦等),但第一个示例通过保留_jobs的订阅来产生开销(虽然我没有包含它,但我会提供一个unsubscribe()方法来帮助缓解这种情况,但这仍然是一个问题); is far more complicated than the second example; 比第二个例子复杂得多; will cause a slight dip in performance; 会导致性能略有下降; and is more likely to encounter problems. 并且更有可能遇到问题。

What am I not understanding about the Mediator pattern? 我对Mediator模式的理解是什么? Why does it seem like an unnecessarily convoluted way of achieving a simple result? 为什么它看起来像是一种不必要的复杂方式来实现简单的结果? Why would I want all those subscriptions floating around in _jobs ? 为什么我希望所有这些订阅在_jobs浮动?

I realize now that the important difference between the first and second examples is that in the second, module2 references module1 via mediator.runFunction(module1.myFunction, "Hello"); 我现在认识到的第一例和第二例之间的重要区别在于,在第二, module2引用module1通过mediator.runFunction(module1.myFunction, "Hello"); , while in the first example, module2 does not reference module1 , instead referencing only the mediator via mediator.publish("LOG_IN"); ,在第一个示例中, module2 引用module1 ,而是仅通过mediator.publish("LOG_IN");引用介mediator.publish("LOG_IN"); Clearly, the second example, as pointed out by @user943702, "serves no purpose." 显然,正如@ user943702所指出的那样,第二个例子“毫无用处”。

The first approach is actually an example of Observer Pattern , in which publisher publish and subscribers listen, whereas in Mediator Pattern all modules communicate with each other through a mediator. 第一种方法实际上是Observer Pattern的示例,其中发布者进行发布,订阅者侦听,而在Mediator Pattern中,所有模块都通过中介器相互通信。 Both patterns share the same benefit ie Decoupling. 两种模式都具有相同的优势,即去耦。 In the second approach module1 is communicating with module2 and is not decoupled (module1 is directly referring to module2) and it seems to be a perfect fit for applying Mediator Pattern if in future you are going to do lots of changes in modules and their interfaces. 在第二种方法中,module1与module2 通信并且没有解耦(module1直接引用module2),并且它似乎非常适合应用Mediator Pattern,如果将来你要对模块及其接口进行大量更改。 Better not let modules worry about the interface, let mediator do it. 最好不要让模块担心接口,让调解员去做。 Separation of concerns :) 关注点分离 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM