如何实现 Javascript 中介(发布-订阅)模式 [英] How to implement the Javascript mediator (publish-subscribe) pattern

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

问题描述

背景

我们有一个相当复杂的 Silverlight 客户端,我们正在用 HTML/Javascript/CSS 重写它,构建在相同的 Web 服务之上.实际上,我们正在移植两个不同的 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.

Addy 描述的模式的 10000 英尺概览:

A 10000 feet overview of the pattern described by Addy:

  • 代码被分成小模块
  • 模块只知道一个中介对象;模块不能直接与其他模块通信
  • 中介者有一个简单的界面来发布和订阅消息
  • 模块可以订阅消息(通过中介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.

问题

  • 中介是否应该直接在自己的界面中提供这种类型的辅助功能?
  • 或者我应该扩展发布接口以允许一个可选的结果参数,其中辅助方法可以直接写入结果?
  • 拥有一个额外的中介层真的值得吗?实现松耦合的好处?

我非常感谢有在复杂"JavaScript 应用程序中实现松耦合的经验的开发人员的建议.

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

推荐答案

您实际上完美地描述了 BarFoos 应用程序框架:

You actually perfectly described the BarFoos application Framework:

https://github.com/jAndreas/BarFoos

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

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