如何实现JavaScript的调解员(发布 - 订阅)模式 [英] How to implement the Javascript mediator (publish-subscribe) pattern

查看:97
本文介绍了如何实现JavaScript的调解员(发布 - 订阅)模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我们有一个相当复杂的Silverlight客户端,我们在HTML / JavaScript的/ CSS重写,建立在相同的Web服务上。其实我们有我们移植2 Silverlight的不同客户,其中一些共同的功能。

We have a fairly complex Silverlight client which we are rewriting in HTML/Javascript/CSS, built on top of the same web services. Actually we have two Silverlight different clients which we are porting, which share some common functionality.

我读了 http://addyosmani.com/largescalejavascript/ 的文章和我打算使用提出的架构,并且特别是介体图案。

I read the article on http://addyosmani.com/largescalejavascript/ and am planning to use the proposed architecture, and in particular the mediator pattern.

由阿迪描述的模式的万英尺概述:

A 10000 feet overview of the pattern described by Addy:


  • code分为小模块

  • 模块只知道调解对象的;模块不能与其他模块直接通信

  • 调解员对出版一个简单的界面和订阅的消息

  • 模块可以订阅信息(通过中介API),使一个回调函数

  • 模块可以将消息发布到调解员,具有参数对象,调解员要求订阅消息中的任何模块的回调方法,传递参数对象

这里的一个主要目标是实现模块之间的松耦合。因此,我们可以重复使用两个客户端的模块。而在隔离测试模块。和调解员应当是全球唯一的对象,我们需要的,这一定是不错的。

One of the main goals here is to achieve loose coupling between modules. So we can reuse the modules in the two clients. And test the modules in isolation. And the mediator should be the only global object we need, which has got to be good.

不过,虽然我喜欢这个想法,我有一种感觉它在某些情况下过于复杂,而且一些我的团队成员也不会相信。让我用例子来说明:

But although I like the idea, I have the feeling it is overly complicated in some cases, and that some of my team members will not be convinced. Let me explain by example:

假设我们有一个执行计算的辅助功能 - 可以说这格式的字符串 - 并承担这个功能应该是提供给任何模块。此功能可在工具或帮助模块中,然后可重复使用和可测试的归属。

Assume we have a helper function which performs a calculation - lets say it formats a string - and assume this function should be available to any module. This function could belong in a 'tools' or 'helper' module which is then reusable and testable.

要从任意模块调用这个函数我要发布的消息,像 的formatString 我的输入字符串作为参数。和辅助功能已订阅的的formatString 消息。但在此之前我发布的的formatString 的消息我必须首先订阅消息例如 formatStringResult ,用一个回调函数可接收的结果。然后,一旦我得到的结果回来,我从退订 formatStringResult 消息。

To call this function from an arbitrary module I have to post a message, something like formatString with my input string as parameter. And the helper function has subscribed to the formatString message. But before I post the formatString message I first have to subscribe to a message like formatStringResult, with a callback function which can receive the result. And then once I get the result back, I unsubscribe from the formatStringResult message.

问题(S)


  • 如果调解员,而直接在自己的界面提供这种类型的辅助功能?

  • 或者我应该延长发布接口,允许一个可选的结果参数,其中辅助方法可以直接写结果?

  • 是具有真正值得额外的中介层的权衡
    实现松耦合的好处?

我真的AP preciate开发商的意见与实现松耦合的经验,复杂的JavaScript应用程序。

I'd really appreciate advice from developers with experience of achieving loose-coupling in 'complex' JavaScript applications.

推荐答案

您实际上完全描述的 BarFoos应用程序框架

https://github.com/jAndreas/BarFoos

这篇关于如何实现JavaScript的调解员(发布 - 订阅)模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆