Created
July 29, 2015 18:35
-
-
Save murdockcrc/e5ff41e5fdc17e7dd36d to your computer and use it in GitHub Desktop.
NodeJS Singleton pattern
This file contains 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
exports = module.exports = (function () { | |
// Instance stores a reference to the Singleton | |
var instance; | |
function init() { | |
// Singleton | |
// Private methods and variables | |
return { | |
// Public methods and variables | |
extend: function extend(extension, obj){ | |
for (var key in extension){ | |
obj[key] = extension[key]; | |
} | |
} | |
}; | |
}; | |
return { | |
// Get the Singleton instance if one exists | |
// or create one if it doesn't | |
getInstance: function () { | |
if (!instance) { | |
instance = init(); | |
} | |
return instance; | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot!