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?

Share Improve this question edited Nov 17, 2012 at 11:03 Afshin Mehrabani asked Nov 17, 2012 at 9:04 Afshin MehrabaniAfshin Mehrabani 35k33 gold badges144 silver badges209 bronze badges 3
  • 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
Add a ment  | 

1 Answer 1

Reset to default 8

Module 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