admin管理员组文章数量:1302360
I've seen that in some projects, Module Pattern
uses instead of Singleton Pattern
and vice versa.
I want to know exactly, what's the different between Module Pattern
and Singleton Pattern
?
I've seen that in some projects, Module Pattern
uses instead of Singleton Pattern
and vice versa.
I want to know exactly, what's the different between Module Pattern
and Singleton Pattern
?
- 4 Please take a look at this: addyosmani./resources/essentialjsdesignpatterns/book/… – Teemu Commented Nov 17, 2012 at 9:15
- 1 @Teemu This is a good document but it's not clarify the different between those. – Afshin Mehrabani Commented Nov 17, 2012 at 10:30
- different names or implementations for nonsense? – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ Commented Nov 17, 2012 at 15:56
1 Answer
Reset to default 8Module pattern in javascript refers to the modularisation of code for a mon mechanism. It works quite well to split one "class" across several files as you can define constructor and various groups of prototype methods independently. Each of the modules is usually wrapped inside a closure to create static, local variables - this is called revealing module pattern.
Singleton pattern in javascript refers to the restriction of instance creations, often using lazy initialisation.
Of course you can consider the module pattern to be a specialisation of the singleton pattern (see the Wikipedia article), the constructor and its prototype object would take the part of the "single instance" then.
Yet you also could bine them "independently": A module that defines a class which uses the singleton approach.
本文标签: javascriptDifferent between Module Pattern and Singleton PatternStack Overflow
版权声明:本文标题:javascript - Different between Module Pattern and Singleton Pattern? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741643682a2390062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论