Created
December 10, 2014 12:12
-
-
Save barmatz/e19adfd5d9ac73fa8b94 to your computer and use it in GitHub Desktop.
Node.js Singleton pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var singletonLock = {}, | |
| instance; | |
| function MySingleton(lock) { | |
| if (!(lock instanceof singletonLock)) { | |
| throw new Error('This is a singleton, do not initiate!'); | |
| } | |
| } | |
| MySingleton.getInstance = function () { | |
| return instance || (instance = new MySingleton(singletonLock)); | |
| }; | |
| module.exports = MySingleton; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment