[英]Javascript / Node JS best way to create singleton object
我完成了我的家庭作業並取得了完美的成績。 但我只是想檢查一下,這是創建單例實例的最佳方式還是其他任何方式:
我使用模塊模式(閉包)創建了一個單例對象,“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());
實際單例對象(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;
然后Singleton對象用戶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;
單例對象用戶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;
請注意,單用戶1和用戶2,是出於某個目的,根據我的項目,上面只是現實世界問題的原型。
我的問題是,我確信這是創建一個類的單個實例(我使用上面的app.js檢查)。 但這是最好的方法嗎?
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
這是我的可配置單件服務
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); }
我發現JavaScript中的單例類有點不穩定,在java中它很清楚,即無論何時創建類的對象,你都得到相同的對象,但在JS中,(至少是IMO)沒有真正的類開始。 (不,ES6課程不計算,回答這個,你能擁有私人屬性嗎?)
你的代碼只是做一個閉包,它可能是下面的代碼並沒有區別:
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
};
也就是說,一天結束時,Singleton只是一種模式,我們的實施方式取決於我們,你的方式沒有什么特別的錯誤。
編輯:一個比我更了解JS的人的單例實現(取自學習JavaScript設計模式 )
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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.