简体   繁体   English

将主干与pub / sub分离

[英]Decoupling backbone with pub/sub

I'm in the process of revamping the way I use backbone/require, I found out the hard way on how bad of a practice it is to have modules coupled (I didn't understand decoupling at the time). 我正在修改使用骨干网/需求的方式,我发现很难将模块耦合起来是多么糟糕的做法(当时我还不了解去耦合)。

I started to play with MinPubSub and understand that for the most part but, from what I read, other modules shouldn't subscribe to other modules (that makes it coupled?). 我开始玩MinPubSub,并在很大程度上理解了这一点,但是从我阅读的内容来看,其他模块不应该订阅其他模块(这会使它耦合吗?)。 Instead their should be a mediator between all the modules which tell them how to interact. 相反,他们应该是所有模块之间的中介者,这些模块告诉他们如何进行交互。

I assume this mediator is subscribed to all modules and all modules are subscribed to the mediator? 我假定此调解器已订阅所有模块,而所有模块都已订阅了调解器?

I have no idea how to implement this and yet to find a solid code example on how to implement this with backbone, any help with adding pubsub to backbone would be appreciated. 我不知道如何实现此目标,但仍未找到有关如何使用主干实现此示例的可靠代码示例,将pubsub添加到主干的任何帮助将不胜感激。

Sorry of this is to general of a question, trying to wrap my head around the concept and find an decent example of it used extensively. 抱歉,这是一个普遍的问题,试图绕过这个概念,并找到一个广泛使用的示例。

Ahem. 咳咳。 The pubsub approach is an implementation of the mediator pattern. pubsub方法中介者模式的一种实现。 Your mediator is your pubsub handler. 您的调解员是您的pubsub处理程序。

Let me clarify this. 让我澄清一下。 In your pubsub model, you register a channel and publish to it. 在pubsub模型中,您注册一个频道并发布到该频道。 Anything in there that is potentially listening will get whatever you publish in the order they subscribed. 那里任何可能在监听的内容都将按照您订阅的顺序发布。

Contrast this with the formal definition of the mediator pattern: 将此与中介者模式的正式定义进行对比:

With the mediator pattern, communication between objects is encapsulated with a mediator object. 使用中介者模式,对象之间的通信将与中介者对象一起封装。 Objects no longer communicate directly with each other, but instead communicate through the mediator. 对象不再彼此直接通信,而是通过调解器进行通信。 This reduces the dependencies between communicating objects, thereby lowering the coupling. 这减少了通信对象之间的依赖性,从而降低了耦合。

(Plucked straight from your favourite encyclopedia ) (直接从您最喜欢的百科全书中摘录

What does this mean for you? 这对您意味着什么? That as long as you do not do what someone I know used to do, you're fine. 只要您不做我认识的人曾经做过的事情,就可以了。 This is absolutely what you want to avoid: 绝对是您要避免的事情:

  1. Module registers a channel with its own name 模块使用自己的名称注册频道
  2. Other modules refer to it by literal name by pubbing on its channel 其他模块通过在其频道上发布以实名引用它

This is where event pub/sub models break down, as it is decoupled in theory, but in practice, you need to know the exact name of the module. 这是事件发布/订阅模型崩溃的地方,因为它在理论上是分离的,但实际上,您需要知道模块的确切名称。 Make your events generic enough that you can swap out modules in-place without losing func, and prevent/refrain from getting to other modules directly, and you'll be fine on coupling. 使事件具有足够的通用性,以便可以在不丢失功能的情况下就地交换模块,并防止/避免直接进入其他模块,并且可以很好地进行耦合。

If any of this was not clear, let me know and I'll attempt to clarify. 如果有任何不清楚的地方,请告诉我,我将尝试澄清。

Funnily enough, one library I've had a lot of joy with is called Mediator . 有趣的是,一个令我很高兴的图书馆称为Mediator

It has great examples on the site, but crudely: 它在网站上有很多很好的例子,但是粗略地讲:

$.getJSON('url/to/json', function(json) {
    mediator.publish('myData:loaded', {response: json});
});

// Somewhere else
mediator.subscribe('myData:loaded', function(json) {
    // Do something
});

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

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