模块 +门面+ JavaScript 中的中介者组合
我已经阅读了一篇关于 http://addyosmani.com/largescalejavascript/ 的精彩文章,
本文介绍了如何将模块、外观和中介器组合在一起形成 JavaScript 应用程序。
我可以编写文章中解释的内容,但我不明白为什么需要使用门面来访问中介器。
我想我可以直接从我的模块访问中介器。
请告诉我..
PS:这是一个有效的问题吗?
I've finished reading a great article on http://addyosmani.com/largescalejavascript/
This article explains how to combine modules, a facade, and a mediator together for a JavaScript application.
I can code what the article explains, but I don't understand why I need to use facade to access the mediator.
I think I can access the mediator directly from my modules.
Please advise me..
P.S: Is it a valid question here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该部分的参考资料 Pro Javascript Design Patterns 很好地回答了您的问题,内容如下:
The reference material for that section, Pro Javascript Design Patterns, answers your question quite nicely when it says the following:
以 Thedric Walker 的答案为基础……
采用这个也许很荒谬的类比:
你是一只啄木鸟(模块),你想挑出一些躺在房屋支撑结构(核心)上的幼虫(数据)。通常情况下,您只需将幼虫从基础设施上啄下来(实际上啄木鸟有一条长长的带刺的舌头,环绕着它们的头骨),但一些木匠(应用架构师)在房子的外面应用了立面。然而,木匠很友善,在正面戳了一些洞,只允许啄木鸟接近幼虫。此外,木匠足够聪明,可以将这些孔做成一定的形状,从而使啄木鸟每次啄食都能完成更多的幼虫进食任务。
那么为什么还要有一个外观呢?
将外观与 Addy 的帖子联系起来——外观增强了沙箱,因为沙箱提供了预定义的功效,允许一个模块完成任务以特定方式,例如批发。例如,您不希望沙箱强制用户写入:
var node = getNode('#errorPanel');
node.innerHTML = 'Field is invalid!';
更好的沙箱可能具有如下内容:
notifyUser.error('Field is invalid!');
外观 另一方面,可以通过仅监听
mediator.fire('error:Validation', 'Field is invalid!')< /代码> 频道。
我一直在使用 Addy 文章中的外观,理论上可以简单地将请求转发到核心。也许,它会检查通道信号中的某些内容,以确保雀科动物不会试图啄食幼虫,从而导致其生病,例如抛出异常。
这就是为什么为您的核心提供一个单独的通道媒介(即调解器)可能是有意义的,其中您的外观是核心的唯一“引用者” - 而您的模块仅引用外观 。另一种方法是在您的团队中强制执行离散通道名称约定,例如 mediator.fire('ready://Core')代码> &
mediator.fire('updated://ToDos/task', taskId)
。facade
会监听'updated://ToDos/task'
并向core
发出请求——这可能看起来类似于:注意!不要不要直接在侦听器调用中编写处理程序!
我真的希望这会有所帮助,并且我不会因为谈论鸟类而被否决;)
Building on Thedric Walker's answer...
Take this, perhaps ridiculous, analogy:
YOU are a woodpecker (module), and you would like to pick out some larvae (data) which have been lain on a house's support-structure (Core). Normally, you'd just peck (actually woodpeckers have a long, barbed tongue that wraps around their skull) the larvae off of the infrastructure, but some carpenter (Application Architect) has applied a Facade on the outside of the house. However, the carpenter was nice enough to poke holes in the Facade to allow only woodpeckers to access the larvae. Furthermore, the carpenter was smart enough to make these holes a certain shape -- allowing the woodpecker to accomplish more larvae-eating tasks per peck.
So why have a facade, again?
Relating the Facade back to Addy's post -- the Facade augments a Sandbox in that a sandbox provides predefined efficacies which allow one module to accomplish tasks in specific ways, e.g. wholesale. For instance, you wouldn't want a sandbox that forces the user to write:
var node = getNode('#errorPanel');
node.innerHTML = 'Field is invalid!';
A better sandbox may have something like:
notifyUser.error('Field is invalid!');
The Facade on the other hand, can provide those throughways just the same, by only listening to the
mediator.fire('error:Validation', 'Field is invalid!')
channel.The Facade in Addy's post, which I use all the time, could theoretically simply forward requests onto the Core. Probably, it would check something in the channel's signal to make sure, say, a Finch isn't trying to peck larvae that will make it sick -- e.g. throw an Exception.
This is why it may make sense to have a separate channel-medium (ie mediator) for your core, where your Facade is the sole 'referencer' to the Core -- and your modules only reference the Facade. Another way, is to enforce amongst your team a convention for discrete-channel-names, e.g.
mediator.fire('ready://Core')
&mediator.fire('updated://ToDos/task', taskId)
. Thefacade
would listen to'updated://ToDos/task'
and would make request(s) to thecore
-- which may look something like:ATTENTION! do not write your handlers directly within your listener calls!
I really hope this helps and that I don't get downvoted for talking about birds ;)