Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript / Node JS best way to create singleton object

I completed my home work and got perfect result. But i just want to check , is this the best way to create singleton instances or is their any other way:

I created a singleton object using the module pattern (closures) as, "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 singleton 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 consider that, Single User 1 and User 2, is for a purpose according to my project, the above is just an prototype to the real world problem.

My question is, I am sure this is creating a single instance of the class (As i checked using the app.js above). But is this the best way?

like image 597
user3278897 Avatar asked Jan 05 '16 01:01

user3278897


2 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
like image 151
Mihails Butorins Avatar answered Sep 18 '22 00:09

Mihails Butorins


this is my configurable singleton for a 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);
}
like image 33
David Rearte Avatar answered Sep 20 '22 00:09

David Rearte