Javascript / Node JS is the best way to create a singleton object

I completed my homework and got a great result. But I just want to check if this is the best way to create singleton instances or is them any other way:

I created a singleton object using a module (closure) template, for example, "app.js"

var singleton1 = require('./singletonUser1'); console.dir(singleton1.getlocalvariable()); singleton1.setlocalvariable(20); console.dir(singleton1.getlocalvariable()); var singleton2 = require('./singletonUser2'); console.dir(singleton2.getlocalvariable()); singleton2.setlocalvariable(30); console.dir(singleton.getlocalvariable()); 

Actual single object (singleton.js):

 var singleton = (function () { var localvariable = 10; return { getlocalvariable: function () { console.dir('This is getInstance'); return localvariable; }, setlocalvariable: function (value) { console.dir('This is setlocalvariable'); localvariable = value; }, }; })(); module.exports = singleton; 

Then Singleton object user 1 (singletonUser1.js):

 var singletonUser1 = (function () { var singleton = require('./singleton'); return { getlocalvariable: function () { console.dir('This is singletonUser1---getlocalvariable'); return singleton.getlocalvariable(); }, setlocalvariable: function (value) { console.dir('This is singletonUser1---setlocalvariable'); singleton.setlocalvariable(value); }, }; })(); module.exports = singletonUser1; 

Singleton Object User 2 (singletonUser2.js)

 var singletonUser2 = (function () { var singleton = require('./singleton'); return { getlocalvariable: function () { console.dir('This is singletonUser2222---getlocalvariable'); return singleton.getlocalvariable(); }, setlocalvariable: function (value) { console.dir('This is singletonUser22222---setlocalvariable'); singleton.setlocalvariable(value); }, }; })(); module.exports = singletonUser2; 

Please note that Single User 1 and User 2 are for a specific purpose according to my project, it is just a prototype of a real-world problem.

My question is: I'm sure this creates one instance of the class (as I checked using app.js above). But is this the best way?

+5
source share
3 answers

 var Singleton = (function(){ function Singleton(){ this.localVariable = 5; } // Object can have instance methods as usually. Singleton.prototype.getLocalVariable = function() { return this.localVariable; }; var instance; return function() { if (!instance) { instance = new Singleton(); } return instance; }; })(); var instance1 = new Singleton(); var instance2 = new Singleton(); console.log(instance1 === instance2); // true console.log(instance1.localVariable, instance2.localVariable); // 5 5 instance1.localVariable = 20; console.log(instance1.localVariable, instance2.localVariable); // 20 20 console.log(instance1.getLocalVariable()); // 20 
+5
source

this is my custom singleton for service

 function AdService(name) { console.log('new instance created'); this.name = name || 'defaultName'; this.greet = function () { console.log('hi ' + this.name); } }; function Singleton() { this.instance = null; this.getInstance = function getInstance(name) { if (!this.instance) this.instance = new AdService(name); return this.instance; } } var singleton = new Singleton(); module.exports = function (name) { return singleton.getInstance(name); } 
+1
source

I find a singleton class in JavaScript a bit shaky, in java this is perfectly clear, i.e. whenever you create a class object, you get the same object, but in JS (at least IMO) there is no true class to start with. (no, ES6 classes are not counted, answer this, can you have private attributes in it?)

You only encode the closure, maybe it was the code below and it didn't matter:

 var localvariable = 10; function getlocalvariable() { console.dir('This is getInstance'); return localvariable; }; function setlocalvariable(value) { console.dir('This is setlocalvariable'); localvariable = value; }; module.exports = { getlocalvariable: getlocalvariable, setlocalvariable: setlocalvariable }; 

who said that the end of the day, Singleton is just a model of how we realize it depends on us, nothing special is the way you did it.

Edit: Implementing single-user mode by someone who knows JS better than me (taken from Learning JavaScript Design Patterns )

 var mySingleton = (function () { // Instance stores a reference to the Singleton var instance; function init() { // Singleton // Private methods and variables function privateMethod(){ console.log( "I am private" ); } var privateVariable = "Im also private"; var privateRandomNumber = Math.random(); return { // Public methods and variables publicMethod: function () { console.log( "The public can see me!" ); }, publicProperty: "I am also public", getRandomNumber: function() { return privateRandomNumber; } }; }; return { // Get the Singleton instance if one exists // or create one if it doesn't getInstance: function () { if ( !instance ) { instance = init(); } return instance; } }; })(); var myBadSingleton = (function () { // Instance stores a reference to the Singleton var instance; function init() { // Singleton var privateRandomNumber = Math.random(); return { getRandomNumber: function() { return privateRandomNumber; } }; }; return { // Always create a new Singleton instance getInstance: function () { instance = init(); return instance; } }; })(); // Usage: var singleA = mySingleton.getInstance(); var singleB = mySingleton.getInstance(); console.log( singleA.getRandomNumber() === singleB.getRandomNumber() ); // true var badSingleA = myBadSingleton.getInstance(); var badSingleB = myBadSingleton.getInstance(); console.log( badSingleA.getRandomNumber() !== badSingleB.getRandomNumber() ); // true // Note: as we are working with random numbers, there is a // mathematical possibility both numbers will be the same, // however unlikely. The above example should otherwise still // be valid. 
0
source

Source: https://habr.com/ru/post/1239845/


All Articles