nodejs中的单例模式是否需要? [英] Singleton pattern in nodejs - is it needed?

查看:266
本文介绍了nodejs中的单例模式是否需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了关于如何在nodejs中编写单例的文章。



http://simplapi.wordpress.com/2012/05/14/node-js-singleton-structure/



我知道的文档需要 状态

 模块在第一次加载后被缓存。 
多次调用require('foo')可能不会导致多次执行模块代码。

所以似乎每个必需的模块都可以轻松地使用没有单例样板代码的单例。 p>

问题:



上述文章提供了关于创建单身人士的解决方案?

解决方案

这与nodejs缓存基本上有关。简单而简单。



https:// nodejs.org/api/modules.html#modules_caching



(v 6.3.1)


缓存



模块在第一次加载后被缓存。这意味着
(除其他外),每次调用require('foo')将获得
完全相同的对象返回,如果它将解析为相同的
文件。



多次调用require('foo')可能不会导致模块代码执行多次
。这是一个重要的功能。使用它,可以返回
部分完成对象,从而允许传递
依赖关系,即使它们会导致循环。



如果您想要一个模块多次执行代码,然后导出
a函数,并调用该函数。



模块缓存注意事项



模块根据其解析的文件名进行缓存。由于模块可以
根据调用
模块的位置(从node_modules文件夹加载)解析为不同的文件名,所以不要保证
require('foo')将始终返回完全相同的对象,如果它将
解析为不同的文件。



另外,在不区分大小写的文件系统或操作系统上,
不同的解决文件名可以指向同一个文件,但缓存
仍然将它们视为不同的模块,并将重新加载文件
多次。例如,require('./ foo')和require('./ FOO')
返回两个不同的对象,而不管./foo和
./FOO是否是相同的文件。


所以简单来说



如果你想要一个Singleton; 导出一个对象



如果你不想要一个Singleton; 导出一个函数(并做东西/返回的东西/该功能的任何东西)。


I recently came across this article on how to write a singleton in nodejs

http://simplapi.wordpress.com/2012/05/14/node-js-singleton-structure/

I know that the documentation of require states that:

Modules are cached after the first time they are loaded. 
Multiple calls to require('foo') may not cause the module code to be executed multiple times.

So it seems that every required module can be easily used a singleton without the singleton boilerplate code.

Question:

Does the above article provide a round about solution to creating a singleton?

解决方案

This has basically to do with nodejs caching. Plain and simple.

https://nodejs.org/api/modules.html#modules_caching

(v 6.3.1)

Caching

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

Multiple calls to require('foo') may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.

If you want to have a module execute code multiple times, then export a function, and call that function.

Module Caching Caveats

Modules are cached based on their resolved filename. Since modules may resolve to a different filename based on the location of the calling module (loading from node_modules folders), it is not a guarantee that require('foo') will always return the exact same object, if it would resolve to different files.

Additionally, on case-insensitive file systems or operating systems, different resolved filenames can point to the same file, but the cache will still treat them as different modules and will reload the file multiple times. For example, require('./foo') and require('./FOO') return two different objects, irrespective of whether or not ./foo and ./FOO are the same file.

So in simple terms.

If you want a Singleton; export an object.

If you do not want a Singleton; export a function (and do stuff/return stuff/whatever in that function).

这篇关于nodejs中的单例模式是否需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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